@@ -81,7 +81,7 @@ def segment_hits(cx, cy, x, y, radius):
81
81
"""
82
82
# Process single points specially
83
83
if len (x ) < 2 :
84
- res , = np .nonzero ((cx - x ) ** 2 + ( cy - y ) ** 2 <= radius ** 2 )
84
+ res , = np .nonzero (np . hypot (cx - x , cy - y ) <= radius )
85
85
return res
86
86
87
87
# We need to lop the last element off a lot.
@@ -93,25 +93,21 @@ def segment_hits(cx, cy, x, y, radius):
93
93
Lnorm_sq = dx ** 2 + dy ** 2 # Possibly want to eliminate Lnorm==0
94
94
u = ((cx - xr ) * dx + (cy - yr ) * dy ) / Lnorm_sq
95
95
candidates = (u >= 0 ) & (u <= 1 )
96
- #if any(candidates): print "candidates",xr[candidates]
97
96
98
97
# Note that there is a little area near one side of each point
99
98
# which will be near neither segment, and another which will
100
99
# be near both, depending on the angle of the lines. The
101
100
# following radius test eliminates these ambiguities.
102
- point_hits = (cx - x ) ** 2 + (cy - y ) ** 2 <= radius ** 2
103
- #if any(point_hits): print "points",xr[candidates]
101
+ points_hit = np .hypot (cx - x , cy - y ) <= radius
104
102
candidates = candidates & ~ (point_hits [:- 1 ] | point_hits [1 :])
105
103
106
104
# For those candidates which remain, determine how far they lie away
107
105
# from the line.
108
106
px , py = xr + u * dx , yr + u * dy
109
- line_hits = (cx - px ) ** 2 + (cy - py ) ** 2 <= radius ** 2
110
- #if any(line_hits): print "lines",xr[candidates]
107
+ line_hits = np .hypot (cx - px , cy - py ) <= radius
111
108
line_hits = line_hits & candidates
112
109
points , = point_hits .ravel ().nonzero ()
113
110
lines , = line_hits .ravel ().nonzero ()
114
- #print points,lines
115
111
return np .concatenate ((points , lines ))
116
112
117
113
@@ -484,17 +480,15 @@ def contains(self, mouseevent):
484
480
else :
485
481
pixels = self .figure .dpi / 72. * self .pickradius
486
482
487
- # the math involved in checking for containment (here and inside of
488
- # segment_hits) assumes that it is OK to overflow. In case the
489
- # application has set the error flags such that an exception is raised
490
- # on overflow, we temporarily set the appropriate error flags here and
491
- # set them back when we are finished.
483
+ # The math involved in checking for containment (here and inside of
484
+ # segment_hits) assumes that it is OK to overflow, so temporarily set
485
+ # the error flags accordingly.
492
486
with np .errstate (all = 'ignore' ):
493
487
# Check for collision
494
488
if self ._linestyle in ['None' , None ]:
495
489
# If no line, return the nearby point(s)
496
- d = ( xt - mouseevent . x ) ** 2 + ( yt - mouseevent . y ) ** 2
497
- ind , = np .nonzero ( np . less_equal ( d , pixels ** 2 ) )
490
+ ind , = np . nonzero (
491
+ np .hypot ( xt - mouseevent . x , yt - mouseevent . y ) <= pixels )
498
492
else :
499
493
# If line, return the nearby segment(s)
500
494
ind = segment_hits (mouseevent .x , mouseevent .y , xt , yt , pixels )
0 commit comments