|
10 | 10 | import numpy.testing as np_test
|
11 | 11 | from numpy.testing import assert_almost_equal, assert_array_equal
|
12 | 12 | from numpy.testing import assert_array_almost_equal
|
13 |
| -from matplotlib.transforms import Affine2D, BlendedGenericTransform, Bbox |
| 13 | +from matplotlib.transforms import (Affine2D, BlendedGenericTransform, Bbox, |
| 14 | + TransformedPath, TransformedPatchPath) |
14 | 15 | from matplotlib.path import Path
|
15 | 16 | from matplotlib.scale import LogScale
|
16 | 17 | from matplotlib.testing.decorators import cleanup, image_comparison
|
@@ -576,6 +577,47 @@ def test_invalid_arguments():
|
576 | 577 | assert_raises(RuntimeError, t.transform, [[1, 2, 3]])
|
577 | 578 |
|
578 | 579 |
|
| 580 | +def test_transformed_path(): |
| 581 | + points = [(0, 0), (1, 0), (1, 1), (0, 1)] |
| 582 | + codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] |
| 583 | + path = Path(points, codes) |
| 584 | + |
| 585 | + trans = mtrans.Affine2D() |
| 586 | + trans_path = TransformedPath(path, trans) |
| 587 | + assert np.allclose(trans_path.get_fully_transformed_path().vertices, |
| 588 | + points) |
| 589 | + |
| 590 | + # Changing the transform should change the result. |
| 591 | + r2 = 1 / np.sqrt(2) |
| 592 | + trans.rotate(np.pi / 4) |
| 593 | + assert np.allclose(trans_path.get_fully_transformed_path().vertices, |
| 594 | + [(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)]) |
| 595 | + |
| 596 | + # Changing the path does not change the result (it's cached). |
| 597 | + path.points = [(0, 0)] * 4 |
| 598 | + assert np.allclose(trans_path.get_fully_transformed_path().vertices, |
| 599 | + [(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)]) |
| 600 | + |
| 601 | + |
| 602 | +def test_transformed_patch_path(): |
| 603 | + trans = mtrans.Affine2D() |
| 604 | + patch = mpatches.Wedge((0, 0), 1, 45, 135, transform=trans) |
| 605 | + |
| 606 | + tpatch = TransformedPatchPath(patch) |
| 607 | + points = tpatch.get_fully_transformed_path().vertices |
| 608 | + |
| 609 | + # Changing the transform should change the result. |
| 610 | + trans.scale(2) |
| 611 | + assert np.allclose(tpatch.get_fully_transformed_path().vertices, |
| 612 | + points * 2) |
| 613 | + |
| 614 | + # Changing the path should change the result (and cancel out the scaling |
| 615 | + # from the transform). |
| 616 | + patch.set_radius(0.5) |
| 617 | + assert np.allclose(tpatch.get_fully_transformed_path().vertices, |
| 618 | + points) |
| 619 | + |
| 620 | + |
579 | 621 | if __name__ == '__main__':
|
580 | 622 | import nose
|
581 | 623 | nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
|
0 commit comments