Draw Lindenmayer Systems with Common LISP!
- Cool stuff
A Lindenmayer system (L-system) is a model originally designed by Aristid Lindenmayer in the 60s to describe plant growth. Another common use for this system is as a technique to generate fractals, i.e, a self-similar geometric figure.
The main idea of this model is to start with a string of symbols such as "F G", and then, rewrite that original string N times, according to some rules such as "replace every F with 'F G F'" and "replace every G with 'G G F'". After doing this rewrite, we have a final string of symbols, and if we define some of those symbols to have some drawing action attached, such as 'F means draw a line forward' and 'G means draw a line to the right', we will end up with a drawing of our fractal.
An L-system requires 3 things to be defined:
The alphabet: symbols which may be replaced in our string, and which may have some drawing action attached to it. In this program, we call each of this symbols an 'aristid' (this is because the original definition of the L-system differentiates between variable symbols and constant symbols, but there is no practical difference between them, so we need a term to group them together (and
symbolis kind of taken by the LISP community))
The axiom: the original string we will start rewriting
The rules: the production rewrite rules which tells which symbols replace by which on each iteration
The main idea of this program is to define each of this things that the system requires, and then draw it, exporting it to a
Go to the examples folder to look at the code for several fractals and see them in action by running
Drawing the Dragon Curve with
- First, we want to enter SBCL (just write
sbclin the terminal, inside the root of the repository) and load this package
(ql:update-dist "quicklisp") (ql:quickload "cl-aristid")
- After the call to
use-packagewe now have access to the symbols exposed by the package, detailed in Interface.
We now want to define the different aristids of our fractal. We are calling an 'aristid' to each symbol on our Lindenmayer alphabet that does something, that is, to any drawing function. If we check the Dragon Curve definition, we have 3 aristids (F, + and -). As LISP already has defined the
- symbols, we will replace them with
(defaristid F :len 2) ; The letter F draws forward a line of length 2. (defaristid LEFT :angle 90) ; LEFT will turn left 90 degrees (defaristid RIGHT :angle -90) ; RIGHT will turn right 90 degrees
After our aristids, we want to define the production rules. This rules are the ones that will rewrite our function string on each iteration. The Dragon Curve has only 2 rules:
(X → X + Y F +) and
(Y → − F X − Y). This means that on each iteration we will replace
X + Y F + and the same happens to
; We wrap every rule in a LISP list, to use as an argument later (defparameter dragon-rules (list (defrule X -> (X RIGHT Y F RIGHT)) (defrule Y -> (LEFT F X LEFT Y))))
We have almost everything to define our fractal! Remember, a L-system consists of three things: the alphabet of symbols (our aristids), the production rules, and an axiom, which is the first string to be rewritten. In the Dragon Curve example, the axiom is
We want to call
make-fractal with all of this attributes.
(defparameter dragon (make-fractal :name "dragon" :rules dragon-rules :axiom '(F X)))
We are now ready to draw! We just call the
draw function that receives our fractal and the N iterations we want
(draw dragon 10)
- Let's pass it through vivus for one last look
defrule symbol -> (newsymbol newsymbol2) :prob 1.00
defrule is used to create a new production rule to rewrite the string.
It recives the symbol to be rewritten, then an -> to symbolize the L-system rule, and then a list of the new symbols to be used. It can also receive a
prob argument, which is explained in Stochastic L-systems and it can also use brackets symbols, which are explained in Brackets.
defaristid symbol :angle 0 :len 0 :nodraw nil :color "black"
defaristid is used to create a new aristid.
It receives the name of the symbol we are defining, and a pack of optional arguments which serve as the drawing actions.
angle means the change of direction in the current drawing,
len means how many dots will be drawn forward (i.e, the length of the line),
nodraw can be set to true to only move the direction without actually drawing, and
color can be set to any of the 140 HTML color names or even a hex value.
- if both
lenare specified, the drawing will first draw forward, and then change the angle
make-fractal :name name :axiom axiom :rules rules
make-fractal serves as the constructor for the
fractal structure. It receives a
name as a string, an
axiom which is a list of symbols and the
rules which is a list of rules
draw fractal gen
draw takes a
fractal structure and draws it's
gen iteration, saving it in a
fractalname_gen.svg. The function also has an optional
background parameter, to change the background color of the drawing.
The bracket symbols (
]) are symbols defined by Lindenmayer to save and restore the current direction of the l-system, so that when enclosing a list of symbols between brackets, a new "branch" starts to be drawn. One fractal plant has one of its rules as "(X → F+[[X]-X]-F[-FX]+X)". This can be achieved in
(defrule X -> (F RIGHT [ [ X ] LEFT X ] LEFT F [ LEFT F X ] RIGHT X))
cl-aristid supports stochastic L-systems, which are systems where rules can be chosen with a certain probability on each iteration of the rewrite. So for example, you want to draw a fractal in which one of it's symbols gets replaced by a list of new symbols only a third of the times it is called, you can define that rule with
:prob such as
(defrule A -> (A RIGHT B F RIGHT) :prob 0.33)
The aristids you define have a
color argument which can be set to any of the 140 HTML color names or hex value. For example
(defaristid A :len 8 :color "purple")
The whole drawing can have a colored background just by adding the
background argument to any HTML color in the
draw function, such as
(draw fractal 10 :background "black")
So I hear you want a fractal plant with a gray background which has some branches in white and some in salmon? Hold my parenthesis!
(defaristid F :len 4 :color "white") (defaristid G :len 4 :color "salmon") (defaristid LEFT :angle 25) (defaristid RIGHT :angle -25) (defparameter freaky-fractal-rules (list (defrule F -> (F G) :prob 0.45) (defrule G -> (F F) ) (defrule X -> (F RIGHT [ [ X ] LEFT X ] LEFT F [ LEFT F X ] RIGHT X)))) (defparameter axiom '(LEFT LEFT LEFT LEFT LEFT LEFT LEFT LEFT X)) (defparameter fractal (make-fractal :name "freaky-fractal-plant" :rules freaky-fractal-rules :axiom axiom)) (draw fractal 6 :background "gray")
:color "rainbow" to your aristid to give it more groove
(defaristid F :len 2 :color "rainbow") (defaristid LEFT :angle 90) (defaristid RIGHT :angle -90) (defparameter rule-1 (defrule X -> (X F X LEFT Y F LEFT Y F RIGHT F X RIGHT F X LEFT Y F LEFT Y F F X RIGHT Y F RIGHT F X F X Y F LEFT F X RIGHT Y F RIGHT F X F X RIGHT Y F LEFT F X Y F LEFT Y F LEFT F X RIGHT F X RIGHT Y F Y F LEFT))) (defparameter rule-2 (defrule Y -> (RIGHT F X F X LEFT Y F LEFT Y F RIGHT F X RIGHT F X Y F RIGHT F X LEFT Y F Y F LEFT F X LEFT Y F RIGHT F X Y F Y F LEFT F X LEFT Y F F X RIGHT F X RIGHT Y F LEFT Y F LEFT F X RIGHT F X RIGHT Y F Y))) (defparameter axiom '(LEFT Y F)) (defparameter fractal (make-fractal :name "freaky-quadratic" :rules (list rule-1 rule-2) :axiom axiom)) (draw fractal 3)