MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / segment_hits

Function segment_hits

lib/matplotlib/lines.py:98–132  ·  view source on GitHub ↗

Return the indices of the segments in the polyline with coordinates (*cx*, *cy*) that are within a distance *radius* of the point (*x*, *y*).

(cx, cy, x, y, radius)

Source from the content-addressed store, hash-verified

96
97
98def segment_hits(cx, cy, x, y, radius):
99 """
100 Return the indices of the segments in the polyline with coordinates (*cx*,
101 *cy*) that are within a distance *radius* of the point (*x*, *y*).
102 """
103 # Process single points specially
104 if len(x) <= 1:
105 res, = np.nonzero((cx - x) ** 2 + (cy - y) ** 2 <= radius ** 2)
106 return res
107
108 # We need to lop the last element off a lot.
109 xr, yr = x[:-1], y[:-1]
110
111 # Only look at line segments whose nearest point to C on the line
112 # lies within the segment.
113 dx, dy = x[1:] - xr, y[1:] - yr
114 Lnorm_sq = dx ** 2 + dy ** 2 # Possibly want to eliminate Lnorm==0
115 u = ((cx - xr) * dx + (cy - yr) * dy) / Lnorm_sq
116 candidates = (u >= 0) & (u <= 1)
117
118 # Note that there is a little area near one side of each point
119 # which will be near neither segment, and another which will
120 # be near both, depending on the angle of the lines. The
121 # following radius test eliminates these ambiguities.
122 point_hits = (cx - x) ** 2 + (cy - y) ** 2 <= radius ** 2
123 candidates = candidates & ~(point_hits[:-1] | point_hits[1:])
124
125 # For those candidates which remain, determine how far they lie away
126 # from the line.
127 px, py = xr + u * dx, yr + u * dy
128 line_hits = (cx - px) ** 2 + (cy - py) ** 2 <= radius ** 2
129 line_hits = line_hits & candidates
130 points, = point_hits.ravel().nonzero()
131 lines, = line_hits.ravel().nonzero()
132 return np.concatenate((points, lines))
133
134
135def _mark_every_path(markevery, tpath, affine, ax):

Callers 1

containsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…