Article directory
1. Translation
In 2D space, we often need to translate one point to another. Suppose a point P ( x , y ) P(x,y) in the spaceP(x,y ) ; direct it tox , yx, yx,Translatetx t_x in the y directiontx, t y t_y ty, assuming that the coordinates of the point after translation are ( x ′ , y ′ ) (x',y')(x′,y′ ), then the translation operation of the above points can be summarized as the following formula:
x ′ = x + txy ′ = x + ty \begin{alignat}{2} &x'=x + t_x\\ &y'=x + t_y \end {alignat}x′=x+txy′=x+ty
Using a homogeneous matrix is expressed as follows:
[ x ′ y ′ 1 ] = [ 1 btx 0 1 ty 0 0 1 ] [ xy 1 ] \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \ begin{bmatrix} 1 & b & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}
x′y′1
=
100b10txty1
xy1
import numpy as np
def translation():
"""
原始数组a 三个点(1,1) (4,4) (7,7)
构建齐次矩阵 P
构建变换矩阵 T
"""
a = np.array([[1, 1],
[4, 4],
[7, 7]])
P = np.array([a[:, 0],
a[:, 1],
np.ones(len(a))])
T = np.array([[1, 0, 2],
[0, 1, 2],
[0, 0, 1]])
return np.dot(T, P)
print(translation())
"""
[[3. 6. 9.]
[3. 6. 9.]
[1. 1. 1.]]
"""
Animation Effect Demonstration
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
X, Y = np.mgrid[0:1:5j, 0:1:5j]
x, y = X.ravel(), Y.ravel()
def trans_translate(x, y, tx, ty):
T = [[1, 0, tx],
[0, 1, ty],
[0, 0, 1]]
T = np.array(T)
P = np.array([x, y, [1] * x.size])
return np.dot(T, P)
fig, ax = plt.subplots(1, 4)
T_ = [[0, 0], [2.3, 0], [0, 1.7], [2, 2]]
for i in range(4):
tx, ty = T_[i]
x_, y_, _ = trans_translate(x, y, tx, ty)
ax[i].scatter(x_, y_)
ax[i].set_title(r'$t_x={0:.2f}$ , $t_y={1:.2f}$'.format(tx, ty))
ax[i].set_xlim([-0.5, 4])
ax[i].set_ylim([-0.5, 4])
ax[i].grid(alpha=0.5)
ax[i].axhline(y=0, color='k')
ax[i].axvline(x=0, color='k')
plt.show()
2. Scaling
In 2D space, a point ( x , y ) (x,y)(x,y ) relative to another point( px , py ) (p_x,p_y)(px,py) for scaling operation, we might as well scale the factor inx, yx, yThe x and y directions are respectively:sx , sy s_x, s_ysx,sy, then the above scaling operation can be summarized as the following formula:
x ′ = sx ( x − px ) + px = sxx + px ( 1 − sx ) y ′ = sy ( y − py ) + py = syy + py ( 1 − sy ) \begin{alignat}{2} &x'=s_x(x-p_x) + p_x &=s_xx + p_x(1-s_x)\\ &y'=s_y(y-p_y) + p_y &=s_yy + p_y( 1-s_y) \end{alignat}x′=sx(x−px)+pxy′=sy(y−py)+py=sxx+px(1−sx)=syy+py(1−sy)
Using a homogeneous matrix is expressed as follows:
[ x ′ y ′ 1 ] = [ sx 0 px ( 1 − sx ) 0 sypy ( 1 − sy ) 0 0 1 ] [ xy 1 ] \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} s_x& 0 & p_x(1-s_x) \\ 0 & s_y& p_y(1-s_y)\\ 0 & 0& 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}
x′y′1
=
sx000sy0px(1−sx)py(1−sy)1
xy1
def trans_scale(x, y, px, py, sx, sy):
T = [[sx, 0 , px*(1 - sx)],
[0 , sy, py*(1 - sy)],
[0 , 0 , 1 ]]
T = np.array(T)
P = np.array([x, y, [1]*x.size])
return np.dot(T, P)
fig, ax = plt.subplots(1, 4)
S_ = [[1, 1], [1.8, 1], [1, 1.7], [2, 2]]
P_ = [[0, 0], [0, 0], [0.45, 0.45], [1.1, 1.1]]
for i in range(4):
sx, sy = S_[i]; px, py = P_[i]
x_, y_, _ = trans_scale(x, y, px, py, sx, sy)
ax[i].scatter(x_, y_)
ax[i].scatter(px, py)
ax[i].set_title(r'$p_x={0:.2f}$ , $p_y={1:.2f}$'.format(px, py) + '\n'
r'$s_x={0:.2f}$ , $s_y={1:.2f}$'.format(sx, sy))
ax[i].set_xlim([-2, 2])
ax[i].set_ylim([-2, 2])
ax[i].grid(alpha=0.5)
ax[i].axhline(y=0, color='k')
ax[i].axvline(x=0, color='k')
plt.show()
3. Rotation
In 2D space, for a point ( x , y ) (x,y)(x,y ) relative to another point( px , py ) (p_x,p_y)(px,py) for rotation operation, generally counterclockwise is positive, clockwise is negative, assuming the rotation angle isβ \betaβ , then the above pointsx , yx,yx,y relative to pointpx, py p_x,p_ypx,pyThe rotation angle β \betaDefine the β function as a function:
[ x ′ y ′ 1 ] = [ cos β − sin β px ( 1 − cos β ) + py sin β sin β cos β py ( 1 − cos β ) + px sin β 0 0 1 ] [ xy 1 ] \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} \cos \beta& -\sin \beta & p_x(1-\cos \beta) + p_y \sin \beta \\ \sin \beta & \cos \beta& p_y(1-\cos \beta) + p_x \sin \beta \\ 0 & 0& 1 \end {bmatrix} \begin{bmatrix}x\\y\\1\end{bmatrix}
x′y′1
=
cosbsinb0−sinbcosb0px(1−cosb )+pysinbpy(1−cosb )+pxsinb1
xy1
def trans_rotate(x, y, px, py, beta):
beta = np.deg2rad(beta)
T = [[np.cos(beta), -np.sin(beta), px*(1 - np.cos(beta)) + py*np.sin(beta)],
[np.sin(beta), np.cos(beta), py*(1 - np.cos(beta)) - px*np.sin(beta)],
[0 , 0 , 1 ]]
T = np.array(T)
P = np.array([x, y, [1]*x.size])
return np.dot(T, P)
fig, ax = plt.subplots(1, 4)
R_ = [0, 225, 40, -10]
P_ = [[0, 0], [0, 0], [0.5, -0.5], [1.1, 1.1]]
for i in range(4):
beta = R_[i]; px, py = P_[i]
x_, y_, _ = trans_rotate(x, y, px, py, beta)
ax[i].scatter(x_, y_)
ax[i].scatter(px, py)
ax[i].set_title(r'$\beta={0}°$ , $p_x={1:.2f}$ , $p_y={2:.2f}$'.format(beta, px, py))
ax[i].set_xlim([-2, 2])
ax[i].set_ylim([-2, 2])
ax[i].grid(alpha=0.5)
ax[i].axhline(y=0, color='k')
ax[i].axvline(x=0, color='k')
plt.show()
4. Shearing
In 2D space, for a point ( x , y ) (x,y)(x,y ) relative to another point( px , py ) (p_x,p_y)(px,py) for miscut operation, which is generally used for deformation processing of elastic objects. Assume that the miscutting parameters along the x direction and y direction areλ x , λ y \lambda _x, \lambda _ylx, ly, then the miscutting operation can be summarized and expressed as a homogeneous matrix as follows:
[ x ′ y ′ 1 ] = [ 1 λ x − λ xpx λ y 1 − λ ypy 0 0 1 ] [ xy 1 ] \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \ begin{bmatrix} 1& \lambda _x & -\lambda _x p_x \\ \lambda _y & 1& -\lambda _y p_y \\ 0 & 0& 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \ end{bmatrix} x′y′1 = 1ly0lx10− lxpx− lypy1 xy1
import matplotlib.pyplot as plt
import numpy as np
X, Y = np.mgrid[0:1:5j, 0:1:5j]
x, y = X.ravel(), Y.ravel()
def trans_shear(x, y, px, py, lambdax, lambday):
T = [[1 , lambdax, -lambdax*px],
[lambday, 1 , -lambday*py],
[0 , 0 , 1 ]]
T = np.array(T)
P = np.array([x, y, [1]*x.size])
return np.dot(T, P)
fig, ax = plt.subplots(1, 4)
L_ = [[0, 0], [2, 0], [0, -2], [-2, -2]]
P_ = [[0, 0], [0, 0], [0, 1.5], [1.1, 1.1]]
for i in range(4):
lambdax, lambday = L_[i]; px, py = P_[i]
x_, y_, _ = trans_shear(x, y, px, py, lambdax, lambday)
ax[i].scatter(x_, y_)
ax[i].scatter(px, py)
ax[i].set_title(r'$p_x={0:.2f}$ , $p_y={1:.2f}$'.format(px, py) + '\n'
r'$\lambda_x={0:.2f}$ , $\lambda_y={1:.2f}$'.format(lambdax, lambday))
ax[i].set_xlim([-3, 3])
ax[i].set_ylim([-3, 3])
ax[i].grid(alpha=0.5)
ax[i].axhline(y=0, color='k')
ax[i].axvline(x=0, color='k')
plt.show()
5. Reflection
For mirroring, the normal vector v of the axis of symmetry ( vx , vy ) v(v_x,v_y)v ( vx,vy) , the mirror matrixT m T_{m}Tm表示为:
[ 1 − 2 x v 2 − 2 x v y v 0 − 2 x v y v 1 − 2 y v 2 0 0 0 1 ] \left[ \begin{array}{ccc} 1-2 x_{v}{ }^{2} & -2 x_{v} y_{v} & 0 \\ -2 x_{v} y_{v} & 1-2 y_{v}{ }^{2} & 0 \\ 0 & 0 & 1 \end{array} \right]
1−2x _v2−2x _ _vyv0−2x _ _vyv1−2 yv20001
In addition, a point is needed to represent the position of the symmetry axis (any point in the 2 points of the symmetry axis), expressed as M ( xm , ym ) M\left(x_{\mathrm{m}}, y_{m}\right)M(xm,ym) , transformation matrixH = T t ∗ T m ∗ T t − 1 H = T_{t}*T_{m}*T_{t}^{-1}H=Tt∗Tm∗Tt−1:
H = [ 1 0 0 0 1 0 xmym 1 ] [ 1 − 2 xv 2 − 2 xvyv 0 − 2 xvyv 1 − 2 yv 2 0 0 0 1 ] [ 1 0 0 0 1 0 − xm − ym ] H = \left[\begin{array}{ccc}1&0&0\\0&1&0\\x_{\mathrm{m}}&y_{m}&1\end{array}\right ] \left[\begin{array}{ccc}1-2x_{v}{}^{2}&-2x_{v}y_{v}&0\\-2x_{v}y_{v} &1-2 y_{v}{}^{2}&0\\0&0&1 \end{array}\right]\left[\begin{array}{ccc}1&0&0\\0 &1&0\\-x_{\mathrm{m}}&-y_{m}&1\end{array}\right]H=
10xm01ym001
1−2x _v2−2x _ _vyv0−2x _ _vyv1−2 yv20001
10−xm01−ym001
The coordinates after mirroring are T o ∗ T t ∗ T m ∗ T t − 1 T_{o}*T_{t}*T_{m}*T_{t}^{-1}To∗Tt∗Tm∗Tt−1。
Mirror matrix for mirroring along the X
axis: [ 1 0 0 0 − 1 0 1 0 1 ] \left[ \begin{array}{ccc} 1 & 0 & 0 \\ 0 & -1 & 0 \\ 1 & 0 & 1 \end{array} \right]
1010−10001
Mirror matrix for mirroring along the Y
axis: [ − 1 0 0 0 1 0 0 1 1 ] \left[ \begin{array}{ccc} -1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 1 & 1 \end{array} \right]
−100011001
import numpy as np
a = np.array([[1, 2],
[2, 2],
[3, 5],
[4, 6]])
a = np.array([a[:,0], a[:,1], np.ones(len(a))])
print("\n",a)
print("--------------------------------")
T_x = np.array( [[ 1, 0, 0],
[ 0, -1, 0],
[ 1, 0, 1]])
print("\n",np.dot(T_x, a))
print("=================================")
T_y = np.array( [[-1, 0, 0],
[ 0, 1, 0],
[ 0, 1, 1]])
print("\n",np.dot(T_y, a))
Reference:
https://zhuanlan.zhihu.com/p/387578291
https://zhuanlan.zhihu.com/p/187411029
https://blog.csdn.net/Akiyama_sou/article/details/122144415