-
Notifications
You must be signed in to change notification settings - Fork 0
/
lforest.py
107 lines (92 loc) · 2.11 KB
/
lforest.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
from __future__ import print_function
#
# Code by Alexander Pruss and under the MIT license
#
# L-system with turtle graphics
#
import lsystem
import random
from mineturtle import *
t = Turtle()
t.pendelay(0)
t.turtle(None)
t.verticalangle(90)
def tree():
global angle
global thickness
global length
angle = 15
thickness = 8
length = 10
t.pendown()
t.penblock(block.WOOD)
rules = {'A': [(0.55,'^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]'),
(0.25,'^f>>[^^f>>>>>>A]>>>[^^f>>>>>>A]')]}
axiom = 'fA'
material = block.WOOD
t.penwidth(thickness)
t.penblock(material)
stack = []
def push():
global length
global thickness
stack.append((length,thickness))
t.push()
thickness = thickness * 0.6
if length == 10:
length = 9
elif length == 9:
length = 8.4
else:
length = length * 0.75
if thickness < 1:
thickness = 1
if length <= 1.6:
t.penblock(block.LEAVES_OAK_PERMANENT)
t.penwidth(thickness)
def pop():
global length
global thickness
length,thickness = stack.pop()
t.pop()
dictionary = {
'[': push,
']': pop,
'^': lambda: t.pitch(angle),
'>': lambda: t.roll(angle),
'f': lambda: t.go(length)
}
lsystem.lsystem(axiom, rules, dictionary, 11)
#tree()
MIN_DISTANCE = 30
MAX_TRIES = 100
SIZE = MIN_DISTANCE * 10
OVALITY = 1.5
cx = t.position.x
cy = t.position.y
cz = t.position.z
tryCount = 0
positions = []
while tryCount < MAX_TRIES:
x = random.uniform(-1,1)
z = random.uniform(-1,1)
if x**2 + z**2 > 1:
continue
x = cx + SIZE/2 * x
z = cz + SIZE/2 * OVALITY * z
ok = True
for x0,z0 in positions:
if (x-x0)**2 + (z-z0)**2 < MIN_DISTANCE**2:
ok = False
break
if not ok:
tryCount += 1
continue
positions.append((x,z))
tryCount = 0
t.goto(x,cy,z)
print(x,cy,z)
t.push()
t.roll(random.uniform(0,30))
tree()
t.pop()