-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaf1.py
151 lines (128 loc) · 4.24 KB
/
leaf1.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
import drawsvg
import random
import datetime
now = str(datetime.datetime.now()).replace(" ","_").replace(":","_")
def rgb_to_hex(red, green, blue):
return '#%02x%02x%02x' % (red, green, blue)
class Tree:
def __init__(self,x,y,d):
self.branches = []
self.drawing = d
self.x2 = x
self.y2 = y
self.red = 200
self.green = 200
self.blue = 200
self.length_to_root = 0
self.tree = self
def has(self,x,y):
for branch in self.branches:
if branch.has(x,y):
return True
return False
def add_branch(self,branch):
self.branches.append(branch)
def grow(self):
branch = Branch(self,self.x2+5,self.y2)
branch.grow()
def __str__(self):
out = "tree:\n"
for branch in self.branches:
out += str(branch) + "\n"
return out
class Branch:
def __init__(self,origin,x2,y2):
self.valid = False
self.x2 = x2
self.y2 = y2
self.children = []
self.origin = origin
self.tree = origin.tree
self.length_to_root = origin.length_to_root + 1
self.x1 = origin.x2
self.y1 = origin.y2
self.drawing = origin.drawing
self.red = 200
if origin.red == 200:
self.red = 150
self.blue = origin.blue
self.green = origin.green
if not self.tree.has(self.x2,self.y2):
self.valid = True
self.draw()
self.tree.add_branch(self)
def draw(self):
self.drawing.append(drawsvg.Line(self.x1,self.y1,self.x2,self.y2,fill="#eeee00",stroke=rgb_to_hex(self.red,self.green,self.blue)))
def isValid(self):
return self.valid
def add_child(self,child):
self.children.append(child)
def has(self,x,y):
return (self.x1 == x and self.y1 == y) or (self.x2 == x and self.y2 == y)
def grow(self):
if self.x2 >= 400 or self.y2 >= 400:
return
#if self.length_to_root >= 40:
# return
x = random.randint(0,2)
branch1 = False
if x != 0:
branch1 = Branch(self,self.x2+5,self.y2+5)
x = random.randint(0,2)
branch2 = False
if x != 0:
branch2 = Branch(self,self.x2+5,self.y2)
x = random.randint(0,2)
branch3 = False
if x != 0:
branch2 = Branch(self,self.x2,self.y2+5)
x = random.randint(0,5)
if x == 0:
if branch3 and branch3.isValid():
branch3.grow()
if branch2 and branch2.isValid():
branch2.grow()
if branch1 and branch1.isValid():
branch1.grow()
if x == 5:
if branch3 and branch3.isValid():
branch3.grow()
if branch1 and branch1.isValid():
branch1.grow()
if branch2 and branch2.isValid():
branch2.grow()
if x == 1:
if branch1 and branch1.isValid():
branch1.grow()
if branch3 and branch3.isValid():
branch3.grow()
if branch2 and branch2.isValid():
branch2.grow()
if x == 2:
if branch1 and branch1.isValid():
branch1.grow()
if branch2 and branch2.isValid():
branch2.grow()
if branch3 and branch3.isValid():
branch3.grow()
if x == 3:
if branch2 and branch2.isValid():
branch2.grow()
if branch1 and branch1.isValid():
branch1.grow()
if branch3 and branch3.isValid():
branch3.grow()
if x == 4:
if branch2 and branch2.isValid():
branch2.grow()
if branch3 and branch3.isValid():
branch3.grow()
if branch1 and branch1.isValid():
branch1.grow()
def __str__(self):
return "branch:" + str(self.x1) + "," + str(self.y1) + "|" + str(self.x2) + "," + str(self.y2) + "|" + rgb_to_hex(self.red,self.blue,self.green) + "|" + str(self.length_to_root)
d = drawsvg.Drawing(410,410,origin="center")
t = Tree(-170,-170,d)
t.grow()
now = ""
d.save_svg("leaf1" + now + ".svg")