Python中的分形树
Python形二叉树的实现。
简介分形树被称为可以通过递归对称分支创建的树。
长度为 1 的树干分成两个长度为 r 的分支,每个分支都与树干的方向成 q 角。这两个分支都分成两个长度为 r*r 的分支,每个分支与其父分支的方向成角度 q。以这种方式继续无限多分支,树是分支的集合,连同它们的极限点,称为分支尖端。
理论到此为止,现在让我们尝试在Python中实现。为此,我们需要两个用于 GUI 或图形用户界面的Python库pygame和数学,这是Python中的内置库,将用于数学调整。
安装 pygame
pip install pygame
那么如何继续,强烈建议您对 pygame 和分形有所了解。
首先创建一个树干,然后开始为每个树干创建分支,其大小等于 0.9*(茎的长度),然后再次将分支视为茎,并重复该过程。
# 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()
输出:
https://media.geeksforgeeks.org/wp-content/uploads/2017-12-18-at-18-40-42.mp4