-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractalTree.py
More file actions
55 lines (43 loc) · 1.57 KB
/
FractalTree.py
File metadata and controls
55 lines (43 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pygame, math, pygame.gfxdraw
# Some constants
surface_height = 1000
surface_width = 1600
surface_background = (53, 70, 92)
color = (127, 138, 152)
scale_ratio = 0.29
default_theta = .30 # 60 degree rotations in radians
default_recursion_depth = 15
# Draws the fractal tree. order = drawings left, depth = drawings completed
def draw_tree( order, theta, size, position, orientation, depth = 0 ) :
# Scale the size of the branch
branch_size = size * scale_ratio
# Rotate the branch
dx = branch_size * math.cos( orientation )
dy = branch_size * math.sin( orientation )
# Calculate drawing coordinates
(x, y) = position
end_position = (x + dx, y + dy)
# Draw the branch
pygame.draw.line(drawing_surface, color, position, end_position)
# If we haven't reached the bottom yet...
if order > 0 :
# Recursively draw two new sub trees
next_size = size * (1 - scale_ratio)
draw_tree( order - 1, theta, next_size, end_position, orientation - theta, depth + 1 )
draw_tree( order - 1, theta, next_size, end_position, orientation + theta, depth + 1 )
# Draw the fractal and wait for user input to quit
def run( ) :
drawing_surface.fill( surface_background )
draw_tree( default_recursion_depth, default_theta, surface_height * 0.9, (surface_width // 2, surface_height - 50), -math.pi / 2 )
pygame.display.flip( )
while True :
# Grab user input to quit
ev = pygame.event.poll( )
if ev.type == pygame.QUIT :
break
# Create the drawing surface
drawing_surface = pygame.display.set_mode( (surface_width, surface_height) )
# PyGame Life Cycle
pygame.init( )
run( )
pygame.quit( )