Skip to content

Commit

Permalink
Add test for rotated rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcdougall committed Nov 11, 2012
1 parent 1780ff5 commit 2b906ee
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/matplotlib/tests/test_patches.py
Expand Up @@ -2,8 +2,11 @@
Tests specific to the patches module.
"""

import numpy as np
from numpy.testing import assert_array_equal
from numpy.testing import assert_almost_equal
from matplotlib.patches import Polygon
from matplotlib.patches import Rectangle

def test_Polygon_close():
"""
Expand Down Expand Up @@ -40,3 +43,25 @@ def test_Polygon_close():
p.set_xy(xyclosed)
assert_array_equal(p.get_xy(), xyclosed)

def test_rotate_rect():
loc = np.asarray([1.0, 2.0])
width = 2
height = 3
angle = 30.0

# A rotated rectangle
rect1 = Rectangle(loc, width, height, angle=angle)

# A non-rotated rectangle
rect2 = Rectangle(loc, width, height)

# Set up an explicit rotation matrix (in radians)
angle_rad = np.pi * angle / 180.0
rotation_matrix = np.array([[np.cos(angle_rad), -np.sin(angle_rad)],
[np.sin(angle_rad), np.cos(angle_rad)]])

# Translate to origin, rotate each vertex, and then translate back
new_verts = np.inner(rotation_matrix, rect2.get_verts() - loc).T + loc

# They should be the same
assert_almost_equal(rect1.get_verts(), new_verts)

0 comments on commit 2b906ee

Please sign in to comment.