Skip to content

Commit 133baaa

Browse files
committed
better animation
1 parent b83912a commit 133baaa

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed
Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import random
33
import matplotlib.pyplot as plt
4-
4+
import matplotlib.animation as animation
55
class Grid():
66

77
# Set in constructor
@@ -47,53 +47,38 @@ def generate_dynamic_obstacle(self, obs_idx: int) -> list[np.ndarray[int, int]]:
4747
print("Impossible situation for obstacle!")
4848
positions.append(positions[-1])
4949

50+
print('obs path len: ', len(positions))
5051
return positions
5152

5253
show_animation = True
5354

5455
def main():
55-
grid = Grid(np.array([10, 10]))
56-
57-
plt.figure()
58-
59-
for t in range(0, grid.time_limit):
60-
plt.clf()
61-
62-
if show_animation: # pragma: no cover
63-
# TODO: iter is clunky. Should use np array
64-
ax = plt.axes()
65-
ax.set_xlim(0, grid.grid_size[0])
66-
ax.set_ylim(0, grid.grid_size[1])
67-
68-
for (obs_idx, obs_path) in enumerate(grid.obstacle_paths):
69-
obs_pos = obs_path[t]
70-
# plt.plot(obs_pos[0], obs_pos[1], "xr")
71-
circle = plt.Circle((obs_pos[0], obs_pos[1]), 0.2)
72-
ax.add_patch(circle)
73-
plt.grid(True)
74-
plt.pause(0.3)
75-
76-
# TODO: better animation closing
77-
# fig, ax = plt.subplots()
78-
# line, = ax.plot([], [])
79-
# ax.set_xlim(0, 10)
80-
# ax.set_ylim(-1, 1)
81-
82-
# def init():
83-
# line.set_data([], [])
84-
# return line,
85-
86-
# def animate(i):
87-
# x = [0, 10]
88-
# y = [0, i % 2 * 2 - 1]
89-
# line.set_data(x, y)
90-
# return line,
91-
92-
# ani = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=20, blit=True)
93-
94-
# def close_event(evt):
95-
# ani.event_source.stop()
96-
# plt.close(fig)
56+
grid = Grid(np.array([11, 11]))
57+
58+
if not show_animation:
59+
return
60+
61+
fig = plt.figure(figsize=(8, 7))
62+
ax = fig.add_subplot(autoscale_on=False, xlim=(0, grid.grid_size[0]-1), ylim=(0, grid.grid_size[1]-1))
63+
ax.set_aspect('equal')
64+
ax.grid()
65+
ax.set_xticks(np.arange(0, 11, 1))
66+
ax.set_yticks(np.arange(0, 11, 1))
67+
points, = ax.plot([], [], 'ro', ms=15)
68+
69+
def get_frame(i):
70+
obs_x_points = []
71+
obs_y_points = []
72+
for (_obs_idx, obs_path) in enumerate(grid.obstacle_paths):
73+
obs_pos = obs_path[i]
74+
obs_x_points.append(obs_pos[0])
75+
obs_y_points.append(obs_pos[1])
76+
points.set_data(obs_x_points, obs_y_points)
77+
return points,
78+
79+
_ani = animation.FuncAnimation(
80+
fig, get_frame, grid.time_limit-1, interval=500, blit=True, repeat=False)
81+
plt.show()
9782

9883
if __name__ == '__main__':
9984
main()

0 commit comments

Comments
 (0)