I might have stumbled on some articles or blogs about Mathematics in Nature, that could have initiated this work. But more so to my frequent visits to the countryside in the past two years, I can see it's influence in my photography and here as well :D.
Imagine a tree, it has a trunk, branches, leaves and flowers. The tree that I have drawn (see Output) is having two branches from each node. I am using Python's turtle module. All the methods of turtle module can be found in the documentation. Few of the methods that I have used are below:
left
orlt
: This method is used to turn Judith's head (I called my turtle Judith) left by the required degrees. Similarly, right method is used to turn right.penup
orpu
: When this method is applied Judith's traversed path will not be captured. Similary, pendown or pd is used to reverse the effect of penup.setheading
is used to orient the turtle's head in the specified direction (direction is provided in terms of degrees measured from x axis)xcor
,ycor
methods are used to get x,y co-ordinates of turtle's current location.setx
,sety
methods are used to move Judith to the desired location.
Let's get back to the code:
- Firstly, I created a drawing board for my turtle.
- Then I drew a trunk. Now, trunk's end point should be remembered (see illustrated image below) because from here I need to draw two branches. I have defined two functions
- fractalY1: This will draw the right branch from a node and return the branch's end point co-ordinates. This end point is a new node.
- fractalY2: This will draw the left branch from a node and return the branch's end point co-ordinates. This end point is a new node.
- The input argument
distance
governs the length of each branch.
- All these nodes' x,y co-ordinates are stored in the list named
x
- Finally, I created a
for
loop and in it, I am calling these functions. I usedif-elif-else
block to create the look and feel of trunk, branches and leaves (branches length progressively decreased).
A new function that draws a flower can be defined and it can be used to draw flowers at the end nodes.