Skip to content

Commit d29e9dc

Browse files
committed
Fix TransformedBbox.{,full_}contains.
... by checking whether the inverse-transformed the query point is in the untransformed bbox.
1 parent 10e8bf1 commit d29e9dc

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/matplotlib/tests/test_transforms.py

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import matplotlib.pyplot as plt
1010
import matplotlib.patches as mpatches
1111
import matplotlib.transforms as mtransforms
12+
from matplotlib.transforms import Affine2D, Bbox, TransformedBbox
1213
from matplotlib.path import Path
1314
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1415

@@ -744,3 +745,14 @@ def test_scale_swapping(fig_test, fig_ref):
744745
ax.plot(x, np.exp(-(x**2) / 2) / np.sqrt(2 * np.pi))
745746
fig.canvas.draw()
746747
ax.set_yscale('linear')
748+
749+
750+
def test_transformedbbox_contains():
751+
bb = TransformedBbox(Bbox.unit(), Affine2D().rotate_deg(30))
752+
assert bb.contains(.8, .5)
753+
assert bb.contains(-.4, .85)
754+
assert not bb.contains(.9, .5)
755+
bb = TransformedBbox(Bbox.unit(), Affine2D().translate(.25, .5))
756+
assert bb.contains(1.25, 1.5)
757+
assert not bb.fully_contains(1.25, 1.5)
758+
assert not bb.fully_contains(.1, .1)

lib/matplotlib/transforms.py

+8
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,14 @@ def get_points(self):
11381138
self._check(points)
11391139
return points
11401140

1141+
def contains(self, x, y):
1142+
# Docstring inherited.
1143+
return self._bbox.contains(*self._transform.inverted().transform((x, y)))
1144+
1145+
def fully_contains(self, x, y):
1146+
# Docstring inherited.
1147+
return self._bbox.fully_contains(*self._transform.inverted().transform((x, y)))
1148+
11411149

11421150
class LockableBbox(BboxBase):
11431151
"""

0 commit comments

Comments
 (0)