Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
$$ M = \begin{pmatrix}\ a_{11} & a_{12} & 0 \\ a_{21} & a_{22} & 0 \\ a_{31} & a_{32} & 1\end{pmatrix}$$
$$ \operatorname{det} M = a_{11}\,a_{22} - a_{12}\,a_{21},\qquad \operatorname{det} M \ne 0$$
$$ p_1 = p_0 \cdot M = (x_0, y_0, 1) \begin{pmatrix}\ a_{11} & a_{12} & 0 \\ a_{21} & a_{22} & 0 \\ a_{31} & a_{32} & 1\end{pmatrix} $$
$$ M_c = R \cdot H_x \cdot S $$
$$ R = \begin{pmatrix}\ \cos \alpha & \sin \alpha & 0 \\ -\sin \alpha & \cos \alpha & 0 \\ 0 & 0 & 1\end{pmatrix}$$
$$ H_x = \begin{pmatrix}\ 1 & hx & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{pmatrix}$$
$$ S = \begin{pmatrix}\ sx & 0 & 0 \\ 0 & sy & 0 \\ 0 & 0 & 1\end{pmatrix}$$
$$ M = T_0 \cdot S \cdot H \cdot R \cdot T_0^{-1} \cdot T $$
$$ T_0 = \begin{pmatrix}\ 1 & 0 & 0 \\ 0 & 1 & 0 \\ -tx_0 & -ty_0 & 1\end{pmatrix}$$
$$ T_0^{-1} = \begin{pmatrix}\ 1 & 0 & 0 \\ 0 & 1 & 0 \\ tx_0 & ty_0 & 1\end{pmatrix}$$
$$ T = \begin{pmatrix}\ 1 & 0 & 0 \\ 0 & 1 & 0 \\ tx & ty & 1\end{pmatrix}$$
$$a_{11} = sx \cdot \cos \alpha - sx \cdot hx \cdot \sin \alpha$$
$$a_{12} = sx \cdot hx \cdot \cos \alpha + sx \cdot \sin \alpha$$
$$a_{21} = -sy \cdot \sin \alpha$$
$$a_{22} = sy \cdot \cos \alpha$$
$$a_{31} = tx + tx_0 \cdot (1 - (sx \cdot \cos \alpha - sx \cdot hx \cdot \sin \alpha)) + ty_0 \cdot sy \cdot \sin \alpha $$
$$= tx + tx_0 \cdot (1 - a_{11}) - ty_0 \cdot a_{21}$$
$$a_{32} = ty + ty_0 \cdot (1 - \cos \alpha \cdot sy) - tx_0 \cdot (sx \cdot hx \cdot \cos \alpha + sx \cdot \sin \alpha) $$
$$ = ty + ty_0 \cdot (1 - a_{22}) - tx_0 \cdot a_{12}$$
$$\operatorname{if} a_{22}=0 $$
$$ \alpha = \frac{\pi}{2},$$
$$ sy = -a_{21} $$
$$ \operatorname{else} $$
$$ \alpha = \operatorname{atan}\left(-\frac{a_{21}}{a_{22}}\right), $$
$$ sy = \frac{a_{22}}{\cos \alpha} $$
$$ sx = \frac{\operatorname{det}(M)}{sy}},$$
$$ hx = \frac{a_{11} \cdot a_{21} + a_{12} \cdot a_{22}}{\operatorname{det}(M)},$$
$$ tx = a_{31} + ty_0 \cdot a_{21} + tx_0 \cdot (a_{11} - 1),$$
$$ ty = a_{32} + tx_0 \cdot a{12} + ty_0 \cdot (a_{22} - 1).$$Technically its impossible to extract scale and rotation from a general transformation matrix, but in reality its generally easy. What does this mean? Well basically a general transformation matrix can have shear as well as scale as well as rotation. And all three of these pieces of data live in the upper 3×3 quadrant of the matrix. But if you know you’re just working with rotations, translations and scale (which in my professional life has always been the case – but perhaps others have different stories?) then its possible and in fact pretty easy.
Разложение матрицы аффинного преобразования