4 import matplotlib.path as mpath
5 import matplotlib.patches as mpatches
6 import matplotlib.pyplot as plt
7 from matplotlib.patches import Arrow, Circle, Wedge, Polygon
8 from matplotlib.collections import PatchCollection
14 def build_poly(ptlist):
16 polydata.append((Path.MOVETO, (ptlist[0])))
18 polydata.append((Path.LINETO, (pt)))
19 polydata.append((Path.CLOSEPOLY, (ptlist[0])))
20 codes, verts = zip(*polydata)
21 poly = mpath.Path(verts, codes)
22 x, y = zip(*poly.vertices)
25 def build_path(ptlist):
27 polydata.append((Path.MOVETO, (ptlist[0])))
29 polydata.append((Path.LINETO, (pt)))
30 codes, verts = zip(*polydata)
31 poly = mpath.Path(verts, codes)
32 x, y = zip(*poly.vertices)
35 def graph(filename, stx, sty, sta, enx, eny, opx, opy):
36 cmd = "./main %d %d %d %d %d %d %d"%(stx, sty, sta, enx, eny, opx, opy)
37 o,i = popen2.popen2(cmd)
42 open(filename + ".txt", "w").write(s)
48 ax = fig.add_subplot(111)
51 x,y = build_poly([(0,0), (3000,0), (3000,2100), (0,2100)])
54 x,y = build_poly([(250,250), (2750,250), (2750,1850), (250,1850)])
58 patches = [ Circle((1500, 1050), 150) ]
64 for l in s.split("\n"):
65 m = re.match("robot at: (-?\d+) (-?\d+) (-?\d+)", l)
67 x,y,a = (int(m.groups()[0]), int(m.groups()[1]), int(m.groups()[2]))
69 a_rad = (a * math.pi / 180.)
70 dx = 150 * math.cos(a_rad)
71 dy = 150 * math.sin(a_rad)
72 patches += [ Circle((x, y), 50) ]
73 patches += [ Arrow(x, y, dx, dy, 50) ]
75 m = re.match("oa_start_end_points\(\) \((-?\d+),(-?\d+)\) \((-?\d+),(-?\d+)\)", l)
77 dst_x,dst_y = (int(m.groups()[2]), int(m.groups()[3]))
78 patches += [ Circle((dst_x, dst_y), 50) ]
80 m = re.match("oa_new_poly\(size=(-?\d+)\)", l)
82 poly_wait_pts = int(m.groups()[0])
85 m = re.match("oponent at: (-?\d+) (-?\d+)", l)
90 m = re.match("oa_poly_set_point\(\) \((-?\d+),(-?\d+)\)", l)
92 poly.append((int(m.groups()[0]), int(m.groups()[1])))
94 if poly_wait_pts == 0:
95 x,y = build_poly(poly)
98 m = re.match("GOTO (-?\d+),(-?\d+)", l)
100 path.append((int(m.groups()[0]), int(m.groups()[1])))
102 m = re.match("With avoidance .: x=(-?\d+) y=(-?\d+)", l)
104 path.append((int(m.groups()[0]), int(m.groups()[1])))
106 p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
109 x,y = build_path(path)
113 ax.set_xlim(-100, 3100)
114 ax.set_ylim(-100, 2200)
115 #ax.set_title('spline paths')
117 fig.savefig(filename)
119 # args are: startx, starty, starta, endx, endy, oppx, oppy
120 graph("normal1.png", 500, 1000, 180, 2000, 1500, 1500, 2000)
121 graph("normal2.png", 500, 300, 50, 2000, 1500, 2500, 2000)
122 graph("normal3.png", 500, 1000, 100, 1000, 1500, 2500, 1000)
123 graph("normal4.png", 500, 300, 0, 2500, 1700, 1000, 1000)
124 graph("normal5.png", 500, 1500, 0, 2500, 1700, 500, 1000)
127 graph("escape1.png", 5200, 1000, 0, 2500, 1700, 2500, 1000)
128 graph("escape2.png", 500, 1300, 0, 2500, 1700, 500, 1000)
129 graph("escape3.png", 500, 1000, 180, 2000, 1500, 1500, 2000)
131 # pas d'echappement possible... petit carre
132 graph("small_square1.png", 500, 300, 0, 2500, 1700, 500, 500)
133 graph("small_square2.png", 500, 1000, 0, 2500, 1700, 500, 1000)
134 graph("small_square3.png", 590, 1000, 0, 2500, 1700, 500, 1000)
135 graph("small_square4.png", 560, 1000, 0, 2500, 1700, 300, 1000)
136 graph("small_square5.png", 550, 1000, 0, 2500, 1700, 250, 1000)
137 graph("small_square6.png", 5750, 1000, 0, 2500, 1700, 2750, 1000)
140 graph("impossible1.png", 500, 300, 0, 2700, 1800, 2700, 1800)
141 graph("impossible2.png", 500, 300, 0, 2700, 1800, 2400, 1500)
142 graph("impossible3.png", 500, 300, 0, 1500, 1150, 2700, 1800 )
145 graph("current01.png", 1274,709, 58, 1312, 400, 2274, 1241 )
150 stx = random.randint(-50, 3050)
151 sty = random.randint(-50, 2050)
152 enx = random.randint(-50, 3050)
153 eny = random.randint(-50, 2050)
154 opx = random.randint(-50, 3050)
155 opy = random.randint(-50, 2050)
156 name = "random%d.png"%(i)
157 print (name, stx, sty, 0, enx, eny, opx, opy)
158 graph(name, stx, sty, 0, enx, eny, opx, opy)