Repository navigation
Expand file tree
/
Copy pathgraphics.py
More file actions
107 lines (78 loc) · 2.41 KB
/
Copy pathgraphics.py
File metadata and controls
107 lines (78 loc) · 2.41 KB
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
#!/usr/bin/env python
"""
Graphics for simulations, based on turtle graphics
Author - Jev Kuznetsov
"""
import math
from time import sleep
import turtle
TESTING = True
class World:
""" wrapper class for visualisations """
def __init__(self, coord=(-10, -10, 10, 10), window_size=(800, 800)):
""" create world with worldcoordinates as coord """
self.screen = turtle.Screen()
self.screen.setup(width=window_size[0], height=window_size[1])
self.screen.setworldcoordinates(*coord)
self.screen.tracer(0, 0)
self.screen.onclick(self.click_callback)
self.turtle = turtle.Turtle()
self.turtle.color('blue')
self._markers = {} # marker objects
self.click_xy = None # last clicked position
def click_callback(self, x, y):
self.click_xy = (x, y)
def add_marker(self,
name,
color='green',
shape="circle",
size=0.5,
trace=False):
""" add marker (markers are Turtle objects) """
m = turtle.Turtle()
m.color(color)
m.shape(shape)
m.shapesize(size, size)
if not trace:
m.penup()
self._markers[name] = m
def move_marker(self, name, xy):
"""move marker to a position
Args:
name (str): marker name
xy (Vector): (x,y) vector
"""
self._markers[name].setpos(xy)
self.screen.update()
def move_robot(self, xy, phi):
""" plot robot state """
self.turtle.setpos(xy)
self.turtle.setheading(math.degrees(phi))
self.screen.update()
def plot_path(self, points, color='red', dotsize=3):
""" plot conntected points """
t = turtle.Turtle()
t.color(color)
t.hideturtle()
for x, y in points:
t.setpos(x, y)
t.dot(dotsize)
self.screen.update()
def main():
""" demo function """
w = World()
# plot path
points = [(-5, -5), (0, 0), (0, 2),
(2, 2), (2, 0), (2, -2), (5, 5)]
w.plot_path(points)
w.add_marker('m')
for x in range(10):
w.move_marker('m', (x/10, 0))
w.move_robot((0, x), math.pi/2)
if not TESTING:
sleep(0.1)
if not TESTING:
print('Please close graphics window...')
turtle.done()
if __name__ == "__main__":
main()