Javascript required
Skip to content Skip to sidebar Skip to footer

How to Draw a Fractal Tree in Python

In this Python tutorial, we will learn about Fractal Python Turtle and we will also cover different examples related to fractal turtles. And, we will cover these topics.

  • Fractal python turtle
  • Fractal tree python turtle
  • Fractal recursion python turtle
  • Fractal drawing turtle

Fractal python turtle

In this section, we will learn about the fractal turtle in Python Turtle.

Fractal python turtle is used to make geometrical shapes with different scales and sizes. In this, it makes repeating shapes in geometry form which works on a different scale and size it is not same in shape.

Code:

In the following code, we have used the fractal python turtle which indicated to make geometrical shapes. To create this we import the turtle library.

We use speed(), penup(), pendown(), forward(), left() goto(), getscreen(), and bgcolor() functions to make this geometry shape.

  • speed() is used to give the speed at which we are creating a geometry shape.
  • penup() is used to stop the drawing.
  • pendown() is used to start the drawing.
  • goto() is used to move the turtle.
  • forward() is used to move the turtle forward.
  • left() is used to move the turtle to left direction.
          from turtle import *  import turtle     tur = turtle.Turtle()     tur.speed(6)    tur.getscreen().bgcolor("black") tur.color("cyan")  tur.penup()    tur.goto((-200, 50))     tur.pendown()     def star(turtle, size):     if size <= 10:         return     else:         for i in range(5):                           turtle.forward(size)             star(turtle, size/3)                 turtle.left(216)      star(tur, 360) turtle.done()        

Output:

In the following output, we can see the different geometry shapes and different scales that we can see in this gif, We use speed(), penup(), pendown(), forward(), left() and goto() functions to make this geometry shape.

fractal turtle python
fractal turtle python

Also, check: Python Turtle Dot

Fractal tree python turtle

In this section, we will learn about how to create a fractal tree turtle in a python turtle.

In this, we are creating a tree using python fractal we created sub-branches (Left and right) and we shorten the new sub-branches until we reach the minimum end to create a tree.

Code:

In the following code, we import the turtle module from turtle import *, import turtle for creating this fractal tree.

To create a tree we assign the x-axis and y-axis that defined the acute angle between the branch of Y.

We assigned the speed() function that helps to draw the shape in what speed a user has assigned.

  • speed() is used to define the speed of the pen to draw the shape.
  • y-axis() is used to plot a Y
  • pencolor() is used for setting color according to color level.
          from turtle import * import turtle       speed('fastest')     right(-90)     angle = 30     def yaxis(size, lvl):           if lvl > 0:         colormode(255)                            pencolor(0, 255//lvl, 0)                    forward(size)            right(angle)           yaxis(0.8 * size, lvl-1)                    pencolor(0, 255//lvl, 0)                    lt( 2 * angle )            yaxis(0.8 * size, lvl-1)                    pencolor(0, 255//lvl, 0)                    right(angle)         forward(-size)             yaxis(80, 7) turtle.done()        

Output:

After running the above code, we get the following output in which we can see the fractal tree is created with size 80 and level 7.

fractal tree python turtle
fractal tree python turtle Output

Read: Python turtle onclick

Fractal recursion python turtle

In this section, we will learn about fractal recursion in python turtle.

Recursion is the process of repeating units in a similar way fractal is used to generate an infinite amount of copies of pictures that form a fractal pattern.

Code:

In the following code, we imported a turtle library we have defined the title to a window with the name "Python Guides" assigning the bg_color and giving the screen height and width.

We define the drawline() which is drawing from pos1 to pos2 (Position is a phrase of pos) after that we assigned the recursive() which is used to generate multiple copies of the same picture.

          from turtle import * import turtle  speed = 5 bg_color = "black" pen_color = "red" screen_width = 800 screen_height = 800 drawing_width= 700 drawing_height = 700 pen_width = 5 title = "Python Guides" fractal_depth = 3   def drawline(tur, pos1, pos2):     tracing the algorithm.     tur.penup()     tur.goto(pos1[0], pos1[1])     tur.pendown()     tur.goto(pos2[0], pos2[1])   def recursivedraw(tur, x, y, width, height, count):     drawline(         tur,         [x + width * 0.25, height // 2 + y],         [x + width * 0.75, height // 2 + y],     )     drawline(         tur,         [x + width * 0.25, (height * 0.5) // 2 + y],         [x + width * 0.25, (height * 1.5) // 2 + y],     )     drawline(         tur,         [x + width * 0.75, (height * 0.5) // 2 + y],         [x + width * 0.75, (height * 1.5) // 2 + y],     )      if count <= 0:  # The base case         return     else:  # The recursive step         count -= 1                  recursivedraw(tur, x, y, width // 2, height // 2, count)               recursivedraw(tur, x + width // 2, y, width // 2, height // 2, count)                 recursivedraw(tur, x, y + width // 2, width // 2, height // 2, count)                  recursivedraw(tur, x + width // 2, y + width // 2, width // 2, height // 2, count)   if __name__ == "__main__":          screenset = turtle.Screen()     screenset.setup(screen_width, screen_height)     screenset.title(title)     screenset.bgcolor(bg_color)         artistpen = turtle.Turtle()     artistpen.hideturtle()     artistpen.pensize(pen_width)     artistpen.color(pen_color)     artistpen.speed(speed)          recursivedraw(artistpen, - drawing_width / 2, - drawing_height / 2, drawing_width, drawing_height, fractal_depth)       turtle.done()        

Output:

In the following output, we can see that how recursion is working and making the same copies of a single picture.

Fractal recursion python turtle
Fractal recursion python turtle Output

Read: Python Turtle Race

Fractal drawing turtle

In this section, we will learn about how to draw fractal drawings in python turtle.

Fractal is used to generate an infinite amount of copies of pictures that form a fractal pattern. This fractal drawing is drawn with the help of a turtle.

Code:

In the following code, we have imported the turtle library and after that, we have defined the fractdraw() later we use the left() right() forward() to give direction to the pattern.

          from turtle import * import turtle  def fractdraw(stp, rule, ang, dept, t):    if dept > 0:       x = lambda: fractdraw(stp, "a", ang, dept - 1, t)       y = lambda: fractdraw(stp, "b", ang, dept - 1, t)       left = lambda: t.left(ang)       right = lambda: t.right(ang)       forward = lambda: t.forward(stp)       if rule == "a":         left(); y(); forward(); right(); x(); forward(); x(); right(); forward(); y(); left();       if rule == "b":         right(); x(); forward(); left(); y(); forward(); y(); left(); forward(); x(); right();          turtle = turtle.Turtle() turtle.speed(0) fractdraw(5, "a", 90, 5, turtle)        

Output:

In the following output, we can see how we draw the fractal turtle and how it is working to create the same picture multiple times using the fractal pattern.

Fractal drawing turtle
Fractal drawing turtle Output

You may also like to read the following tutorials.

  • Python Turtle Tracer
  • Python Turtle Window
  • Python Turtle Triangle
  • Replit Python Turtle
  • Python Turtle Oval
  • Python Turtle Size
  • Python Turtle Mouse
  • Python Turtle Font
  • Python Turtle Get Position

Here, we will discuss Fractal Python Turtle and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Fractal python turtle
  • Fractal tree python turtle
  • Fractal recursion python turtle
  • Fractal drawing turtle

blanchonger1996.blogspot.com

Source: https://pythonguides.com/fractal-python-turtle/