Y Fractal Tree in Python using turtle


What is going on everybody? And welcome to another tutorial on python. Today we are going to look at turtle. So a turtle is a module in python that helps
us draw lines and shapes. It’s not super crazy and fancy but some simple
stuff for helping beginners learn faster. And by the end of the video hopefully we will be able
to create a fractal tree by using a turtle. For creating a fractal tree you should have
general knowledge in recursion. So let’s begin. First of all before using turtle we must first
import turtle in our program. So import tur-tle as tu. So how do we draw shapes using turtle. For that we must first create an object turtle. As you can see this will create a canvas for
us. And this arrow right here at the middle of
the canvas is our turtle. So I should probably explain what I did. ‘foo’ is just a variable. You can put anything of your choice in here. Then to say python we want to create a turtle
named ‘foo’ we must reference it by using tu.Turtle*. Now to make foo move forward I can use – 100
pixels. And I can make foo rotate left by using left(). And move it – make it move backward by using
backward. Distance will be in the unit of pixels. Angle will be in the unit of degree. ‘foo’ can also rotate on right. Let me reset ‘foo’. As you see when we first create a* turtle
it will be facing on right direction. We want our turtle to face upward. So first of all we must do foo.left() in 90
degree. If I make foo move forward by 100 deg – pixels*
then it will be there. Let’s rotate ‘foo’ by 30 degree. And make it move forward by 50. Half of the initial distance. And make it move back by the same distance. And make foo rotate right in 60 degree. And make foo move forward 50 and backward
50. And let’s reset the angle of ‘foo’. dot-left()-30. So a fractal tree is going to be a repetitive
version of this. So let’s begin creating a fractal tree. First of all open a new file. Then import turtle tu. Then create an* object turtle. Now to make ‘foo’ face in the up direction
– left() by 90 degree. I can change the speed of ‘foo’ using speed(). I am going to use 10 here. Now the hard part begins. I am going to use recursion, for creating
a fractal tree. So define a function draw() that takes length
as argument. And same thing as I did previously. First of all make ‘foo’ move forward by l. Then rotate ‘foo’ on the left by 30. Then draw a fractal on the left of ‘foo’,
with half of it’s length. Rotate ‘foo’ on the right by 60. Because I want them to be 60 degree apart. And Draw another fractal tree on the right. And then I want to reset the angle of ‘foo’. So foo-dot-left()-30. Now foo will be facing forward. I want it to move to it’s initial position. So foo-dot-forward by l. In ideal world a fractal is going to repeat
itself infinitely. But we don’t infinite resource and time. So, I’m going to limit my program by a factor
of length. If length is less than – less than – say 1. Then return. Else do the following part. Now I must call draw() from outside the function
to actually execute this. I’m going to provide 100 as parameter. Hopefully this should work. So let’s throw 10 instead of 1 because it’s
going to take a lot of time if I use 1. So it I run this. So this is a fractal tree. As you can see, the smaller part over here
is repetitive version of the whole part. I can do some crazy thing by changing the
scaling factor in the draw() here. So if I do something like 3 times l divide
by 4 and same thing over here. And if I run this. This will go in more depth and detail. So let’s see execute fully. I am going to maximize it. This is going to take a lot of time. This is our final fractal tree. So this is it. I hope you found it helpful and learned something
new. Do not forget to like share and comment. And subscribe to my channel.

12 thoughts on “Y Fractal Tree in Python using turtle

  1. Just subscribed. I am interested in PHYLOGENETIC TREES IN BIOLOGY. Could you try to make a program that takes data from a table about Species: which kingdom, phylum, class, order, family, genus it is and draws a phylogenetic tree, for instance the tree for the class Birds, with its orders and so on…?

Leave a Reply

Your email address will not be published. Required fields are marked *