39from sys
import argv, stdout
40from os.path
import basename, splitext
41from math
import cos, sin, atan2, pi, ceil
42import matplotlib.pyplot
as plt
43import matplotlib.animation
as animation
51propagationStepSize = 0
57fig = plt.figure(figsize=(6, 6))
58ax = plt.axes(xlim=(0, 1), ylim=(0, 1))
59fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=
None, hspace=
None)
60(handle,) = ax.plot([], [])
64def normalizeAngle(theta):
66 return theta + 2.0 * pi
68 return theta - 2.0 * pi
75 (cs, ss) = (shipRadius * cos(theta), shipRadius * sin(theta))
76 v = [u[0] - x[2], u[1] - x[3]]
77 deltaTheta = normalizeAngle(atan2(v[1], v[0]) - theta)
78 if v[0] * v[0] + v[1] * v[1] >= shipDelta * shipDelta:
79 if abs(deltaTheta) < shipEps:
82 plt.Circle((pos[0] - cs, pos[1] - ss), 0.3 * shipRadius, color=
"red")
87 plt.Circle((pos[0] + ss, pos[1] - cs), 0.3 * shipRadius, color=
"red")
92 plt.Circle((pos[0] - ss, pos[1] + cs), 0.3 * shipRadius, color=
"red")
95 ax.add_patch(plt.Circle(x[:2], shipRadius, color=
"yellow"))
100 pos[0] + 0.7 * shipRadius * cos(theta + 0.75),
101 pos[1] + 0.7 * shipRadius * sin(theta + 0.75),
110 pos[0] + 0.7 * shipRadius * cos(theta - 0.75),
111 pos[1] + 0.7 * shipRadius * sin(theta - 0.75),
119def plotKoules(state):
120 numKoules = int(len(state) / 4)
121 for i
in range(numKoules):
123 plt.Circle((state[4 * i], state[4 * i + 1]), kouleRadius, color=
"red")
127def plotSystem(index):
129 ax.add_patch(plt.Rectangle((0, 0), 1, 1, color=
"black"))
130 plotKoules(path[index][5:-3])
131 plotShip(path[index][0:5], path[index][-3:])
139 with open(fname,
"r")
as f:
144 propagationStepSize, \
159 ) = [float(x)
for x
in next(f).split()]
160 path = [[float(x)
for x
in line.split(
" ")]
for line
in f]
162 print(
"Error: %s contains no solution path" % fname)
164 step = int(ceil(speedUp / (propagationStepSize * targetFrameRate)))
165 path = path[0 : len(path) : step]
166 print(
"Creating a movie with %d frames..." % len(path))
167 print(
"Printing a '.' for every 10th frame:")
168 ani = animation.FuncAnimation(
169 fig, plotSystem, frames=len(path), interval=1000.0 / step, blit=
True
171 (base, _) = splitext(basename(fname))
172 outfname = base +
".mp4"
173 ani.save(outfname, bitrate=300, fps=targetFrameRate)
177if __name__ ==
"__main__":
179 print(
"Usage: KoulesPlayback.py <filename> [<filename2> ...]")
181 for trajectory_file
in argv[1:]:
182 makeMovie(trajectory_file)