# The cl-aristid Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

# The cl-aristid Reference Manual

This is the cl-aristid Reference Manual, version 0.0.2, generated automatically by Declt version 3.0 "Montgomery Scott" on Mon Apr 19 14:30:36 2021 GMT+0.

Next: , Previous: , Up: Top   [Contents][Index]

# cl-aristid

Draw Lindenmayer Systems with Common LISP!

## 1. Introduction

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 `symbol` is 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 `.svg`

## 2. Example

Go to the examples folder to look at the code for several fractals and see them in action by running `make`!

Drawing the Dragon Curve with `cl-aristid`

1. First, we want to enter SBCL (just write `sbcl` in the terminal, inside the root of the repository) and load this package
``````(ql:update-dist "quicklisp")
``````
1. After the call to `use-package` we now have access to the symbols exposed by the package, detailed in Interface.
``````(use-package 'cl-aristid)
``````
1. `defaristid`

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 `+` and `-` symbols, we will replace them with `RIGHT` and `LEFT`

``````(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
``````
1. `defrule`

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` with `X + Y F +` and the same happens to `Y`.

``````; 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))))
``````
1. `make-fractal`

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 `(F X)`

We want to call `make-fractal` with all of this attributes.

``````(defparameter dragon (make-fractal :name "dragon"
:rules dragon-rules
:axiom '(F X)))
``````
1. `draw`

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)
``````
1. Result
1. Let's pass it through vivus for one last look

## 3. Interface

``````defrule symbol -> (newsymbol newsymbol2) :prob 1.00
``````

The macro `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"
``````

The macro `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 `angle` and `len` are specified, the drawing will first draw forward, and then change the angle
``````make-fractal :name name :axiom axiom :rules rules
``````

The function `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
``````

The function `draw` takes a `fractal` structure and draws it's `gen` iteration, saving it in a `.svg` named `fractalname_gen.svg`. The function also has an optional `background` parameter, to change the background color of the drawing.

## 4. Cool stuff

### Brackets

The bracket symbols (`[` and `]`) 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 `cl-aristid` with `(defrule X -> (F RIGHT [ [ X ] LEFT X ] LEFT F [ LEFT F X ] RIGHT X))`

### Stochastic L-systems

`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)`

### Colors

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")`

### Everything together

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")
``````

### Rainbows

Add `: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))

:rules (list rule-1 rule-2)
:axiom axiom))

(draw fractal 3)
``````

Next: , Previous: , Up: Top   [Contents][Index]

## 2 Systems

The main system appears first, followed by any subsystem dependency.

Previous: , Up: Systems   [Contents][Index]

### 2.1 cl-aristid

Author

FdelMazo, JDSanto, camidvorkin, anitasec

MIT

Description

Draw Lindenmayer Systems with Common LISP!

Version

0.0.2

Dependencies
• cl-svg
• cl-colors
Source

cl-aristid.asd (file)

Components

Next: , Previous: , Up: Top   [Contents][Index]

## 3 Files

Files are sorted by type and then listed depth-first from the systems components trees.

Previous: , Up: Files   [Contents][Index]

### 3.1 Lisp

Next: , Previous: , Up: Lisp files   [Contents][Index]

#### 3.1.1 cl-aristid.asd

Location

cl-aristid.asd

Systems

cl-aristid (system)

Next: , Previous: , Up: Lisp files   [Contents][Index]

#### 3.1.2 cl-aristid/package.lisp

Parent

cl-aristid (system)

Location

package.lisp

Packages

Next: , Previous: , Up: Lisp files   [Contents][Index]

#### 3.1.3 cl-aristid/cl-aristid.lisp

Dependency

package.lisp (file)

Parent

cl-aristid (system)

Location

cl-aristid.lisp

Exported Definitions
Internal Definitions

Next: , Previous: , Up: Lisp files   [Contents][Index]

#### 3.1.4 cl-aristid/canvas.lisp

Dependency

cl-aristid.lisp (file)

Parent

cl-aristid (system)

Location

canvas.lisp

Internal Definitions

Previous: , Up: Lisp files   [Contents][Index]

#### 3.1.5 cl-aristid/fractal.lisp

Dependency

canvas.lisp (file)

Parent

cl-aristid (system)

Location

fractal.lisp

Exported Definitions

make-fractal (function)

Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

## 4 Packages

Packages are listed by definition order.

Previous: , Up: Packages   [Contents][Index]

### 4.1 cl-aristid

Source

package.lisp (file)

Use List

common-lisp

Exported Definitions
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

## 5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.

Next: , Previous: , Up: Definitions   [Contents][Index]

### 5.1 Exported definitions

Next: , Previous: , Up: Exported definitions   [Contents][Index]

#### 5.1.1 Macros

Macro: defaristid NAME &rest BODY
Package
Source

cl-aristid.lisp (file)

Macro: defrule SYM -> REPLACE &key PROB
Package
Source

cl-aristid.lisp (file)

Previous: , Up: Exported definitions   [Contents][Index]

#### 5.1.2 Functions

Function: [ CANVAS
Package
Source

cl-aristid.lisp (file)

Function: ] CANVAS
Package
Source

cl-aristid.lisp (file)

Function: draw FRACTAL GEN &key BACKGROUND
Package
Source

cl-aristid.lisp (file)

Function: make-fractal &key (NAME NAME) (AXIOM AXIOM) (RULES RULES)
Package
Source

fractal.lisp (file)

Previous: , Up: Definitions   [Contents][Index]

### 5.2 Internal definitions

Next: , Previous: , Up: Internal definitions   [Contents][Index]

#### 5.2.1 Functions

Function: %make-canvas &key (MATRIX MATRIX) (DIR DIR) (POINT POINT) (PREV-POINT PREV-POINT) (CORNERS CORNERS) (STACK STACK)
Package
Source

canvas.lisp (file)

Function: -> OLD NEW &optional PROB
Package
Source

cl-aristid.lisp (file)

Function: apply-commands CANVAS COMMAND-ARR
Package
Source

fractal.lisp (file)

Function: aristid &key ANGLE LEN NODRAW COLOR
Package
Source

cl-aristid.lisp (file)

Function: canvas-corners INSTANCE
Function: (setf canvas-corners) VALUE INSTANCE
Package
Source

canvas.lisp (file)

Function: canvas-dir INSTANCE
Function: (setf canvas-dir) VALUE INSTANCE
Package
Source

canvas.lisp (file)

Function: canvas-matrix INSTANCE
Function: (setf canvas-matrix) VALUE INSTANCE
Package
Source

canvas.lisp (file)

Function: canvas-move CANVAS
Package
Source

canvas.lisp (file)

Function: canvas-p OBJECT
Package
Source

canvas.lisp (file)

Function: canvas-point INSTANCE
Function: (setf canvas-point) VALUE INSTANCE
Package
Source

canvas.lisp (file)

Function: canvas-prev-point INSTANCE
Function: (setf canvas-prev-point) VALUE INSTANCE
Package
Source

canvas.lisp (file)

Function: canvas-stack INSTANCE
Function: (setf canvas-stack) VALUE INSTANCE
Package
Source

canvas.lisp (file)

Function: commands N STR RULES
Package
Source

fractal.lisp (file)

Function: copy-canvas INSTANCE
Package
Source

canvas.lisp (file)

Function: copy-fractal INSTANCE
Package
Source

fractal.lisp (file)

Function: create-svg CANVAS BG
Package
Source

canvas.lisp (file)

Function: d2r DEGREES
Package
Source

canvas.lisp (file)

Function: draw-fractal FRACTAL GEN BACKGROUND
Package
Source

fractal.lisp (file)

Function: draw-point CANVAS &optional COLOR
Package
Source

canvas.lisp (file)

Function: flatten TREE
Package
Source

fractal.lisp (file)

Function: fractal-axiom INSTANCE
Function: (setf fractal-axiom) VALUE INSTANCE
Package
Source

fractal.lisp (file)

Function: fractal-name INSTANCE
Function: (setf fractal-name) VALUE INSTANCE
Package
Source

fractal.lisp (file)

Function: fractal-p OBJECT
Package
Source

fractal.lisp (file)

Function: fractal-rules INSTANCE
Function: (setf fractal-rules) VALUE INSTANCE
Package
Source

fractal.lisp (file)

Function: get-next-color ()
Package
Source

canvas.lisp (file)

Function: make-canvas ()
Package
Source

canvas.lisp (file)

Function: pop-stack CANVAS
Package
Source

canvas.lisp (file)

Function: push-stack CANVAS
Package
Source

canvas.lisp (file)

Function: random-increase COLOR
Package
Source

canvas.lisp (file)

Function: rotate-dir DIR ANGLE
Package
Source

canvas.lisp (file)

Function: string-rewrite STR RULES
Package
Source

fractal.lisp (file)

Package
Source

canvas.lisp (file)

Function: turn-angle CANVAS ANGLE
Package
Source

canvas.lisp (file)

Previous: , Up: Internal definitions   [Contents][Index]

#### 5.2.2 Structures

Structure: canvas ()
Package
Source

canvas.lisp (file)

Direct superclasses

structure-object (structure)

Direct slots
Slot: matrix

canvas-matrix (function)

Writers

(setf canvas-matrix) (function)

Slot: dir

canvas-dir (function)

Writers

(setf canvas-dir) (function)

Slot: point

canvas-point (function)

Writers

(setf canvas-point) (function)

Slot: prev-point

canvas-prev-point (function)

Writers

(setf canvas-prev-point) (function)

Slot: corners

canvas-corners (function)

Writers

(setf canvas-corners) (function)

Slot: stack

canvas-stack (function)

Writers

(setf canvas-stack) (function)

Structure: fractal ()
Package
Source

fractal.lisp (file)

Direct superclasses

structure-object (structure)

Direct slots
Slot: name

fractal-name (function)

Writers

(setf fractal-name) (function)

Slot: axiom

fractal-axiom (function)

Writers

(setf fractal-axiom) (function)

Slot: rules

fractal-rules (function)

Writers

(setf fractal-rules) (function)

Previous: , Up: Top   [Contents][Index]

## Appendix A Indexes

Next: , Previous: , Up: Indexes   [Contents][Index]

### A.1 Concepts

Next: , Previous: , Up: Indexes   [Contents][Index]

### A.2 Functions

Jump to: %   (   -   [   ]   A   C   D   F   G   M   P   R   S   T
Jump to: %   (   -   [   ]   A   C   D   F   G   M   P   R   S   T

Next: , Previous: , Up: Indexes   [Contents][Index]