-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_fractal_trees.py
85 lines (63 loc) · 2.27 KB
/
object_fractal_trees.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
import turtle
import math
ang=math.radians(float(input("Enter the branch angle: ")))
ratio=float(input("Enter the shrinking ratio: "))
level=int(input("Enter the amount of levels: "))
def draw_line(x1, y1, x2, y2):
myTurtle.penup()
myTurtle.goto(x1, y1)
myTurtle.pendown()
myTurtle.goto(x2, y2)
myTurtle.penup()
def distance(x1, y1, x2, y2):
return math.sqrt(pow((x1-x2), 2)+pow((y1-y2), 2))
class Branch:
def __init__(self, _start_x, _start_y, _end_x, _end_y, _angle_r, _angle_l):
self.start_x=_start_x
self.start_y = _start_y
self.end_x= _end_x
self.end_y = _end_y
self.spawn=None
self.angle_r=_angle_r
self.angle_l = _angle_l
def __repr__(self):
return "["+str((self.start_x, self.start_y)) + ", " + str((self.end_x, self.end_y))+"]"
def dist(self):
return distance(self.start_x, self.start_y, self.end_x, self.end_y)*ratio
def draw(self):
draw_line(self.start_x, self.start_y, self.end_x, self.end_y)
def branch_right(self):
return Branch(self.end_x, self.end_y,
self.end_x + self.dist() * math.cos(self.angle_r),
self.end_y + self.dist() * math.sin(self.angle_r),
self.angle_r-(math.radians(90)-ang), self.angle_l+(math.radians(90)-ang))
def branch_left(self):
return Branch(self.end_x, self.end_y,
self.end_x - self.dist() * math.cos(self.angle_l),
self.end_y + self.dist() * math.sin(self.angle_l),
self.angle_r+(math.radians(90)-ang), self.angle_l-(math.radians(90)-ang))
wn = turtle.Screen()
myTurtle= turtle.Turtle()
tree=[]
def setup():
wn.setup(startx=0, starty=0)
myTurtle.setheading(90)
wn.screensize(400, 400)
root = Branch(0, (-wn.canvheight+100), 0, -100, ang, ang)
tree.append(root)
def add_branches():
for item in tree[::-1]:
if item.spawn is None:
tree.append(item.branch_right())
tree.append(item.branch_left())
item.spawn=True
if __name__ == '__main__':
myTurtle.speed(10)
setup()
print(len(tree))
for i in range(level):
add_branches()
print(tree)
for branch in tree:
branch.draw()
turtle.done()