|
11 | 11 | from matplotlib.artist import Artist |
12 | 12 |
|
13 | 13 |
|
| 14 | +def dist(x, y): |
| 15 | + """ |
| 16 | + Return the distance between two points. |
| 17 | + """ |
| 18 | + d = x - y |
| 19 | + return np.sqrt(np.dot(d, d)) |
| 20 | + |
| 21 | + |
| 22 | +def dist_point_to_segment(p, s0, s1): |
| 23 | + """ |
| 24 | + Get the distance of a point to a segment. |
| 25 | + *p*, *s0*, *s1* are *xy* sequences |
| 26 | + This algorithm from |
| 27 | + http://geomalgorithms.com/a02-_lines.html |
| 28 | + """ |
| 29 | + v = s1 - s0 |
| 30 | + w = p - s0 |
| 31 | + c1 = np.dot(w, v) |
| 32 | + if c1 <= 0: |
| 33 | + return dist(p, s0) |
| 34 | + c2 = np.dot(v, v) |
| 35 | + if c2 <= c1: |
| 36 | + return dist(p, s1) |
| 37 | + b = c1 / c2 |
| 38 | + pb = s0 + b * v |
| 39 | + return dist(p, pb) |
| 40 | + |
| 41 | + |
14 | 42 | class PolygonInteractor(object): |
15 | 43 | """ |
16 | 44 | A polygon editor. |
@@ -84,32 +112,6 @@ def get_ind_under_point(self, event): |
84 | 112 |
|
85 | 113 | return ind |
86 | 114 |
|
87 | | - def dist(self, x, y): |
88 | | - """ |
89 | | - Return the distance between two points. |
90 | | - """ |
91 | | - d = x-y |
92 | | - return np.sqrt(np.dot(d, d)) |
93 | | - |
94 | | - def dist_point_to_segment(self, p, s0, s1): |
95 | | - """ |
96 | | - Get the distance of a point to a segment. |
97 | | - *p*, *s0*, *s1* are *xy* sequences |
98 | | - This algorithm from |
99 | | - http://geomalgorithms.com/a02-_lines.html |
100 | | - """ |
101 | | - v = s1 - s0 |
102 | | - w = p - s0 |
103 | | - c1 = np.dot(w, v) |
104 | | - if c1 <= 0: |
105 | | - return self.dist(p, s0) |
106 | | - c2 = np.dot(v, v) |
107 | | - if c2 <= c1: |
108 | | - return self.dist(p, s1) |
109 | | - b = c1 / c2 |
110 | | - pb = s0 + b * v |
111 | | - return self.dist(p, pb) |
112 | | - |
113 | 115 | def button_press_callback(self, event): |
114 | 116 | 'whenever a mouse button is pressed' |
115 | 117 | if not self.showverts: |
@@ -149,7 +151,7 @@ def key_press_callback(self, event): |
149 | 151 | for i in range(len(xys) - 1): |
150 | 152 | s0 = xys[i] |
151 | 153 | s1 = xys[i + 1] |
152 | | - d = self.dist_point_to_segment(p, s0, s1) |
| 154 | + d = dist_point_to_segment(p, s0, s1) |
153 | 155 | if d <= self.epsilon: |
154 | 156 | self.poly.xy = np.insert( |
155 | 157 | self.poly.xy, i+1, |
|
0 commit comments