Skip to content

Backport PR #25902 on branch v3.7.x (Fix TransformedBbox.{,full_}contains.) #25943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.transforms as mtransforms
from matplotlib.transforms import Affine2D, Bbox, TransformedBbox
from matplotlib.path import Path
from matplotlib.testing.decorators import image_comparison, check_figures_equal

Expand Down Expand Up @@ -755,3 +756,14 @@ def test_offset_copy_errors():
with pytest.raises(ValueError,
match='For units of inches or points a fig kwarg is needed'):
mtransforms.offset_copy(None, units='inches')


def test_transformedbbox_contains():
bb = TransformedBbox(Bbox.unit(), Affine2D().rotate_deg(30))
assert bb.contains(.8, .5)
assert bb.contains(-.4, .85)
assert not bb.contains(.9, .5)
bb = TransformedBbox(Bbox.unit(), Affine2D().translate(.25, .5))
assert bb.contains(1.25, 1.5)
assert not bb.fully_contains(1.25, 1.5)
assert not bb.fully_contains(.1, .1)
8 changes: 8 additions & 0 deletions lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,14 @@ def get_points(self):
self._check(points)
return points

def contains(self, x, y):
# Docstring inherited.
return self._bbox.contains(*self._transform.inverted().transform((x, y)))

def fully_contains(self, x, y):
# Docstring inherited.
return self._bbox.fully_contains(*self._transform.inverted().transform((x, y)))


class LockableBbox(BboxBase):
"""
Expand Down