Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
QuLogic merged 1 commit intomatplotlib:mainfromdstansby:patch-helpers
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionslib/matplotlib/patches.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionlib/matplotlib/tests/test_patches.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp