Skip to content

Add corner coordinate helper methods to Ellipse/Rectangle #21977

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
merged 1 commit into from
Jan 5, 2022
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
22 changes: 22 additions & 0 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,18 @@ def get_xy(self):
"""Return the left and bottom coords of the rectangle as a tuple."""
return self._x0, self._y0

def get_corners(self):
"""
Return the corners of the rectangle, moving anti-clockwise from
(x0, y0).
"""
return self.get_patch_transform().transform(
[(0, 0), (1, 0), (1, 1), (0, 1)])

def get_center(self):
"""Return the centre of the rectangle."""
return self.get_patch_transform().transform((0.5, 0.5))

def get_width(self):
"""Return the width of the rectangle."""
return self._width
Expand Down Expand Up @@ -1657,6 +1669,16 @@ def get_angle(self):

angle = property(get_angle, set_angle)

def get_corners(self):
"""
Return the corners of the ellipse bounding box.

The bounding box orientation is moving anti-clockwise from the
lower left corner defined before rotation.
"""
return self.get_patch_transform().transform(
[(-1, -1), (1, -1), (1, 1), (-1, 1)])


class Annulus(Patch):
"""
Expand Down
50 changes: 49 additions & 1 deletion lib/matplotlib/tests/test_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

import matplotlib as mpl
from matplotlib.patches import (Annulus, Patch, Polygon, Rectangle,
from matplotlib.patches import (Annulus, Ellipse, Patch, Polygon, Rectangle,
FancyArrowPatch)
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from matplotlib.transforms import Bbox
Expand Down Expand Up @@ -54,6 +54,54 @@ def test_Polygon_close():
assert_array_equal(p.get_xy(), xyclosed)


def test_corner_center():
loc = [10, 20]
width = 1
height = 2

# Rectangle
# No rotation
corners = ((10, 20), (11, 20), (11, 22), (10, 22))
rect = Rectangle(loc, width, height)
assert_array_equal(rect.get_corners(), corners)
assert_array_equal(rect.get_center(), (10.5, 21))

# 90 deg rotation
corners_rot = ((10, 20), (10, 21), (8, 21), (8, 20))
rect.set_angle(90)
assert_array_equal(rect.get_corners(), corners_rot)
assert_array_equal(rect.get_center(), (9, 20.5))

# Rotation not a multiple of 90 deg
theta = 33
t = mtransforms.Affine2D().rotate_around(*loc, np.deg2rad(theta))
corners_rot = t.transform(corners)
rect.set_angle(theta)
assert_almost_equal(rect.get_corners(), corners_rot)

# Ellipse
loc = [loc[0] + width / 2,
loc[1] + height / 2]
ellipse = Ellipse(loc, width, height)

# No rotation
assert_array_equal(ellipse.get_corners(), corners)

# 90 deg rotation
corners_rot = ((11.5, 20.5), (11.5, 21.5), (9.5, 21.5), (9.5, 20.5))
ellipse.set_angle(90)
assert_array_equal(ellipse.get_corners(), corners_rot)
# Rotation shouldn't change ellipse center
assert_array_equal(ellipse.get_center(), loc)

# Rotation not a multiple of 90 deg
theta = 33
t = mtransforms.Affine2D().rotate_around(*loc, np.deg2rad(theta))
corners_rot = t.transform(corners)
ellipse.set_angle(theta)
assert_almost_equal(ellipse.get_corners(), corners_rot)


def test_rotate_rect():
loc = np.asarray([1.0, 2.0])
width = 2
Expand Down