-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualizer.py
166 lines (123 loc) · 3.55 KB
/
Visualizer.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
__author__ = 'aprakash'
import turtle
from math import *
DIMX = 800.
DIMY = 600.
VECTOR_DIM = 50.
zone_color_map = ["red", "blue", "red", "blue", "green", "blue", "red", "blue", "red"]
def setup(bot):
global scale
scale = get_scale(bot)
turtle.setup(DIMX, DIMY)
window = turtle.Screen()
window.bgcolor('white')
window.colormode(255)
# turtle.setworldcoordinates(0, 0, DIMX, DIMY)
turtle.tracer(10, 1)
draw_lines(bot)
b = turtle.Turtle()
b.radians()
b.shape('arrow')
b.color('green')
b.resizemode('user')
b.shapesize(0.5, 0.5, 0.5)
b.penup()
return window, b
def draw_lines(bot):
ox, oy = scale_dim(bot.offset_x , bot.offset_y)
dx, dy = scale_dim(bot.maxx - bot.minx, bot.maxy - bot.miny)
lines = turtle.Turtle()
lines.penup()
# draw outlines
lines.width(5.)
lines.color("black")
drawV(lines, 0, dy)
drawV(lines, dx, dy)
drawH(lines, 0, dx)
drawH(lines, dy, dx)
# draw zone lines
lines.penup()
lines.width(1.)
lines.color("purple")
drawV(lines, ox, dy)
drawV(lines, dx - ox, dy)
drawH(lines, oy, dx)
drawH(lines, dy - oy, dx)
# draw vertical line at x
def drawV(trtl, x_coord, max = DIMY):
draw_line(trtl, (x_coord, 0), (x_coord, max))
# draw horizontal line at y
def drawH(trtl, y_coord, max = DIMX):
draw_line(trtl, (0, y_coord), (max, y_coord))
# draw line from p1 to p2
def draw_line(trtl, p1, p2):
x1, y1 = p1
x2, y2 = p2
trtl.penup()
trtl.goto(shift(x1, y1))
trtl.pendown()
trtl.goto(shift(x2, y2))
trtl.penup()
def shift(x, y):
x -= DIMX / 2.
y -= DIMY / 2.
return x, y
def scale_dim(x, y):
x /= scale
y /= scale
return x, y
def bot_scale_shift(x, y, bot):
x -= bot.minx # zero x's to bot.minx
y -= bot.miny # zero y's to bot.miny
# scale to fit in window
x, y = scale_dim(x, y)
# shift window origin to bottom left corner
x, y = shift(x, y)
return x, y
def show_vectors(bot, granularity = 10):
window, b = setup(bot)
for i in range(1, len(bot.magnitudes), granularity):
draw_vector(i, b, bot)
window.exitonclick()
def show_path(bot):
window, b = setup(bot)
for i in range(len(bot.measurements)):
draw_point(i, b, bot)
window.exitonclick()
def draw_vector(index, trtl, bot):
x, y = bot.measurements[index]
x, y = bot_scale_shift(x, y, bot)
zone = bot.zones[index]
trtl.color(zone_color_map[zone - 1])
mag = bot.magnitudes[index]
dir = bot.directions[index]
trtl.goto(x, y)
trtl.setheading(dir) #/ 2. / pi * 360.)
trtl.pendown()
trtl.forward(mag / bot.max_mag * VECTOR_DIM)
trtl.stamp()
trtl.penup()
def draw_point(index, trtl, bot):
x, y = bot.measurements[index]
x, y = bot_scale_shift(x, y, bot)
zone = bot.zones[index]
trtl.color(zone_color_map[zone - 1])
trtl.goto(x, y)
trtl.pendown()
def get_scale(bot):
return max((bot.maxx - bot.minx) / DIMX, (bot.maxy - bot.miny) / DIMY)
def show_histo(bot):
window, b = setup(bot)
b.shape("square")
dx, dy = scale_dim(DIMX / bot.histo_x, DIMY / bot.histo_y)
b.shapesize(dx, dy)
H = bot.histogram
for row in range(len(H)):
for col in range(len(H[0])):
b.penup()
x, y = shift(col * DIMX / scale / bot.histo_x, row * DIMY / scale / bot.histo_y)
b.goto(x, y)
intensity = min(255, 255. * H[row][col] / bot.maxh + 20)
b.color(intensity, intensity, intensity)
b.stamp()
window.exitonclick()