How to resolve the algorithm Fractal tree step by step in the Common Lisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fractal tree step by step in the Common Lisp programming language
Table of Contents
Problem Statement
Generate and draw a fractal tree.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fractal tree step by step in the Common Lisp programming language
Source code in the common programming language
;; (require :lispbuilder-sdl)
(defun deg-to-radian (deg)
"converts degrees to radians"
(* deg pi 1/180))
(defun cos-deg (angle)
"returns cosin of the angle expressed in degress"
(cos (deg-to-radian angle)))
(defun sin-deg (angle)
"returns sin of the angle expressed in degress"
(sin (deg-to-radian angle)))
(defun draw-tree (surface x y angle depth)
"draws a branch of the tree on the sdl-surface"
(when (plusp depth)
(let ((x2 (+ x (round (* depth 10 (cos-deg angle)))))
(y2 (+ y (round (* depth 10 (sin-deg angle))))))
(sdl:draw-line-* x y x2 y2 :surface surface :color sdl:*green*)
(draw-tree surface x2 y2 (- angle 20) (1- depth))
(draw-tree surface x2 y2 (+ angle 20) (1- depth)))))
(defun fractal-tree (depth)
"shows a window with a fractal tree"
(sdl:with-init ()
(sdl:window 800 600 :title-caption "fractal-tree")
(sdl:clear-display sdl:*black*)
(draw-tree sdl:*default-surface* 400 500 -90 depth)
(sdl:update-display)
(sdl:with-events ()
(:video-expose-event ()
(sdl:update-display))
(:quit-event ()
t))))
(fractal-tree 9)
You may also check:How to resolve the algorithm Pick random element step by step in the VBScript programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the Scilab programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Count in octal step by step in the ARM Assembly programming language