A viewer for Lindenmayer Systems written in Python / Pygame.
Lots of inspiration is taken from Fractint's L-System implementation.
An L-System is defined by three properties:
- An axiom, eg
A
- A set of replacement rules, eg
(A -> AB), (B -> A)
- An angle, eg
4
The angle is defined by an integer number of turns that make up 360 degrees, for example:
3
is equivalent to 120 degrees per turn4
is equivalent to 90 degrees per turn36
is equivalent to 10 degrees per turn
Equivalently, the angle in degrees per turn is 360 / angle
The graphics state at any point during the drawing process is defined as follows:
x
, the x position of the turtley
, the y position of the turtletheta
, an angle in degreesphi
, an angle in degreeslength
, the drawing length, in arbitrary unitsreverse
, true if an odd number of!
command have been encounteredcolour
, an index into a colour palette
F
: Draw forward using angletheta
G
: Move forward using angletheta
D
: Draw forward using anglephi
M
: Move forward using anglephi
-
: Turn left by360/angle
(subtract360/angle
fromtheta
)+
: Turn right by360/angle
(add360/angle
totheta
)[
: Save the current graphics state to the graphics stack]
: Restore the topmost graphics state from the graphics stack!
: Reverse the meaning of+
and-
, and\
and/
|
: Turn 180 degrees (or as close as possible ifangle
is odd)\{NUM}
: Turn left{NUM}
degrees relative tophi
/{NUM}
: Turn right{NUM}
degrees relative tophi
<{NUM}
: Decrement the current colour index by{NUM}
>{NUM}
: Increment the current colour index by{NUM}
C{NUM}
: Set the current colour index to{NUM}
@{NUM}
: Scalelength
by a factor of{NUM}
Where {NUM}
is a positive number in decimal, optionally preceded by
q
and/or i
, which represent the square root and inverse
(respectively) of the decimal number.
The decimal place may be omitted in the case of integers.
In the case where the decimal place is included, leading or
trailing digits may be omitted if they are zero, ie .5
and 0.5
are
equivalent, and 5.0
, 5.
and 5
are all equivalent.
q2
evaluates to approx1.414
, ie sqrt(2)i2
evaluates to0.5
, ie 1/2iq2
evaluates to approx0.707
, ie 1/sqrt(2)- This is equivalent to
qi2
, since 1/sqrt(n) === sqrt(1/n)
- This is equivalent to