A Numpy Python csomag segítségével lehetőségünk van mátrixokkal műveleteket végezni. Transzformációs mátrixok összeállítását és vizsgálatát mutatja be a 04_01_np_geometric_trf_matrices.py példaprogram. A program az AffinTranszformaciok.xhtml példaprogramban látott számításokat hajtja végre.
Megadjuk a tetszőleges pont körüli elforgatás paramétereit. Ezután meghatározzuk az elemi merev transzformációs mátrixokat (t1, R, t2). Mátrix szorzással előállítjuk a kompozit transzformációs mátrixot, és kiírjuk az adatait. Teszteljük a transzformáció hatását kiválasztott síkbeli pontokra.
import numpy as np
# Center of rotation:
rx = 100
ry = 100
# Rotation angle:
theta = np.radians(30)
t1 = np.array([
[1, 0, -rx],
[0, 1, -ry],
[0, 0, 1]
])
# Manual rotational transformation definition
# R = np.array([[ 0.8660254, -0.5, 0.0],
# [ 0.5, 0.8660254, 0.0],
# [ 0, 0, 1]])
# Alternative solution:
# https://scipython.com/book/chapter-6-numpy/examples/creating-a-rotation-matrix-in-numpy/
c, s = np.cos(theta), np.sin(theta)
R = np.matrix('{} {} 0.0; {} {} 0.0; 0.0 0.0 1.0'.format(c, -s, s, c))
# print(R)
t2 = np.array([
[1, 0, rx],
[0, 1, ry],
[0, 0, 1]
])
print('Transformation matrix:')
M = np.matmul(t2, np.matmul(R, t1))
print(M)
print('')
print('Determinant of rotation submatrix:')
print( np.linalg.det( M[0:2,0:2] ) )
print('')
# Rotating around point (100, 100) keeps this point in place
print('Rotating around point (100, 100) keeps this point (100, 100) in place:')
p = np.array([100, 100, 1])
print(M.dot(p))
print('')
# Let's try other coordinates!
print('Other coordinates:')
p = np.array([0, 100, 1])
print(M.dot(p))