-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Improve RectangleSelector rotation #21945
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
Draft
dstansby
wants to merge
5
commits into
matplotlib:main
Choose a base branch
from
dstansby:rect-rotation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+440
−324
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8eeae8e
Use assert_allclose in RectangleSelector tests
dstansby 923a90e
Add edge midpoint helper methods
dstansby ab8b1d7
Use display coordinates for RectangleSelector
dstansby d2d2ce1
Use multiple start/end corners for rectangle resize test
dstansby baea572
Update handles when canvas is resized
dstansby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -738,13 +738,6 @@ def __init__(self, xy, width, height, angle=0.0, *, | |
self._height = height | ||
self.angle = float(angle) | ||
self.rotation_point = rotation_point | ||
# Required for RectangleSelector with axes aspect ratio != 1 | ||
# The patch is defined in data coordinates and when changing the | ||
# selector with square modifier and not in data coordinates, we need | ||
# to correct for the aspect ratio difference between the data and | ||
# display coordinate systems. Its value is typically provide by | ||
# Axes._get_aspect_ratio() | ||
self._aspect_ratio_correction = 1.0 | ||
self._convert_units() # Validate the inputs. | ||
|
||
def get_path(self): | ||
|
@@ -772,13 +765,11 @@ def get_patch_transform(self): | |
rotation_point = bbox.x0, bbox.y0 | ||
else: | ||
rotation_point = self.rotation_point | ||
return transforms.BboxTransformTo(bbox) \ | ||
+ transforms.Affine2D() \ | ||
.translate(-rotation_point[0], -rotation_point[1]) \ | ||
.scale(1, self._aspect_ratio_correction) \ | ||
.rotate_deg(self.angle) \ | ||
.scale(1, 1 / self._aspect_ratio_correction) \ | ||
.translate(*rotation_point) | ||
return (transforms.BboxTransformTo(bbox) + | ||
transforms.Affine2D() | ||
.translate(-rotation_point[0], -rotation_point[1]) | ||
.rotate_deg(self.angle) | ||
.translate(*rotation_point)) | ||
|
||
@property | ||
def rotation_point(self): | ||
|
@@ -816,6 +807,14 @@ def get_corners(self): | |
return self.get_patch_transform().transform( | ||
[(0, 0), (1, 0), (1, 1), (0, 1)]) | ||
|
||
def _get_edge_midpoints(self): | ||
""" | ||
Return the edge midpoints of the rectangle, moving anti-clockwise from | ||
the center of the left-hand edge. | ||
""" | ||
return self.get_patch_transform().transform( | ||
[(0, 0.5), (0.5, 0), (1, 0.5), (0.5, 1)]) | ||
|
||
def get_center(self): | ||
"""Return the centre of the rectangle.""" | ||
return self.get_patch_transform().transform((0.5, 0.5)) | ||
|
@@ -1565,12 +1564,6 @@ def __init__(self, xy, width, height, angle=0, **kwargs): | |
self._width, self._height = width, height | ||
self._angle = angle | ||
self._path = Path.unit_circle() | ||
# Required for EllipseSelector with axes aspect ratio != 1 | ||
# The patch is defined in data coordinates and when changing the | ||
# selector with square modifier and not in data coordinates, we need | ||
# to correct for the aspect ratio difference between the data and | ||
# display coordinate systems. | ||
self._aspect_ratio_correction = 1.0 | ||
# Note: This cannot be calculated until this is added to an Axes | ||
self._patch_transform = transforms.IdentityTransform() | ||
|
||
|
@@ -1588,9 +1581,8 @@ def _recompute_transform(self): | |
width = self.convert_xunits(self._width) | ||
height = self.convert_yunits(self._height) | ||
self._patch_transform = transforms.Affine2D() \ | ||
.scale(width * 0.5, height * 0.5 * self._aspect_ratio_correction) \ | ||
.scale(width * 0.5, height * 0.5) \ | ||
.rotate_deg(self.angle) \ | ||
.scale(1, 1 / self._aspect_ratio_correction) \ | ||
.translate(*center) | ||
|
||
def get_path(self): | ||
|
@@ -1681,6 +1673,14 @@ def get_corners(self): | |
return self.get_patch_transform().transform( | ||
[(-1, -1), (1, -1), (1, 1), (-1, 1)]) | ||
|
||
def _get_edge_midpoints(self): | ||
""" | ||
Return the corners of the ellipse, moving anti-clockwise from | ||
the center of the left-hand edge before rotation. | ||
""" | ||
return self.get_patch_transform().transform( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to have test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
[(-1, 0), (0, -1), (1, 0), (0, 1)]) | ||
|
||
|
||
class Annulus(Patch): | ||
""" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to have test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is private API, and is implicitly tested by a lot of the selector tests. If the points were somehow wrong here, the handles would be in the wrong place, and if they were correct but in the wrong order the resizing logic would not work as intended.