| 1 | from math import sin, cos, radians |
| 2 | from unittest import TestCase |
| 3 | from pgzero.actor import transform_anchor |
| 4 | |
| 5 | root2 = 2 ** 0.5 |
| 6 | |
| 7 | |
| 8 | def assertVecEqual(a, b, decimal_places=7): |
| 9 | for t in (a, b): |
| 10 | if not isinstance(t, tuple): |
| 11 | raise AssertionError('%r is not a tuple' % t) |
| 12 | if len(t) != 2: |
| 13 | raise AssertionError('Expected 2-tuple, not %r' % t) |
| 14 | |
| 15 | ax, ay = a |
| 16 | bx, by = b |
| 17 | epsilon = 10 ** -decimal_places |
| 18 | if abs(ax - bx) > epsilon or abs(ay - by) > epsilon: |
| 19 | raise AssertionError('%r != %r (to %d decimal places)' % ( |
| 20 | a, b, decimal_places |
| 21 | )) |
| 22 | |
| 23 | |
| 24 | class TransformAnchorTest(TestCase): |
| 25 | def test_identity(self): |
| 26 | assertVecEqual( |
| 27 | transform_anchor(5, 5, 10, 10, 0), |
| 28 | (5, 5) |
| 29 | ) |
| 30 | |
| 31 | def test_45deg(self): |
| 32 | assertVecEqual( |
| 33 | transform_anchor(5, 5, 10, 10, 45), |
| 34 | (5 * root2, 5 * root2) |
| 35 | ) |
| 36 | |
| 37 | def test_45deg_offset(self): |
| 38 | assertVecEqual( |
| 39 | transform_anchor(0, 0.5, 1, 1, 45), |
| 40 | (0.25 * root2, 0.75 * root2) |
| 41 | ) |