Skip to content

Commit 501436f

Browse files
author
Johann Krauter
committed
get_vertices and get_co_vertices returns now the coordinates of the major and minor axis depending on the length between the coordinates of points
1 parent 49d89d1 commit 501436f

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

lib/matplotlib/patches.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,33 +1654,59 @@ def get_corners(self):
16541654
return self.get_patch_transform().transform(
16551655
[(-1, -1), (1, -1), (1, 1), (-1, 1)])
16561656

1657+
def _calculate_length_between_points(self, x0, y0, x1, y1):
1658+
return np.sqrt((x1 - x0)**2 + (y1 - y0)**2)
1659+
1660+
def _calculate_vertices_coordinates(self, return_major: bool = True):
1661+
# calculate the vertices of width axis
1662+
w_x0 = self._center[0] - self._width / 2 * np.cos(np.deg2rad(self._angle))
1663+
w_y0 = self._center[1] - self._width / 2 * np.sin(np.deg2rad(self._angle))
1664+
w_x1 = self._center[0] + self._width / 2 * np.cos(np.deg2rad(self._angle))
1665+
w_y1 = self._center[1] + self._width / 2 * np.sin(np.deg2rad(self._angle))
1666+
1667+
# calculate the vertices of height axis
1668+
h_x0 = self._center[0] - self._height / 2 * np.sin(np.deg2rad(self._angle))
1669+
h_y0 = self._center[1] + self._height / 2 * np.cos(np.deg2rad(self._angle))
1670+
h_x1 = self._center[0] + self._height / 2 * np.sin(np.deg2rad(self._angle))
1671+
h_y1 = self._center[1] - self._height / 2 * np.cos(np.deg2rad(self._angle))
1672+
1673+
if self._calculate_length_between_points(
1674+
w_x0, w_y0, w_x1, w_y1
1675+
) >= self._calculate_length_between_points(
1676+
h_x0, h_y0, h_x1, h_y1
1677+
): # width is major
1678+
major = [(w_x0, w_y0), (w_x1, w_y1)]
1679+
minor = [(h_x0, h_y0), (h_x1, h_y1)]
1680+
else: # minor
1681+
major = [(h_x0, h_y0), (h_x1, h_y1)]
1682+
minor = [(w_x0, w_y0), (w_x1, w_y1)]
1683+
1684+
if return_major:
1685+
coordinates = major
1686+
else:
1687+
coordinates = minor
1688+
1689+
return coordinates
1690+
16571691
def get_vertices(self):
16581692
"""
1659-
Return the left and right vertex coordinates of the ellipse.
1693+
Return the vertices coordinates of the ellipse.
16601694
16611695
The definition can be found `here <https://en.wikipedia.org/wiki/Ellipse>`_
16621696
16631697
.. versionadded:: 3.8
16641698
"""
1665-
x0 = self._center[0] - self._width / 2 * np.cos(np.deg2rad(self._angle))
1666-
y0 = self._center[1] - self._width / 2 * np.sin(np.deg2rad(self._angle))
1667-
x1 = self._center[0] + self._width / 2 * np.cos(np.deg2rad(self._angle))
1668-
y1 = self._center[1] + self._width / 2 * np.sin(np.deg2rad(self._angle))
1669-
return [(x0, y0), (x1, y1)]
1699+
return self._calculate_vertices_coordinates()
16701700

16711701
def get_co_vertices(self):
16721702
"""
1673-
Return the left and right co-vertex coordinates of the ellipse.
1703+
Return the co-vertices coordinates of the ellipse.
16741704
16751705
The definition can be found `here <https://en.wikipedia.org/wiki/Ellipse>`_
16761706
16771707
.. versionadded:: 3.8
16781708
"""
1679-
x0 = self._center[0] - self._height / 2 * np.sin(np.deg2rad(self._angle))
1680-
y0 = self._center[1] + self._height / 2 * np.cos(np.deg2rad(self._angle))
1681-
x1 = self._center[0] + self._height / 2 * np.sin(np.deg2rad(self._angle))
1682-
y1 = self._center[1] - self._height / 2 * np.cos(np.deg2rad(self._angle))
1683-
return [(x0, y0), (x1, y1)]
1709+
return self._calculate_vertices_coordinates(return_major=False)
16841710

16851711

16861712
class Annulus(Patch):

0 commit comments

Comments
 (0)