# 二维变换矩阵

### 二维平面上常见的三种几何变换

• 平移
• 缩放
• 旋转

#### 平移

P ′ = P + Δ P [ x ′ y ′ ] = [ x y ] + [ Δ x Δ y ] = [ x + Δ x y + Δ y ] P&#x27;=P+\Delta P\\ \left [ \begin{matrix} x&#x27; \\y&#x27; \end{matrix}\right]=\left [ \begin{matrix} x \\y \end{matrix}\right]+\left [ \begin{matrix} \Delta x \\ \Delta y \end{matrix}\right] =\left [ \begin{matrix} x+\Delta x \\ y + \Delta y \end{matrix}\right]

#### 缩放

P ′ = S P [ x ′ y ′ ] = [ S x 0 0 S y ] [ x y ] = [ x S x y S y ] P&#x27;=SP\\ \left [ \begin{matrix} x&#x27; \\y&#x27; \end{matrix}\right]=\left [ \begin{matrix} S_x &amp; 0 \\0 &amp; S_y \end{matrix}\right]\left [ \begin{matrix} x \\ y \end{matrix}\right] =\left [ \begin{matrix} xS_x \\ yS_y \end{matrix}\right]

#### 旋转

R = [ c o s ( α ) s i n ( α ) − s i n ( α ) c o s ( α ) ] R = \left [ \begin{matrix} cos(\alpha) &amp; sin(\alpha) \\-sin(\alpha) &amp; cos(\alpha) \end{matrix}\right]

P ′ = R P [ x ′ y ′ ] = [ c o s ( α ) s i n ( α ) − s i n ( α ) c o s ( α ) ] [ x y ] = [ x c o s ( α ) + y s i n ( α ) − x s i n ( α ) + y c o s ( α ) ] P&#x27;=RP\\ \left [ \begin{matrix} x&#x27; \\y&#x27; \end{matrix}\right]=\left [ \begin{matrix} cos(\alpha) &amp; sin(\alpha) \\-sin(\alpha) &amp; cos(\alpha) \end{matrix}\right]\left [ \begin{matrix} x \\ y \end{matrix}\right] =\left [ \begin{matrix} xcos(\alpha)+ysin(\alpha) \\-xsin(\alpha) +ycos(\alpha) \end{matrix}\right]

#### 齐次坐标系的引入

P = [ x y 1 ] P = \left[ \begin{matrix} x\\ y\\1 \end{matrix} \right]

Δ P = [ 1 0 Δ x 0 1 Δ y 0 0 1 ] \Delta P = \left[ \begin{matrix} 1 &amp; 0 &amp; \Delta x \\ 0 &amp; 1 &amp; \Delta y \\ 0 &amp; 0 &amp; 1\end{matrix} \right]

P ′ = Δ P × P [ x ′ y ′ 1 ] = [ 1 0 Δ x 0 1 Δ y 0 0 1 ] [ x y 1 ] = [ x + Δ x y + Δ y 1 ] P&#x27;=\Delta P \times P\\ \left [ \begin{matrix} x&#x27; \\y&#x27; \\ 1 \end{matrix}\right]=\left[ \begin{matrix} 1 &amp; 0 &amp; \Delta x \\ 0 &amp; 1 &amp; \Delta y \\ 0 &amp; 0 &amp; 1\end{matrix} \right] \left [ \begin{matrix} x \\ y \\ 1\end{matrix}\right] =\left [ \begin{matrix} x+\Delta x \\ y + \Delta y \\ 1 \end{matrix}\right]

S = [ S x 0 0 0 S y 0 0 0 1 ] S = \left[ \begin{matrix} S_x &amp; 0 &amp; 0 \\ 0 &amp; S_y &amp; 0 \\ 0 &amp; 0 &amp; 1\end{matrix} \right]

P ′ = S × P [ x ′ y ′ 1 ] = [ S x 0 0 0 S y 0 0 0 1 ] [ x y 1 ] = [ x S x y S y 1 ] P&#x27;=S \times P\\ \left [ \begin{matrix} x&#x27; \\y&#x27; \\ 1 \end{matrix}\right]=\left[ \begin{matrix} S_x &amp; 0 &amp; 0 \\ 0 &amp; S_y &amp; 0 \\ 0 &amp; 0 &amp; 1\end{matrix} \right] \left [ \begin{matrix} x \\ y \\ 1\end{matrix}\right] =\left [ \begin{matrix} xS_x \\ y S_y \\ 1 \end{matrix}\right]

S = [ c o s ( α ) s i n ( α ) 0 − s i n ( α ) c o s ( α ) 0 0 0 1 ] S = \left[ \begin{matrix} cos(\alpha) &amp; sin(\alpha) &amp; 0 \\ -sin(\alpha) &amp; cos(\alpha) &amp; 0 \\ 0 &amp; 0 &amp; 1\end{matrix} \right]

P ′ = R × P [ x ′ y ′ 1 ] = [ c o s ( α ) s i n ( α ) 0 − s i n ( α ) c o s ( α ) 0 0 0 1 ] [ x y 1 ] = [ x c o s ( α ) + y s i n ( α ) − x s i n ( α ) + y c o s ( α ) 1 ] P&#x27;=R \times P\\ \left [ \begin{matrix} x&#x27; \\y&#x27; \\ 1 \end{matrix}\right]=\left[ \begin{matrix} cos(\alpha) &amp; sin(\alpha) &amp; 0 \\ -sin(\alpha) &amp; cos(\alpha) &amp; 0 \\ 0 &amp; 0 &amp; 1\end{matrix} \right] \left [ \begin{matrix} x \\ y \\ 1\end{matrix}\right] =\left [ \begin{matrix} xcos(\alpha) + ysin(\alpha) \\ -xsin(\alpha)+ycos(\alpha) \\ 1 \end{matrix}\right]

• 将图形平移到原点
• 对图形进行旋转
• 将图形平移回原来的位置

P ′ = R ( P + Δ P ) + ( − Δ P ) = [ c o s ( α ) s i n ( α ) − s i n ( α ) c o s ( α ) ] ( [ x y ] + [ Δ x Δ y ] ) − [ Δ x Δ y ] P&#x27; = R(P+\Delta P)+(-\Delta P) \\ = \left [ \begin{matrix} cos(\alpha) &amp; sin(\alpha) \\-sin(\alpha) &amp; cos(\alpha) \end{matrix}\right] \left( \left [ \begin{matrix} x \\ y \end{matrix}\right] + \left [ \begin{matrix} \Delta x \\ \Delta y \end{matrix}\right] \right) -\left [ \begin{matrix} \Delta x \\ \Delta y \end{matrix}\right]

P ′ = ( − Δ P ) × R × Δ P × P = − [ 1 0 Δ x 0 1 Δ y 0 0 1 ] [ c o s ( α ) s i n ( α ) 0 − s i n ( α ) c o s ( α ) 0 0 0 1 ] [ 1 0 Δ x 0 1 Δ y 0 0 1 ] [ x y 1 ] P&#x27; = (-\Delta P)\times R\times \Delta P\times P \\ =- \left[ \begin{matrix} 1 &amp; 0 &amp; \Delta x \\ 0 &amp; 1 &amp; \Delta y \\ 0 &amp; 0 &amp; 1\end{matrix} \right] \left[ \begin{matrix} cos(\alpha) &amp; sin(\alpha) &amp; 0 \\ -sin(\alpha) &amp; cos(\alpha) &amp; 0 \\ 0 &amp; 0 &amp; 1\end{matrix} \right]\left[ \begin{matrix} 1 &amp; 0 &amp; \Delta x \\ 0 &amp; 1 &amp; \Delta y \\ 0 &amp; 0 &amp; 1\end{matrix} \right] \left[ \begin{matrix} x\\ y\\1 \end{matrix} \right]

import numpy as np
import matplotlib.pyplot as plt

def poly_gen(x0=(10,10),length = 20):
poly = []
x = x0[0]
y = x0[1]
for _ in range(length):
y += 1
poly.append(np.array([x,y,1]))

x = x0[0]
y = x0[1]
for _ in range(length):
x += 1
poly.append(np.array([x,y,1]))
x = x0[0]+length
y = x0[1]
for _ in range(length):
y += 1
poly.append(np.array([x,y,1]))

x = x0[0]
y = x0[1]+length
for _ in range(length):
x +=1
poly.append(np.array([x,y,1]))

center = np.array([x0[0]+length/2,x0[1]+length/2])

return poly, center

def translation(poly,dx,dy):
T = np.array([[1,0,dx],
[0,1,dy],
[0,0,1]])

poly_trans = []
for x in poly:
poly_trans.append(np.dot(T,x))
return poly_trans

def rotation(poly,theta):
R = np.array([[np.cos(theta), np.sin(theta),0],
[-np.sin(theta),np.cos(theta),0],
[0,0,1]])
poly_rotation = []
for x in poly:
poly_rotation.append(np.dot(R,x))
return  poly_rotation

if __name__=='__main__':

fig1=plt.figure()
poly, center = poly_gen()
for x in poly:
plt.scatter(x[0],x[1])

plt.xlim(0,50)
plt.ylim(0,50)

fig2=plt.figure()
poly_trans = translation(poly,-center[0],-center[1])
poly_rotation = rotation(poly_trans, np.pi / 4.)
poly_trans = translation(poly_rotation,center[0],center[1])
for x in poly_trans:
plt.scatter(x[0],x[1])
plt.xlim(0,50)
plt.ylim(0,50)
plt.show()


07-09 1233

08-18 363
09-27 1989
11-27 1275
10-17 756
03-21 8927
12-03
03-18 5283
04-22 3614
10-04 9791
03-19 5208
01-27 146
06-21 6万+
10-12 1万+
09-03 15万+
03-14 3万+
04-07 3万+