-
Notifications
You must be signed in to change notification settings - Fork 46
/
draw.py
77 lines (68 loc) · 1.71 KB
/
draw.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
import turtle
from utils import *
import reeds_shepp as rs
import random as rd
# drawing n units (eg turtle.forward(n)) will draw n * SCALE pixels
SCALE = 40
def scale(x):
"""
Scale the input coordinate(s).
"""
if type(x) is tuple or type(x) is list:
return [p * SCALE for p in x]
return x * SCALE
def unscale(x):
"""
Unscale the input coordinate(s).
"""
if type(x) is tuple or type(x) is list:
return [p / SCALE for p in x]
return x / SCALE
# note: bob is a turtle
def vec(bob):
"""
Draw an arrow.
"""
bob.down()
bob.pensize(3)
bob.forward(scale(1.2))
bob.right(25)
bob.backward(scale(.4))
bob.forward(scale(.4))
bob.left(50)
bob.backward(scale(.4))
bob.forward(scale(.4))
bob.right(25)
bob.pensize(1)
bob.up()
def goto(bob, pos, scale_pos=True):
"""
Go to a position without drawing.
"""
bob.up()
if scale_pos:
bob.setpos(scale(pos[:2]))
else:
bob.setpos(pos[:2])
bob.setheading(pos[2])
bob.down()
def draw_path(bob, path):
"""
Draw the path (list of rs.PathElements).
"""
for e in path:
gear = 1 if e.gear == rs.Gear.FORWARD else -1
if e.steering == rs.Steering.LEFT:
bob.circle(scale(1), gear * rad2deg(e.param))
elif e.steering == rs.Steering.RIGHT:
bob.circle(- scale(1), gear * rad2deg(e.param))
elif e.steering == rs.Steering.STRAIGHT:
bob.forward(gear * scale(e.param))
def set_random_pencolor(bob):
"""
Draws noodles.
"""
r, g, b = 1, 1, 1
while r + g + b > 2.5:
r, g, b = rd.uniform(0, 1), rd.uniform(0, 1), rd.uniform(0, 1)
bob.pencolor(r, g, b)