-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathturtle_tree.py
117 lines (94 loc) · 2.82 KB
/
turtle_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
from cqmore.matrix import rotation, translation
from cqmore import Workplane
from cqmore.polyhedron import tetrahedron
class Turtle:
def __init__(self, pos = (0, 0, 0)):
self.coordinateVt = pos
self.xAxis = (1, 0, 0)
self.yAxis = (0, 1, 0)
self.zAxis = (0, 0, 1)
def forward(self, leng):
m = translation(tuple(elem * leng for elem in self.xAxis)) # type: ignore
self.coordinateVt = m.transform(self.coordinateVt)
return self
def roll(self, angle):
xr = rotation(self.xAxis, angle)
self.yAxis = xr.transform(self.yAxis)
self.zAxis = xr.transform(self.zAxis)
return self
def pitch(self, angle):
yr = rotation(self.yAxis, -angle)
self.xAxis = yr.transform(self.xAxis)
self.zAxis = yr.transform(self.zAxis)
return self
def turn(self, angle):
zr = rotation(self.zAxis, angle)
self.xAxis = zr.transform(self.xAxis)
self.yAxis = zr.transform(self.yAxis)
return self
def pos(self):
return self.coordinateVt
def copy(self):
t = Turtle()
t.coordinateVt = self.coordinateVt
t.xAxis = self.xAxis
t.yAxis = self.yAxis
t.zAxis = self.zAxis
return t
def turtle_tree(leng, leng_scale1, leng_scale2, limit, turnAngle, rollAngle, line_diameter):
_LINE_WORKPLANE = Workplane()
_LINE_JOIN = _LINE_WORKPLANE.polyhedron(*tetrahedron(line_diameter / 2))
def line(p1, p2):
return _LINE_WORKPLANE.polylineJoin([p1, p2], _LINE_JOIN)
def _turtle_tree(workplane, turtle, leng, leng_scale1, leng_scale2, limit, turnAngle, rollAngle):
if leng > limit:
workplane = workplane.add(
line(turtle.pos(), turtle.forward(leng).pos())
)
workplane = _turtle_tree(
workplane,
turtle.copy().turn(turnAngle),
leng * leng_scale1,
leng_scale1,
leng_scale2,
limit,
turnAngle,
rollAngle
)
return _turtle_tree(
workplane,
turtle.copy().roll(rollAngle),
leng * leng_scale2,
leng_scale1,
leng_scale2,
limit,
turnAngle,
rollAngle
)
return workplane
return _turtle_tree(
Workplane(),
Turtle(),
leng,
leng_scale1,
leng_scale2,
limit,
turnAngle,
rollAngle
).combine()
leng = 20
limit = 1
leng_scale1 = 0.4
leng_scale2 = 0.9
turnAngle = 60
rollAngle = 135
line_diameter = 4
tree = turtle_tree(
leng,
leng_scale1,
leng_scale2,
limit,
turnAngle,
rollAngle,
line_diameter
)