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
26 col = [TYPE_WAYPOINT] * WAYPOINTS_NBY
27 waypoints = [col[:] for i in range(WAYPOINTS_NBX)]
28 corn_table = [TYPE_WHITE_CORN]*18
50 def mark_one_neigh(i, j):
52 if i < 0 or j < 0 or i >= WAYPOINTS_NBX or j >= WAYPOINTS_NBY:
54 if waypoints[i][j] != TYPE_WAYPOINT:
56 waypoints[i][j] = TYPE_NEIGH
58 def mark_all_neigh(i, j):
60 if waypoints[i][j] != TYPE_WAYPOINT:
62 waypoints[i][j] = TYPE_NEIGH
68 mark_one_neigh(i+1,j-1)
69 mark_one_neigh(i-1,j-1)
71 mark_one_neigh(i+1,j+1)
72 mark_one_neigh(i-1,j+1)
75 if i == 0 and j == 2: return 0
76 if i == 0 and j == 4: return 1
77 if i == 0 and j == 6: return 2
78 if i == 2 and j == 3: return 3
79 if i == 2 and j == 5: return 4
80 if i == 2 and j == 7: return 5
81 if i == 4 and j == 4: return 6
82 if i == 4 and j == 6: return 7
83 if i == 6 and j == 5: return 8
84 if i == 6 and j == 7: return 9
85 if i == 8 and j == 4: return 10
86 if i == 8 and j == 6: return 11
87 if i == 10 and j == 3: return 12
88 if i == 10 and j == 5: return 13
89 if i == 10 and j == 7: return 14
90 if i == 12 and j == 2: return 15
91 if i == 12 and j == 4: return 16
92 if i == 12 and j == 6: return 17
96 sym = [15, 16, 17, 12, 13, 14, 10, 11, 8, 9, 6, 7, 3, 4, 5, 0, 1, 2]
99 def init_corn_table(conf_side, conf_center):
100 global corn_table, corn_side_confs, corn_center_confs
101 print "confs = %d, %d"%(conf_side, conf_center)
103 if i in corn_side_confs[conf_side]:
104 corn_table[i] = TYPE_BLACK_CORN
106 if corn_get_sym(i) in corn_side_confs[conf_side]:
107 corn_table[i] = TYPE_BLACK_CORN
109 if i in corn_center_confs[conf_center]:
110 corn_table[i] = TYPE_BLACK_CORN
112 if corn_get_sym(i) in corn_center_confs[conf_center]:
113 corn_table[i] = TYPE_BLACK_CORN
115 corn_table[i] = TYPE_WHITE_CORN
117 def init_waypoints():
118 global waypoints, corn_table
120 for i in range(WAYPOINTS_NBX):
121 for j in range(WAYPOINTS_NBY):
125 waypoints[i][j] = corn_table[c]
127 # too close of border
128 if (i & 1) == 1 and j == WAYPOINTS_NBY -1:
129 waypoints[i][j] = TYPE_OBSTACLE
132 if i >= 2 and i < WAYPOINTS_NBX - 2 and j < 2:
133 waypoints[i][j] = TYPE_OBSTACLE
136 if i == 0 or i == WAYPOINTS_NBX-1:
137 waypoints[i][j] = TYPE_DANGEROUS
139 if (i&1) == 0 and j == WAYPOINTS_NBY-1:
140 waypoints[i][j] = TYPE_DANGEROUS
143 waypoints[i][j] = TYPE_WAYPOINT
145 print i, waypoints[i]
148 def build_poly(ptlist):
150 polydata.append((Path.MOVETO, (ptlist[0])))
151 for pt in ptlist[1:]:
152 polydata.append((Path.LINETO, (pt)))
153 polydata.append((Path.CLOSEPOLY, (ptlist[0])))
154 codes, verts = zip(*polydata)
155 poly = mpath.Path(verts, codes)
156 x, y = zip(*poly.vertices)
159 def build_path(ptlist):
161 polydata.append((Path.MOVETO, (ptlist[0])))
162 for pt in ptlist[1:]:
163 polydata.append((Path.LINETO, (pt)))
164 codes, verts = zip(*polydata)
165 poly = mpath.Path(verts, codes)
166 x, y = zip(*poly.vertices)
169 def nearest_corn(x, y):
186 j = (y * 2) + (x & 1)
187 if i >= WAYPOINTS_NBX:
189 if j >= WAYPOINTS_NBY:
195 y = OFFSET_CORN_Y + STEP_CORN_Y/2
196 y += (j * STEP_CORN_Y)
198 x = OFFSET_CORN_X + (i * STEP_CORN_X)
205 x,y = build_poly([(0,0), (3000,0), (3000,2100), (0,2100)])
208 x,y = build_poly([(0,0), (0,500), (500,500), (500,0)])
211 x,y = build_poly([(3000,0), (3000,500), (2500,500), (2500,0)])
214 x,y = build_poly([(740,0), (740,500), (2260,500), (2260,0)])
219 x,y = build_path([(375,0), (375,2050)])
222 x,y = build_path([(150,1972), (2850,472)])
225 x,y = build_path([(0,1722), (3000,1722)])
228 x,y = build_path([(150,972), (1950,1972)])
231 acoef = (2850-150)/5.
232 bcoef = (472-1972)/5.
233 x,y = build_path([(600,1972), (600+bcoef, 1972-acoef)])
236 p = PatchCollection([Circle((2400,972), 225)],
237 cmap=matplotlib.cm.jet,
238 alpha=0.5, facecolor=(1.,0.,0.))
244 #courbes a 2 balles 50
245 for xx in range(0, 3000, 100):
247 for yy in range(0, 2100, 100):
248 pouet = nearest_corn(xx, yy)
252 x,y = build_path([(xx,yy), (xxx,yyy)])
257 #x,y = build_poly([(250,250), (2750,250), (2750,1850), (250,1850)])
258 #ax.plot(x, y, 'g--')
260 xtick = [i * STEP_CORN_X + OFFSET_CORN_X for i in range(WAYPOINTS_NBX)]
262 ytick = [(i-1) * STEP_CORN_Y/2 + OFFSET_CORN_Y for i in range(WAYPOINTS_NBY*2)]
265 init_corn_table(random.randint(0,8), random.randint(0,3))
267 waypoints = init_waypoints()
283 y = OFFSET_CORN_Y + STEP_CORN_Y/2
287 if waypoints[i][j] == TYPE_WHITE_CORN:
288 wcorn.append(Circle((x, y), 25))
289 if waypoints[i][j] == TYPE_BLACK_CORN:
290 bcorn.append(Circle((x, y), 25))
291 elif waypoints[i][j] == TYPE_WAYPOINT:
292 points.append(Circle((x, y), 10))
293 elif waypoints[i][j] == TYPE_DANGEROUS:
294 dpoints.append(Circle((x, y), 10))
295 elif waypoints[i][j] == TYPE_NEIGH:
296 neighs.append(Circle((x, y), 10))
303 p = PatchCollection(wcorn, cmap=matplotlib.cm.jet,
304 alpha=0.5, facecolor=(.5,.5,1))
307 p = PatchCollection(bcorn, cmap=matplotlib.cm.jet,
308 alpha=0.7, facecolor=(0,0,0))
311 p = PatchCollection(points, cmap=matplotlib.cm.jet,
312 alpha=0.3, facecolor=(1,1,0))
315 p = PatchCollection(dpoints, cmap=matplotlib.cm.jet,
316 alpha=0.5, facecolor=(1,0,1))
319 p = PatchCollection(neighs, cmap=matplotlib.cm.jet,
320 alpha=1., facecolor=(1,0,0),
327 ax = fig.add_subplot(111, aspect='equal')
331 ax.grid(color = (0.3, 0.3, 0.3))
332 ax.set_xlim(-100, 3100)
333 ax.set_ylim(-100, 2200)
335 #ax.set_ylim(1472, 1972)
336 #ax.set_title('spline paths')
338 fig.savefig(filename)