Lets create the factorial tree with a turtle library.
Making the factorial tree allows us to demonstrate how this library is drawing amazing images in an easy way.
Preparation
Creating the project.
Adding dependency in cargo.toml.
Coding in src/main.rs
We need to import the turtle crate.
Lets create the turtle in the main function.
Our tree is a fractal which consists of 1 large square and 2 small squares.
Every small square represents itself the large square of the next level.
We can use the recursion. Lets create the draw function.
This function accepts 3 parameters:
the turtle object which was created in the main function;
the size of the first large square's side;
the depth of recursion (it's a size of the tree).
We need to check the actual depth for drawing smaller squares. Then we can move to the left upper corner of every small square and call the draw function.
Now we need to change our main function.
But as our tree is made in the black color we can make it look more attractive and change the color of our tree by adding a new function.
Now we need to change the code which drawing the large square.