README.md: mention also the tetraflexagon example
[flexagon-toolkit.git] / contrib / affine_composition.py
1 #!/usr/bin/env python
2
3 from affine import Affine
4 from math import sin, cos, radians
5
6 src_x = 10
7 src_y = 10
8
9 angle = 30
10
11 dest_x = 20
12 dest_y = 30
13
14 # Some transform to compose
15 T1 = Affine.translation(-src_x, -src_y)
16 R = Affine.rotation(angle)
17 T2 = Affine.translation(dest_x, dest_y)
18
19 # Composition is performed by multiplying from right to left
20 matrix = T2 * R * T1
21 print [item for item in matrix]
22
23 theta = radians(angle)
24
25 # This is the equivalent transformation matrix
26 matrix = [
27     cos(theta), -sin(theta), -src_x * cos(theta) + src_y * sin(theta) + dest_x,
28     sin(theta),  cos(theta), -src_x * sin(theta) - src_y * cos(theta) + dest_y,
29            0.0,                              0.0,                          1.0
30 ]
31 print matrix