Árboles fractales en Python

Implementación de Fractal Binary Trees en python.

Introducción Un árbol fractal se conoce como un árbol que se puede crear mediante una ramificación recursivamente simétrica.

El tronco de longitud 1 se divide en dos ramas de longitud r, cada una formando un ángulo q con la dirección del tronco. Ambas ramas se dividen en dos ramas de longitud r*r, cada una formando un ángulo q con la dirección de su rama principal. Continuando así por infinitas ramificaciones, el árbol es el conjunto de ramas, junto con sus puntos límite, llamados puntas de las ramas.

Entonces, suficiente con la teoría, ahora intentemos implementarlo en Python. Para hacerlo, necesitamos dos bibliotecas de python, pygame para GUI o interfaz gráfica de usuario y matemáticas, que es una biblioteca integrada en python y se usará para ajustes matemáticos.

Para instalar pygame

pip install pygame

Entonces, cómo proceder, es muy recomendable que sepas un poco sobre pygame y fractales.

Primero cree un tronco y luego comience a crear las ramas para cada tronco tomando el tamaño de las ramas del tamaño igual a 0.9 * (longitud del tallo) y luego nuevamente considerando las ramas como tallo nuevamente y repitiendo el proceso.

# 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()

Producción:


          

Publicación traducida automáticamente

Artículo escrito por Subhajit Saha y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *