Enonce
Enonce
Instructions for submitting : You will submit the two files listed above on the Moodle
platform. These will not be in an archive. Deadline is Tuesday, April 22th , 23h59.
Spé - EPITA 1
NTS S4 2025
A derivation of a string is applying the production rules to this string. It consists in replacing
every symbol in V by the content of the associated production rule.
Example 2 Let us consider the following L-system :
— V = {F, X}
— Σ = {[, ], +, −}
— P = {(F, F F ), (X, F [+X][−X]F X)}
— w=X
The axiom contains only one symbol, X. The first derivation simply replaces X by F [+X][−X]F X.
In a second derivation, we read this string and replace every X by F [+X][−X]F X and every F
by F F . Thus, after two derivations, we obtain the string
F F [+F [+X][−X]F X][−F [+X][−X]F X]F F F [+X][−X]F X
Examples of L-systems are given in the files lsystems/lsystem2d.py and lsystems/lsystem3d.py.
They also contain other parameters that will be necessary for the drawings.
2.2 Implementation
In our implementation, strings will be strings. Production rules are given by a dictionary. A way
to know if a symbol we meet is a variable is by checking if it is in the keys of the dictionary.
W1 ♣ In the file lsystem.py, write the function derivation that applies one derivation on
the input axiom according to the production rules given by the input rules.
W2 ♣ In the same file, write the function generation that takes as input an axiom, a set of
rules and the number of times the derivation must be done.
derivation('X', {'F':'FF', 'X':'F[+X][-X]FX'})
>> 'F[+X][-X]FX'
3 Drawing in 2D
3.1 Mathematical tools
In order to draw, we will need functions that perform transformations of the plan (and later of
the space). First, standard libraries provide us useful trigonometric functions, but their input as
to be given in radians and our measures are in degree.
W3 ♣ In the file mathsutils.py, write the function degreetorad that takes as input an angle
in degrees and returns its value in radians.
Then, our transformations are modeled by matrices and vectors. We need basic tools to handle
these.
W4 ♣ In this same file, write the function multmatvector that takes as input a matrix and a
vector and returns their product.
Spé - EPITA 2
NTS S4 2025
W5 ♣ In this same file, write the function multmat that takes as input two matrices and
returns their product.
degreetorad(40)
>> 0.6981317007977318
multmat([[1, 2], [3, -1], [0, 2]], [[-1, 1], [0, 1]])
>> [[-1, 3], [-3, 2], [0, 2]]
For the drawing, we will use the turtle system 1 . To do that, we store the orientation of the turtle
as a rotation matrix. Given an angle θ, the rotation matrix is given by
cos(θ) − sin(θ)
sin(θ) cos(θ)
W6 ♣ In this same file, write the function r2d that takes as input an angle in degrees and
returns the corresponding matrix. (remember, radians !)
r2d(30)
>> [[0.8660254037844387, -0.49999999999999994],
[0.49999999999999994, 0.8660254037844387]]
(Floatings...)
Now we need to be able to move forward by a distance d from an initial position (x, y) with an
angle given by the rotation matrix R. This is given by the formula
′
x x d
= +R
y′ y 0
W7 ♣ In this same file, write the function move2d that takes as inputs the position, the rotation
matrix and the distance, and returns the new coordinates.
move2d(0, 5, r2d(30), 3)
>> (2.598076211353316, 6.5)
Spé - EPITA 3
NTS S4 2025
W8 In the file lsystem.py, write the function axiomtoline2d that takes as input a string, a
couple of floats representing the initial position, and an lsystem (object of the class LSystem2d).
This function must return a list of Line (class Line) whose constructor takes as input two
couples of floats (the coordinates). Everytime the turtle moves forward, a line is created between
its starting position and its ending position.
Finally, once this has been done, you can launch the command python 2dgen.py and admire
(or not ?) the result. Notice that some well known fractals have been included, so you can know
if your function is correct.
4 Drawing in 3D
This section is similar to the previous one, but the objective is to draw our fractals in 3D.
W10 ♣ In this same file, write the function move3d that takes as inputs the position, the
rotation matrix and the distance, and returns the new coordinates.
Spé - EPITA 4
NTS S4 2025
W11 In the file lsystem.py, write the function axiomtoline3d that takes as input a string, a
couple of floats representing the initial position, and an lsystem (object of the class LSystem3d).
Now the coordinates in the line will be triplets (as we are in 3d). Moreover, each time a line is
created, the current depth will be stored alongside.
This function must return a list of couples, where the first part of the couple is a line, and the
second is its depth.
Finally, once this has been done, you can launch the command python 3dgen.py and admire
the result.
4.3 To go further
In nature, plants are not always the same. Create the function axiomtoline3drand that is similar
to axiomtoline3d but adds some randomness in the creation of the lines. It may be the angles,
the lengths of the lines... Be creative ! (This function will not be evaluated).
Spé - EPITA 5