Open In App

Fractal Trees in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Implementation of Fractal Binary Trees in python.

Introduction A fractal tree is known as a tree which can be created by recursively symmetrical branching.

The trunk of length 1 splits into two branches of length r, each making an angle q with the direction of the trunk. Both of these branches divides into two branches of length r*r, each making an angle q with the direction of its parent branch. Continuing in this way for infinitely many branching, the tree is the set of branches, together with their limit points, called branch tips.

So enough with the theory now let’s try to implementation in Python. To do so we require two python libraries pygame for GUI or graphical user interface and math which is a builtin library in python and will be used for mathematical tweakings.

To install pygame

pip install pygame

So how to proceed, its highly recommended that you know a bit about pygame and fractals.

First create a trunk and then start creating the branches for each trunk taking the size of the branches of the size equal to the 0.9*(length of the stem) and then again considering the branches as stem again and such repeating the process.




# Importing the python libraries
import pygame, math
  
# Initialize all imported pygame modules
pygame.init()
  
# Create a new surface and window.
surface_height, surface_width = 800, 600        #Surface variables
main_surface = pygame.display.set_mode((surface_height,surface_width))
  
# Captioning the window
pygame.display.set_caption("Fractal_Tree_geeksforgeeks")
  
def draw_tree(order, theta, sz, posn, heading, color=(0,0,0), depth=0):
  
   # The relative ratio of the trunk to the whole tree  
   trunk_ratio = 0.29     
  
   # Length of the trunk  
   trunk = sz * trunk_ratio
   delta_x = trunk * math.cos(heading)
   delta_y = trunk * math.sin(heading)
   (u, v) = posn
   newpos = (u + delta_x, v + delta_y)
   pygame.draw.line(main_surface, color, posn, newpos)
  
   if order > 0:   # Draw another layer of subtrees
  
      # These next six lines are a simple hack to make 
      # the two major halves of the recursion different 
      # colors. Fiddle here to change colors at other 
      # depths, or when depth is even, or odd, etc.
      if depth == 0:
          color1 = (255, 0, 0)
          color2 = (0, 0, 255)
      else:
          color1 = color
          color2 = color
  
      # make the recursive calls to draw the two subtrees
      newsz = sz*(1 - trunk_ratio)
      draw_tree(order-1, theta, newsz, newpos, heading-theta, color1, depth+1)
      draw_tree(order-1, theta, newsz, newpos, heading+theta, color2, depth+1)
  
  
def main():
    theta = 0
  
    while True:
  
        # Update the angle
        theta += 0.01
  
        # This little part lets us draw the stuffs 
        # in the screen everything
        main_surface.fill((255, 255, 0))
        draw_tree(9, theta, surface_height*0.9, (surface_width//2, surface_width-50), -math.pi/2)
        pygame.display.flip()
  
# Calling the main function
main()
pygame.quit()


Output:


          


Last Updated : 03 Oct, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads