Games101 ---3

变换

为什么学习变换

1.表示旋转等复杂的动作
2.做动画
3.光栅化成像

二维的变换

缩放(Scale)

1.设 S1,S2为扩大或缩小的倍数
S1缩放x轴 S2缩放y轴
{ x ′ y ′ } = { S 1 0 0 S 2 } ∗ { x y } \begin{Bmatrix}\mathbf{x}^\mathrm{'}\\\mathbf{y}^\mathrm{'}\end{Bmatrix}=\begin{Bmatrix}S1&&0\\0&&S2\end{Bmatrix}*\begin{Bmatrix}x\\y\end{Bmatrix} {xy}={S100S2}{xy}

请添加图片描述

2.缩放矩阵
{ S 1 0 0 S 2 } \begin{Bmatrix}S1&0\\0&S2\end{Bmatrix} {S100S2}

反射(Reflection)

1.也就是反转
x ′ = − x \mathbf{x}^\mathrm{'} = -x x=x
y ′ = − y \mathbf{y}^\mathrm{'}=-y y=y
请添加图片描述

2.反转矩阵
{ x ′ y ′ } = { − 1 0 0 1 } ∗ { x y } \begin{Bmatrix}\mathbf{x}^\mathrm{'}\\\mathbf{y}^\mathrm{'}\end{Bmatrix}=\begin{Bmatrix}-1&&0\\0&&1\end{Bmatrix}*\begin{Bmatrix}x\\y\end{Bmatrix} {xy}={1001}{xy}

切变(Shear)

1.设a为切变的距离 则
y ′ = y \mathbf{y}^\mathrm{'}=y y=y
x ′ = a x \mathbf{x}^\mathrm{'}=ax x=ax
{ x ′ y ′ } = { 1 a 0 1 } ∗ { x y } \begin{Bmatrix}\mathbf{x}^\mathrm{'}\\\mathbf{y}^\mathrm{'}\end{Bmatrix}=\begin{Bmatrix}1&a\\0&1\end{Bmatrix}*\begin{Bmatrix}x\\y\end{Bmatrix} {xy}={10a1}{xy}
请添加图片描述

注:*重要的是知道x和y是如何变化的 才能判断矩阵怎么写*

旋转(Rotate)

一般默认以逆时针为正方向
1.设 θ 为 旋 转 角 度 \theta为旋转角度 θ
{ x ′ y ′ } = { c o s θ − s i n θ s i n θ c o s θ } ∗ { x y } \begin{Bmatrix}\mathbf{x}^\mathrm{'}\\\mathbf{y}^\mathrm{'}\end{Bmatrix}=\begin{Bmatrix}cos\theta&-sin\theta\\sin\theta&cos\theta\end{Bmatrix}*\begin{Bmatrix}x\\y\end{Bmatrix} {xy}={cosθsinθsinθcosθ}{xy}
请添加图片描述

2.旋转矩阵
R θ = { c o s θ − s i n θ s i n θ c o s θ } R\theta=\begin{Bmatrix}cos\theta&-sin\theta\\sin\theta&cos\theta\end{Bmatrix} Rθ={cosθsinθsinθcosθ}
3.只要不声明旋转点在哪,那就在原点

线性变换(Linear Transforms)

是以上的总结
x ′ = a x + b y \mathbf{x}^\mathrm{'}=ax+by x=ax+by
y ′ = c x + d y \mathbf{y}^\mathrm{'}=cx+dy y=cx+dy
M = { x ′ y ′ } = { a b c d } ∗ { x y } M=\begin{Bmatrix}\mathbf{x}^\mathrm{'}\\\mathbf{y}^\mathrm{'}\end{Bmatrix}=\begin{Bmatrix}a&b\\c&d\end{Bmatrix}*\begin{Bmatrix}x\\y\end{Bmatrix} M={xy}={acbd}{xy}
x ′ = M x \mathbf{x}^\mathrm{'}=Mx x=Mx
请添加图片描述

移动

{ x ′ y ′ } = { a b c d } ∗ { x y } + { t 1 t 2 } \begin{Bmatrix}\mathbf{x}^\mathrm{'}\\\mathbf{y}^\mathrm{'}\end{Bmatrix}=\begin{Bmatrix}a&b\\c&d\end{Bmatrix}*\begin{Bmatrix}x\\y\end{Bmatrix}+\begin{Bmatrix}t1\\t2\end{Bmatrix} {xy}={acbd}{xy}+{t1t2}

####齐次坐标
齐次坐标就是将一个原本是n维的向量用一个n+1维向量来表示
齐次坐标解析
####矩阵表示
2 D p o i n t = ( x , y , a ) T 2D point=\mathbf{(x,y,a)}^\mathrm{T} 2Dpoint=(x,y,a)T
a不等于0
请添加图片描述

但由于向量具有平移不变性 ,因此
2 D v e c t o r = ( x , y , 0 ) T 2D vector=\mathbf{(x,y,0)}^\mathrm{T} 2Dvector=(x,y,0)T
通过a = 0的手法使向量移动中不改变它
{ x ′ y ′ w ′ } = { 1 0 t x 0 1 t y 0 0 1 } ∗ { x y 1 } = { x + t x y + t y 1 } \begin{Bmatrix}\mathbf{x}^\mathrm{'}\\\mathbf{y}^\mathrm{'}\\\mathbf{w}^\mathrm{'}\end{Bmatrix}=\begin{Bmatrix}1&0&tx\\0&1&ty\\0&0&1\end{Bmatrix}*\begin{Bmatrix}x\\y\\1\end{Bmatrix}=\begin{Bmatrix}x+tx\\y+ty\\1\end{Bmatrix} xyw=100010txty1xy1=x+txy+ty1

向量与点的运算

1.向量+向量=向量
体现在 w1+w2 = 0
2.点 - 点 = 向量
体现在w1 = w2 且 w1-w2 = 0
3.点+向量=点
体现在w1不等于0但w2等于0 且 w1-w2不等于0
4.点 + 点 = 两点的中点
设 点 p 1 为 { x 1 y 1 1 } p 2 = { x 2 y 2 1 } 设点p1为\begin{Bmatrix}x1\\y1\\1\end{Bmatrix}p2=\begin{Bmatrix}x2\\y2\\1\end{Bmatrix} p1x1y11p2=x2y21
$ 则p1+p2=\begin{Bmatrix}x1+x2\y1+y2\2\end{Bmatrix}=\begin{Bmatrix}\frac{x1+x2}{2}\\frac{y1+y2}{2}\1\end{Bmatrix} $
[外链图片转存失败,源站可能有防盗]!链机制,建(齐次变换.pniml齐变换.png)]
###仿射变换
仿射变换,又称仿射映射,是指在几何中,一个向量空间进行一次线性变换并接上一个平移,变换为另一个向量空间
1.也就是以上提到的线性变换加一次平移
{ x ′ y ′ } = { a b c d } ∗ { x y } + { t 1 t 2 } \begin{Bmatrix}\mathbf{x}^\mathrm{'}\\\mathbf{y}^\mathrm{'}\end{Bmatrix}=\begin{Bmatrix}a&b\\c&d\end{Bmatrix}*\begin{Bmatrix}x\\y\end{Bmatrix}+\begin{Bmatrix}t1\\t2\end{Bmatrix} {xy}={acbd}{xy}+{t1t2}
2.可以被齐次坐标表示

齐次坐标表示下变换矩阵的特点

{ a b t x c d t y 0 0 1 } \begin{Bmatrix}a&b&tx\\c&d&ty\\0&0&1\end{Bmatrix} ac0bd0txty1
1.第三行前两列都是0

####齐次坐标下的各种矩阵
1.缩放矩阵(Scale)
M = { S 1 0 0 0 S 2 0 0 0 1 } M=\begin{Bmatrix}S1&0&0\\0&S2&0\\0&0&1\end{Bmatrix} M=S1000S20001
2.旋转矩阵
M = { c o s θ − s i n θ 0 s i n θ c o s θ 0 0 0 1 } M=\begin{Bmatrix}cos\theta&-sin\theta&0\\sin\theta&cos\theta&0\\0&0&1\end{Bmatrix} M=cosθsinθ0sinθcosθ0001
3.平移矩阵
M = { 1 0 t x 0 1 t y 0 0 1 } M=\begin{Bmatrix}1&0&tx\\0&1&ty\\0&0&1\end{Bmatrix} M=100010txty1

逆变换

1.乘对应的逆矩阵

矩阵乘法的方向以右往左

假设矩阵M先旋转后平移 则表达式应为
M ′ = { 1 0 t x 0 1 t y 0 0 1 } ∗ { c o s θ − s i n θ 0 s i n θ c o s θ 0 0 0 1 } ∗ M \mathbf{M}^\mathrm{'}=\begin{Bmatrix}1&0&tx\\0&1&ty\\0&0&1\end{Bmatrix}*\begin{Bmatrix}cos\theta&-sin\theta&0\\sin\theta&cos\theta&0\\0&0&1\end{Bmatrix}*M M=100010txty1cosθsinθ0sinθcosθ0001M
从右往左运算

合成变换

可将变换矩阵先相乘后与向量矩阵相乘
An(…A2(A1(x)))M = An…A2A1M = (An*…A2*A1)*M
请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值