A polygon editor. Key-bindings 't' toggle vertex markers on and off. When vertex markers are on, you can move them, delete them 'd' delete the vertex under point 'i' insert a vertex at point. You must be within epsilon of the line connecting two e
| 36 | |
| 37 | |
| 38 | class PolygonInteractor: |
| 39 | """ |
| 40 | A polygon editor. |
| 41 | |
| 42 | Key-bindings |
| 43 | |
| 44 | 't' toggle vertex markers on and off. When vertex markers are on, |
| 45 | you can move them, delete them |
| 46 | |
| 47 | 'd' delete the vertex under point |
| 48 | |
| 49 | 'i' insert a vertex at point. You must be within epsilon of the |
| 50 | line connecting two existing vertices |
| 51 | |
| 52 | """ |
| 53 | |
| 54 | showverts = True |
| 55 | epsilon = 5 # max pixel distance to count as a vertex hit |
| 56 | |
| 57 | def __init__(self, ax, poly): |
| 58 | if poly.figure is None: |
| 59 | raise RuntimeError('You must first add the polygon to a figure ' |
| 60 | 'or canvas before defining the interactor') |
| 61 | self.ax = ax |
| 62 | canvas = poly.figure.canvas |
| 63 | self.poly = poly |
| 64 | |
| 65 | x, y = zip(*self.poly.xy) |
| 66 | self.line = Line2D(x, y, |
| 67 | marker='o', markerfacecolor='r', |
| 68 | animated=True) |
| 69 | self.ax.add_line(self.line) |
| 70 | |
| 71 | self.cid = self.poly.add_callback(self.poly_changed) |
| 72 | self._ind = None # the active vert |
| 73 | |
| 74 | canvas.mpl_connect('draw_event', self.on_draw) |
| 75 | canvas.mpl_connect('button_press_event', self.on_button_press) |
| 76 | canvas.mpl_connect('key_press_event', self.on_key_press) |
| 77 | canvas.mpl_connect('button_release_event', self.on_button_release) |
| 78 | canvas.mpl_connect('motion_notify_event', self.on_mouse_move) |
| 79 | self.canvas = canvas |
| 80 | |
| 81 | def on_draw(self, event): |
| 82 | self.background = self.canvas.copy_from_bbox(self.ax.bbox) |
| 83 | self.ax.draw_artist(self.poly) |
| 84 | self.ax.draw_artist(self.line) |
| 85 | # do not need to blit here, this will fire before the screen is |
| 86 | # updated |
| 87 | |
| 88 | def poly_changed(self, poly): |
| 89 | """This method is called whenever the pathpatch object is called.""" |
| 90 | # only copy the artist props to the line (except visibility) |
| 91 | vis = self.line.get_visible() |
| 92 | Artist.update_from(self.line, poly) |
| 93 | self.line.set_visible(vis) # don't use the poly visibility state |
| 94 | |
| 95 | def get_ind_under_point(self, event): |
no outgoing calls
no test coverage detected
searching dependent graphs…