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

Skip to content

Commit 8acb14f

Browse files
authored
Merge pull request #9645 from yymao/patch-1
expose Path.contains_points as a method of Patch
2 parents 37978a8 + 05f6999 commit 8acb14f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/matplotlib/patches.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,30 @@ def contains(self, mouseevent, radius=None):
143143

144144
def contains_point(self, point, radius=None):
145145
"""
146-
Returns *True* if the given point is inside the path
146+
Returns ``True`` if the given *point* is inside the path
147147
(transformed with its transform attribute).
148+
149+
*radius* allows the path to be made slightly larger or smaller.
148150
"""
149151
radius = self._process_radius(radius)
150152
return self.get_path().contains_point(point,
151153
self.get_transform(),
152154
radius)
153155

156+
def contains_points(self, points, radius=None):
157+
"""
158+
Returns a bool array which is ``True`` if the (closed) path
159+
contains the corresponding point.
160+
(transformed with its transform attribute).
161+
162+
*points* must be Nx2 array.
163+
*radius* allows the path to be made slightly larger or smaller.
164+
"""
165+
radius = self._process_radius(radius)
166+
return self.get_path().contains_points(points,
167+
self.get_transform(),
168+
radius)
169+
154170
def update_from(self, other):
155171
"""
156172
Updates this :class:`Patch` from the properties of *other*.

lib/matplotlib/tests/test_patches.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,27 @@ def test_datetime_datetime_fails():
386386

387387
with pytest.raises(TypeError):
388388
mpatches.Rectangle((0, start), 1, dt_delta)
389+
390+
391+
def test_contains_point():
392+
ell = mpatches.Ellipse((0.5, 0.5), 0.5, 1.0, 0)
393+
points = [(0.0, 0.5), (0.2, 0.5), (0.25, 0.5), (0.5, 0.5)]
394+
path = ell.get_path()
395+
transform = ell.get_transform()
396+
radius = ell._process_radius(None)
397+
expected = np.array([path.contains_point(point,
398+
transform,
399+
radius) for point in points])
400+
result = np.array([ell.contains_point(point) for point in points])
401+
assert np.all(result == expected)
402+
403+
404+
def test_contains_points():
405+
ell = mpatches.Ellipse((0.5, 0.5), 0.5, 1.0, 0)
406+
points = [(0.0, 0.5), (0.2, 0.5), (0.25, 0.5), (0.5, 0.5)]
407+
path = ell.get_path()
408+
transform = ell.get_transform()
409+
radius = ell._process_radius(None)
410+
expected = path.contains_points(points, transform, radius)
411+
result = ell.contains_points(points)
412+
assert np.all(result == expected)

0 commit comments

Comments
 (0)