Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b906ee

Browse files
committed
Add test for rotated rectangle
1 parent 1780ff5 commit 2b906ee

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/matplotlib/tests/test_patches.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
Tests specific to the patches module.
33
"""
44

5+
import numpy as np
56
from numpy.testing import assert_array_equal
7+
from numpy.testing import assert_almost_equal
68
from matplotlib.patches import Polygon
9+
from matplotlib.patches import Rectangle
710

811
def test_Polygon_close():
912
"""
@@ -40,3 +43,25 @@ def test_Polygon_close():
4043
p.set_xy(xyclosed)
4144
assert_array_equal(p.get_xy(), xyclosed)
4245

46+
def test_rotate_rect():
47+
loc = np.asarray([1.0, 2.0])
48+
width = 2
49+
height = 3
50+
angle = 30.0
51+
52+
# A rotated rectangle
53+
rect1 = Rectangle(loc, width, height, angle=angle)
54+
55+
# A non-rotated rectangle
56+
rect2 = Rectangle(loc, width, height)
57+
58+
# Set up an explicit rotation matrix (in radians)
59+
angle_rad = np.pi * angle / 180.0
60+
rotation_matrix = np.array([[np.cos(angle_rad), -np.sin(angle_rad)],
61+
[np.sin(angle_rad), np.cos(angle_rad)]])
62+
63+
# Translate to origin, rotate each vertex, and then translate back
64+
new_verts = np.inner(rotation_matrix, rect2.get_verts() - loc).T + loc
65+
66+
# They should be the same
67+
assert_almost_equal(rect1.get_verts(), new_verts)

0 commit comments

Comments
 (0)