-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBranching
68 lines (59 loc) · 1.61 KB
/
Branching
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
w = 1000
h = 1000
dia = 5
step = 5
trees = []
def setup():
global t, pg
size(w, h)
pg = createGraphics(w, h)
pg.beginDraw()
pg.background(255)
pg.stroke(0)
pg.strokeWeight(5)
pg.endDraw()
trees.append(Tree(w/2, h/2, random(TAU)))
def draw():
global t
image(pg, 0, 0)
pg.loadPixels()
pg.beginDraw()
for t in trees:
cx = t.x
cy = t.y
newang = t.ang + random(-TAU*0.1, TAU*0.1)
t.ang = newang
if abs(degrees(newang)-degrees(t.beginang)) > 50:
trees.remove(t)
bol = 0
for dis in range(5, step*5):
newx = dis*cos(newang)+cx
newy = dis*sin(newang)+cy
inde = floor(newx) + floor(newy) * w
if red(pg.pixels[inde]) < 50:
bol += 1
if bol == 0:
newx = step*cos(newang)+cx
newy = step*sin(newang)+cy
if dist(w/2, h/2, newx, newy) < 400:
pg.line(cx, cy, newx, newy)
t.x = newx
t.y = newy
t.beginang = newang
else:
trees.remove(t)
else: t.stuck += 1
if t.stuck > 10:
t = Tree(w/2, h/2, random(TAU))
elif floor(random(10)) == 5 and len(trees) < 500:
trees.append(Tree(newx, newy, random(TAU)))
pg.endDraw()
class Tree(object):
def __init__(self, startx, starty, startAng):
self.x = startx
self.y = starty
self.px = startx
self.py = starty
self.ang = startAng
self.beginang = startAng
self.stuck = 0