Javatpoint Logo
Javatpoint Logo

Drawing Y Fractal tree in Turtle - Python

Python comes with a built-in module called Turtle that functions like a virtual canvas on which we may create beautiful images and forms. We can draw on the screen with the provided on-screen pen.

The turtle library's main objective is to acquaint kids with programming. New programmers can learn how to use Python to program in a fun and interactive way with the aid of Turtle's library.

Because it allows for the creation of distinctive shapes, eye-catching images, and a variety of activities, it is advantageous to both children and seasoned programmers. The animation and minigames can also be created by us. We shall learn about the various functions of the turtle library in the next section.

A fractal is a pattern that never ends. Fractals are infinitely intricate patterns that resemble one another at all scales. They are made by repeatedly performing a straightforward technique in a never-ending feedback loop. Fractals, which are driven by recursion, are representations of chaotic dynamical systems.

In this tutorial, we'll create a vibrant Y fractal tree.

Y Fractal tree in Turtle - Python :

The creation of a fractal tree turtle in Python will be covered in this part.

Here, we are using Python Fractal to make a tree. To do this, we built new sub-branches (Left and Right) and shortened them till we reached the minimum end.

Approach :

  1. To begin, we create a solitary "Y" form for the root (level 1) tree. Then the bases of the other two "Y"s are formed by both of the "Y's" branches (level 2).
  2. The size of the Y reduces as level rises and is repeated repeatedly.
  3. The tree is colored in a level-by-level fashion, from darkest at the base to lightest at the top.

Snippets :

The turtle pen can be made after importing the turtle library for making this Y fractal tree.

The speed of drawing is set to fast using the speed() method that assists in drawing the shape at the speed the user has set and the Y fractal tree is turned upwards.

We provided the x-axis and y-axis that determined an acute angle between the Y branch in order to produce a tree.

Then we used the pencolor() method to define the rgb color range for each level of the tree. We also used the forward(), left() and right() methods to advance the turtle for drawing the Y fractal tree.

Then we made a recursive call for drawing the right subtree first and mentioned its length and level to the y() method. Furthermore, we used the left() method to move left and change the angle.

Then we made a recursive call for drawing the left subtree first and mentioned its length and level to the y() method. Furthermore, we used the right() method to move left and change the angle and then the forward() method to advance.

At last, we gave the dimensions, that is the length and the number of levels of the Y fractal tree.

Complete Code :


Drawing Y Fractal tree in Turtle - Python

Explanation :

The output of the code, that results from running the aforementioned code shows that a Y fractal tree of length 40 and level 8 was generated by the help of various methods imported from the turtle Python library.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA