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)
171 x,y = build_poly([(0,0), (3000,0), (3000,2100), (0,2100)])
174 x,y = build_poly([(0,0), (0,500), (500,500), (500,0)])
177 x,y = build_poly([(3000,0), (3000,500), (2500,500), (2500,0)])
180 x,y = build_poly([(740,0), (740,500), (2260,500), (2260,0)])
184 #x,y = build_poly([(250,250), (2750,250), (2750,1850), (250,1850)])
185 #ax.plot(x, y, 'g--')
187 init_corn_table(random.randint(0,8), random.randint(0,3))
189 waypoints = init_waypoints()
205 y = OFFSET_CORN_Y + STEP_CORN_Y/2
209 if waypoints[i][j] == TYPE_WHITE_CORN:
210 wcorn.append(Circle((x, y), 25))
211 if waypoints[i][j] == TYPE_BLACK_CORN:
212 bcorn.append(Circle((x, y), 25))
213 elif waypoints[i][j] == TYPE_WAYPOINT:
214 points.append(Circle((x, y), 10))
215 elif waypoints[i][j] == TYPE_DANGEROUS:
216 dpoints.append(Circle((x, y), 10))
217 elif waypoints[i][j] == TYPE_NEIGH:
218 neighs.append(Circle((x, y), 10))
225 p = PatchCollection(wcorn, cmap=matplotlib.cm.jet,
226 alpha=0.5, facecolor=(.5,.5,1))
229 p = PatchCollection(bcorn, cmap=matplotlib.cm.jet,
230 alpha=0.7, facecolor=(0,0,0))
233 p = PatchCollection(points, cmap=matplotlib.cm.jet,
234 alpha=0.3, facecolor=(1,1,0))
237 p = PatchCollection(dpoints, cmap=matplotlib.cm.jet,
238 alpha=0.5, facecolor=(1,0,1))
241 p = PatchCollection(neighs, cmap=matplotlib.cm.jet,
242 alpha=1., facecolor=(1,0,0),
249 ax = fig.add_subplot(111)
254 ax.set_xlim(-100, 3100)
255 ax.set_ylim(-100, 2200)
256 #ax.set_title('spline paths')
258 fig.savefig(filename)