|
2 | 2 | Tests specific to the patches module.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import numpy as np |
5 | 6 | from numpy.testing import assert_array_equal
|
| 7 | +from numpy.testing import assert_almost_equal |
6 | 8 | from matplotlib.patches import Polygon
|
| 9 | +from matplotlib.patches import Rectangle |
7 | 10 |
|
8 | 11 | def test_Polygon_close():
|
9 | 12 | """
|
@@ -40,3 +43,25 @@ def test_Polygon_close():
|
40 | 43 | p.set_xy(xyclosed)
|
41 | 44 | assert_array_equal(p.get_xy(), xyclosed)
|
42 | 45 |
|
| 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