-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.py
230 lines (199 loc) · 11.9 KB
/
Tree.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import pygame
import pygame_tools
import fractal_tools
import random
import math
class Tree:
def __init__(self, screen, color):
self.screen = screen
self.color = color
def perfect_branch(self, center, length, thickness, start_theta, theta): #the start theta defines the current planes rotation
length = length * 0.4
thickness = thickness * 0.9
if(length > 2):
right_theta = start_theta - theta
left_theta = start_theta + theta
#draw left and right branch
right_point = (center[0] + length * math.cos(right_theta), center[1] - length * math.sin(right_theta))
left_point = (center[0] + length * math.cos(left_theta), center[1] - length * math.sin(left_theta))
pygame.draw.line(self.screen, self.color, center, right_point, int(thickness))
pygame.draw.line(self.screen, self.color, center, left_point, int(thickness))
#continue branch left and right
self.perfect_branch(right_point, length, thickness, right_theta, theta)
self.perfect_branch(left_point, length, thickness, left_theta, theta)
def random_branch(self, center, olength, thickness, start_theta): #the start theta defines the current planes rotation
thickness = float(thickness * 0.8)
num_branch = random.randrange(0,5)
if(num_branch > 0):
interval = float(olength/(num_branch))
else:
interval = 0
spot = interval
#make the top branch off many times
for i in range(random.randrange(1,3)):
theta = (float(random.randrange(50,100))/100 * math.pi/4)
if(olength > 2): #stop branching
length = olength * float(random.randrange(50, 70)) / 100
random_bin = random.randrange(0, 2)
if(random_bin == 1):
new_theta = start_theta - theta
else:
new_theta = start_theta + theta
#draw branch
new_point = (center[0] + length * math.cos(new_theta), center[1] - length * math.sin(new_theta))
if(length > 30):
array = [0 for i in range(30)]
fractal_tools.random_koch(0, 29, 5, 0.5, array)
pygame_tools.draw_lines(array, self.screen, self.color, center, new_point, thickness)
else:
pygame.draw.line(self.screen, self.color, center, new_point, int(thickness))
#continue branch left and right
self.random_branch(new_point, length, thickness, new_theta)
for i in range(num_branch - 1): #randomize the number of branches
theta = (float(random.randrange(50,100))/100 * math.pi/4)
if(olength > 2): #stop branching
current_center = (center[0] - spot*math.cos(start_theta), center[1] + spot*math.sin(start_theta))
spot = spot + interval
length = olength * float(random.randrange(50, 70)) / 100
random_bin = random.randrange(0, 2)
if(random_bin == 1):
new_theta = start_theta - theta
else:
new_theta = start_theta + theta
#draw branch
new_point = (current_center[0] + length * math.cos(new_theta), current_center[1] - length * math.sin(new_theta))
if(length > 30):
array = [0 for i in range(30)]
fractal_tools.random_koch(0, 29, 5, 0.5, array)
pygame_tools.draw_lines(array, self.screen, self.color, current_center, new_point, thickness)
else:
pygame.draw.line(self.screen, self.color, current_center, new_point, int(thickness))
#continue branch left and right
self.random_branch(new_point, length, thickness, new_theta)
def random_flower(self, center, olength, thickness, start_theta, color): #the start theta defines the current planes rotation
thickness = float(thickness * 0.8)
num_branch = random.randrange(0,5)
if(num_branch > 0):
interval = float(olength/(num_branch))
else:
interval = 0
spot =interval
#make the top branch off many times
for i in range(random.randrange(1,3)):
theta = (float(random.randrange(50,100))/100 * math.pi/4)
if(olength > 2): #stop branching
length = olength * float(random.randrange(50, 70)) / 100
random_bin = random.randrange(0, 2)
if(random_bin == 1):
new_theta = start_theta - theta
else:
new_theta = start_theta + theta
#draw branch
new_point = (center[0] + length * math.cos(new_theta), center[1] - length * math.sin(new_theta))
if(length > 30):
array = [0 for i in range(30)]
fractal_tools.random_koch(0, 29, 5, 0.5, array)
pygame_tools.draw_lines(array, self.screen, self.color, center, new_point, thickness)
else:
pygame.draw.line(self.screen, self.color, center, new_point, int(thickness))
#continue branch left and right
self.random_flower(new_point, length, thickness, new_theta, color)
for i in range(num_branch - 1): #randomize the number of branches
theta = (float(random.randrange(50,100))/100 * math.pi/4)
if(olength < 2): #stop branching, flower
self.draw_flower(center, 4, theta, color)
else:
current_center = (center[0] - spot*math.cos(start_theta), center[1] + spot*math.sin(start_theta))
spot = spot + interval
length = olength * float(random.randrange(50, 70)) / 100
random_bin = random.randrange(0, 2)
if(random_bin == 1):
new_theta = start_theta - theta
else:
new_theta = start_theta + theta
#draw branch
new_point = (current_center[0] + length * math.cos(new_theta), current_center[1] - length * math.sin(new_theta))
if(length > 30):
array = [0 for i in range(30)]
fractal_tools.random_koch(0, 29, 5, 0.5, array)
pygame_tools.draw_lines(array, self.screen, self.color, current_center, new_point, thickness)
else:
pygame.draw.line(self.screen, self.color, current_center, new_point, int(thickness))
#continue branch left and right
self.random_flower(new_point, length, thickness, new_theta, color)
def pine_tree(self, center, olength, thickness, start_theta, color):
thickness = 1
num_branch = random.randrange(8, 10)
interval = float(olength/(num_branch))
spot = 0
current_center = center
theta = ( math.pi/6)
new_theta = start_theta - theta
for i in range(1, num_branch + 1):
length = olength * float(i)/num_branch * 0.4
if(length > 2):
current_center = (center[0] - spot*math.cos(start_theta), center[1] + spot*math.sin(start_theta))
spot = spot + interval
#draw branch
new_point = (current_center[0] + (length * math.cos(new_theta)), current_center[1] - (length * math.sin(new_theta)))
opposite_new_point = (current_center[0] + (length * math.cos(start_theta + theta)), current_center[1] - (length * math.sin(start_theta + theta)))
if(length < 4):
pygame.draw.line(self.screen, color, current_center, new_point, int(thickness))
pygame.draw.line(self.screen, color, current_center, opposite_new_point, int(thickness))
else:
pygame.draw.line(self.screen, self.color, current_center, new_point, int(thickness))
pygame.draw.line(self.screen, self.color, current_center, opposite_new_point, int(thickness))
#continue branch left and right
self.pine_tree(new_point, length, thickness, new_theta, color)
self.pine_tree(opposite_new_point, length, thickness, start_theta + theta, color)
def draw(self, center, length, thickness, theta, tree_type, color):
#first branch is always straight up (trunk)
if(theta == 0): #randomize tree
trunk_length = length * float(random.randrange(40, 100)) / 100
if(tree_type != 2):
two_variation = (float(random.randrange(70, 120))/100 * 2)
branch_trunk = float((trunk_length)/ two_variation) #branches along top half of trunk
branchless_trunk = trunk_length - branch_trunk
num_branch = random.randrange(2,6)
increment = float(branch_trunk) / num_branch #place branches along trunk on this increment
for i in range(num_branch):
#50% to 100% of placement
percent = float(random.randrange(50, 100)) / 100
placement = branchless_trunk + (increment * i * percent)
#place random branching along spot on trunk
branch_center = (center[0], center[1] - placement)
if(tree_type == 0):
self.random_branch(branch_center,placement * 0.7, thickness, (math.pi/2))
else:
self.random_flower(branch_center,placement * 0.7, thickness, (math.pi/2), color)
#top of the tree
new_center = (center[0], center[1] - trunk_length)
#draw a rickety trunk
array = [0 for i in range(30)]
fractal_tools.random_koch(0, 29, 5, 0.5, array)
pygame_tools.draw_lines(array, self.screen, self.color, center, new_center, thickness)
if(tree_type == 0): #basic
self.random_branch(new_center, branch_trunk, thickness, (math.pi/2))
elif(tree_type == 1): #flowering
self.random_flower(new_center, branch_trunk, thickness, (math.pi/2), color)
else: #pine
self.pine_tree(new_center, trunk_length, thickness, (math.pi/2), color)
else: #perfect tree
new_center = (center[0], center[1] - length)
pygame.draw.line(self.screen, self.color, center, new_center, thickness)
self.perfect_branch(new_center, length, thickness, (math.pi/2), theta)
def draw_flower(self, center, length, angle, color):
ll = float(length/2)
sl = float(length/8)
part1 = [0 for i in range(4)]
part2 = [0 for i in range(4)]
part1[0] =(center[0] + ll*math.cos(angle), center[1] + ll*math.sin(angle))
part1[1] =(center[0] + sl*math.cos(angle + math.pi/2), center[1] +sl*math.sin(angle + math.pi/2))
part1[2] = (center[0] + ll*math.cos(angle + math.pi), center[1] + ll*math.sin(angle + math.pi))
part1[3] = (center[0] + sl*math.cos(angle + math.pi*3/2), center[1] + sl*math.sin(angle + math.pi*3/2))
part2[0] = (center[0] + sl*math.cos(angle), center[1] + sl*math.sin(angle))
part2[1] = (center[0] + ll*math.cos(angle + math.pi/2), center[1] + ll*math.sin(angle + math.pi/2))
part2[2] = (center[0] + sl*math.cos(angle + math.pi), center[1] + sl*math.sin(angle + math.pi))
part2[3] = (center[0] + ll*math.cos(angle + math.pi*3/2), center[1] + ll*math.sin(angle + math.pi*3/2))
pygame.draw.polygon(self.screen, color, part1)
pygame.draw.polygon(self.screen, color, part2)