Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0466b93

Browse files
committed
Move methods to functions in poly_editor.py
1 parent ddf38fe commit 0466b93

1 file changed

Lines changed: 29 additions & 27 deletions

File tree

examples/event_handling/poly_editor.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@
1111
from matplotlib.artist import Artist
1212

1313

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+
1442
class PolygonInteractor(object):
1543
"""
1644
A polygon editor.
@@ -84,32 +112,6 @@ def get_ind_under_point(self, event):
84112

85113
return ind
86114

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-
113115
def button_press_callback(self, event):
114116
'whenever a mouse button is pressed'
115117
if not self.showverts:
@@ -149,7 +151,7 @@ def key_press_callback(self, event):
149151
for i in range(len(xys) - 1):
150152
s0 = xys[i]
151153
s1 = xys[i + 1]
152-
d = self.dist_point_to_segment(p, s0, s1)
154+
d = dist_point_to_segment(p, s0, s1)
153155
if d <= self.epsilon:
154156
self.poly.xy = np.insert(
155157
self.poly.xy, i+1,

0 commit comments

Comments
 (0)