-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle3d.py
293 lines (246 loc) · 7.38 KB
/
turtle3d.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from vpython import *
class Turtle3D:
def __init__(self):
"""
Initializes the turtle at the origin of the coordinates
looking horizontally to the right and in red
Parameters
----------
None
Returns
-------
None
"""
self._activeScene = False
self._yaw = 0
self._pitch = 0
self._turtle = None
self._currentPaintingColor = color.red
self._homePosition = vector(0, 0, 0)
self._homeOrientation = vector(1, 0, 0)
self._canDraw = True
self.trailWidth = 0.08
#print("Hi!, I am a turtle :D!")
def setupScene(self):
"""
Initializes a default scene, the turtle object and draws it in the scene
Parameters
----------
None
Returns
-------
None
"""
self._activeScene = True
scene.height = scene.width = 1000
scene.autocenter = True
scene.caption = """\nTo rotate "camera", drag with right button or Ctrl-drag.\nTo zoom, drag with middle button or Alt/Option depressed, or use scroll wheel.\n On a two-button mouse, middle is left + right.\nTo pan left/right and up/down, Shift-drag.\nTouch screen: pinch/extend to zoom, swipe or two-finger rotate.\n"""
self._turtle = cylinder(
pos=self._homePosition,
axis=self._homeOrientation,
radius=0.1,
color=color.green)
def _getDirection(self):
"""
Returns the direction calculated from the current Euler Angles
of the turtle.
Parameters
----------
None
Returns
-------
A vector with the calculated direction
"""
yaw = self._yaw
pitch = self._pitch
x = cos(yaw) * cos(pitch)
z = sin(yaw) * cos(pitch)
y = sin(pitch)
return vector(x, y, z)
def _draw(self, distance):
"""
Draws the turtle and three objects more in the scene, a cylinder
of size distance which acts as the trail of the turtle
and two cilinders at both ends of it for aesthetic purposes.
Only leaves a trail if '_canDraw' is true otherwise just changes
the direction and position of the turtle according to the
displacement of the distance.
Parameters
----------
distance: float
A float that indicates the displacement of the turtle in the direction
described by its current Euler angles
Returns
-------
None
"""
direction = self._getDirection()
oldPosition = self._turtle.pos
newPosition = direction * distance + oldPosition
newAxis = newPosition - oldPosition
if(self._canDraw):
sphere(
pos=oldPosition,
radius=self.trailWidth,
color=self._currentPaintingColor)
cylinder(
pos=oldPosition,
axis=newAxis,
radius=self.trailWidth,
color=self._currentPaintingColor)
sphere(
pos=newPosition,
radius=self.trailWidth,
color=self._currentPaintingColor)
self._turtle.pos = newPosition
self._turtle.axis = direction
def forward(self, distance):
"""
Moves the turtle forward in the current direction a distance 'distance'.
Parameters
----------
distance : float
A float describing the distance the turtle will move.
Returns
-------
None
"""
# print("forward")
self._draw(distance)
def backward(self, distance):
"""
Moves the turtle in the opposite direction to the current direction a distance 'distance'.
Parameters
----------
distance : float
A float describing the distance the turtle will move.
Returns
-------
None
"""
# print("backward")
self._draw(-distance)
def left(self, deg):
"""
Changes the turtle direction 'deg' degrees towards the left with respect to the horizontal axis.
Parameters
----------
deg : float
A float describing the degrees turtle will rotate.
Returns
-------
None
"""
self._yaw += radians(-deg)
self._draw(0)
# print("left")
def right(self, deg):
"""
Changes the turtle direction 'deg' degrees towards the right with respect to the horizontal axis.
Parameters
----------
deg : float
A float describing the degrees turtle will rotate.
Returns
-------
None
"""
self._yaw += radians(deg)
self._draw(0)
# print("right")
def up(self, deg):
"""
Changes the turtle direction 'deg' degrees up with respect to the vertical axis.
Parameters
----------
deg : float
A float describing the degrees turtle will rotate.
Returns
-------
None
"""
self._pitch += radians(deg)
self._draw(0)
# print("up")
def down(self, deg):
"""
Changes the turtle direction 'deg' degrees down with respect to the vertical axis.
Parameters
----------
deg : float
A float describing the degrees turtle will rotate.
Returns
-------
None
"""
self._pitch += radians(-deg)
self._draw(0)
# print("down")
def color(self, r, g, b):
"""
Changes the color used, by the turtle, to draw on screen.
Parameters
----------
r : float
A float value denoting the red component in RGB, between 0 and 1.
g : float
A float value denoting the green component in RGB, between 0 and 1.
b : float
A float value denoting the blue component in RGB, between 0 and 1.
Returns
-------
None
"""
self._currentPaintingColor = vector(r, g, b)
#print("set color!")
def hide(self):
"""
The turtle stops drawing on screen while moving.
Parameters
----------
None
Returns
-------
None
"""
self._canDraw = False
# print("hide")
def show(self):
"""
The turtle starts drawing on screen while moving.
Parameters
----------
None
Returns
-------
None
"""
self._canDraw = True
# print("show")
def home(self):
"""
Changes the turtle's position to its original position and orientation.
Parameters
----------
None
Returns
-------
None
"""
# print("home")
self._pitch = 0
self._yaw = 0
self._turtle.pos = self._homePosition
self._turtle.axis = self._homeOrientation
def isSceneActive(self):
"""
Verifies if the scene has already been initialized or not.
Parameters
----------
None
Returns
-------
activeScene : bool
Returns true if the scene has already been initialized. False otherwise.
"""
return self._activeScene