(GAMES101) 1 - Review && Transformation (2D)
作者:互联网
「転科・転専攻」は失敗したが、
千束ちゃんは「やりたいこと最優先!」と言ってくれました!
#1. Review of Linear Algebra
Vectors Normalization & Cartesian Coordinates
magnitude (length) : \(\Vert\vec a\Vert\)
unit vector : \(\hat a = \cfrac{\vec a}{\Vert {\vec a} \Vert}\)
\(\boldsymbol A = \begin{bmatrix} ~x~ \\ y \end{bmatrix} ~~~~~ \boldsymbol A^T = \begin{bmatrix} ~x , ~y~ \end{bmatrix} ~~~~~ \Vert \boldsymbol A \Vert = \sqrt{x^2 + y^2}\)
Dot (Scalar) Product
\(\vec a \cdot \vec b = \Vert \vec a \Vert \Vert \vec b \Vert \cos \theta ~~~~~ \cos \theta = \cfrac{\vec a \cdot \vec b}{\Vert \vec a \Vert \Vert \vec b \Vert}\)
for unit vectors : \(\cos \theta = \hat a \cdot \hat b\)
in cartesian coordinates : \(\vec a \cdot \vec b = \begin{bmatrix} ~x_a~ \\ y_a \\ z_a \end{bmatrix} \cdot \begin{bmatrix} ~x_b~ \\ y_b \\ z_b \end{bmatrix} = x_ax_b + y_ay_b + z_az_b\)
\[i.e. ~~\boldsymbol a \cdot \boldsymbol b = \boldsymbol a ^ T \boldsymbol b \]projection of \(\vec b\) onto \(\vec a\) : \(\Vert\vec b_{\perp}\Vert = \Vert \vec b \Vert \cos \theta\)
usage : intersection angle, decompose, determine for/backward
Cross (Vector) Product
construct coordinate systems
cartesian formula : \(\vec a \times \vec b = \begin{bmatrix} ~y_az_b - y_bz_a ~\\~ z_ax_b - x_az_b ~\\~ x_ay_b - y_ax_b ~\end{bmatrix}\)
\[i.e. ~~ \vec a \times \vec b = A^*\boldsymbol b = \mathop{\begin{bmatrix} ~ 0 & -z_a & y_a ~\\ ~z_a & 0 & -x_a~ \\ ~-y_a & x_a & 0~\end{bmatrix}}\limits_{\text{dual matrix of a}} \begin{bmatrix} ~x_b~ \\ ~y_b~ \\ ~0~\end{bmatrix} \]usage : determine left / right, determine inside / outside
Matrix
array of numbers ($m \times n = $ m rows, n columns)
Matrix-Matrix Multiplication
require # columns in A = # rows in B
\[i.e. ~~(m \times n)(n \times p) = (m \times p) \]\((i, j)\) is dot product of \(\mathcal{row~ i~ from~ A}\) and \(\mathcal{column~ j~ from~ B}\)
-
non-commutative (\(AB\) and \(BA\) are different in general)
-
associative and distributive :
Matrix-Vector Multiplication
treat vector as a column matrix \(m \times 1\)
key for transforming points
Transpose of a Matrix
switch rows and columns
property : \((AB)^T = B^TA^T\)
Identity Matrix and Inverses
\(AA^{-1} = A^{-1}A = I ~~~~~~~~~ (AB)^{-1} = B^{-1}A^{-1}\)
#2. Transformation (2D)
Scaling
\[\begin{bmatrix}~ x' ~\\~ y' ~\end{bmatrix} = \begin{bmatrix} ~s_x & 0 ~\\~ 0 & s_y ~\end{bmatrix} \begin{bmatrix}~ x ~\\ ~ y ~ \end{bmatrix} \]Reflection
horizontal reflection
\[\begin{bmatrix}~ x' ~\\ y'\end{bmatrix} = \begin{bmatrix} ~-1 & 0 ~ \\~ 0 & 1~\end{bmatrix} \begin{bmatrix} ~x~ \\ y\end{bmatrix} \]Shearing
horizontal shift
\[\begin{bmatrix}~ x' ~\\ y'\end{bmatrix} = \begin{bmatrix} ~1 & \tan\phi ~ \\~ 0 & 1~\end{bmatrix} \begin{bmatrix} ~x~ \\ y\end{bmatrix} \]Rotation
about the origin \((0,0)\), CCW by default
\[\begin{bmatrix}~ x' ~\\ y'\end{bmatrix} = \begin{bmatrix} ~\cos\phi & -\sin\phi ~ \\~ \sin\phi & \cos\phi \end{bmatrix} \begin{bmatrix} ~x~ \\ y\end{bmatrix} \]Linear Transforms = Matrices (of the same dimension)
\[\mathbf x' = \mathcal M \mathbf x \]Translation
In order to solve such the problem
\[\begin{bmatrix} ~ x' ~ \\ ~ y' ~ \end{bmatrix} = \mathcal M \begin{bmatrix} ~ x ~ \\ ~ y ~ \\ \end{bmatrix} = \begin{bmatrix} ~ x + t_x ~ \\ ~ y + t_y ~ \end{bmatrix} \]2D point \(= (x, y, 1)^T\)
2D vector \(= (x, y, 0)^T\)
Matrix representation of translation
\[\begin{bmatrix} ~ x' ~ \\ ~ y' ~ \\ ~ w' ~ \end{bmatrix} = \begin{bmatrix} ~ 1 & 0 & t_x ~\\ ~ 0 & 1 & t_y ~ \\ ~ 0 & 0 & 1 ~ \end{bmatrix} \begin{bmatrix} ~ x ~ \\ ~ y ~ \\ ~ 1 ~ \end{bmatrix} = \begin{bmatrix} ~ x + t_x ~ \\ ~ y + t_y ~ \\ ~ 1 ~ \end{bmatrix} \]Valid operation if w-coordinate of result is \(1\) or \(0\)
- vector + vector = vector
- point - point = vector
- point + vector = point
- point + point = midpoint
In homogeneous coordinates, \(\begin{bmatrix} ~ x ~ \\ y \\ w \end{bmatrix}\) is the 2D point \(\begin{bmatrix} ~ x/w ~ \\ y/w \\ 1 \end{bmatrix},~w \neq 0\)
Affine map = linear map + translation (Linear Transforms First)
\[\begin{bmatrix} ~ x' ~ \\ ~ y' ~ \end{bmatrix} = \begin{bmatrix} ~ a & b ~ \\ ~ c & d ~ \end{bmatrix} \begin{bmatrix} ~ x ~ \\ ~ y ~ \\ \end{bmatrix} + \begin{bmatrix} ~ t_x ~ \\ ~ t_y ~ \end{bmatrix} \]Using homogeneous coordinates
\[\begin{bmatrix} ~ x' ~ \\ ~ y' ~ \\ ~ w' ~ \end{bmatrix} = \begin{bmatrix} ~ a & b & t_x ~\\ ~ c & d & t_y ~ \\ ~ 0 & 0 & 1 ~ \end{bmatrix} \begin{bmatrix} ~ x ~ \\ ~ y ~ \\ ~ 1 ~ \end{bmatrix} \]2D Transformations with homogeneous coordinates
Scale
\[\mathbf S(s_x, s_y) = \begin{bmatrix} ~ s_x & 0 & 0 ~\\ ~ 0 & s_y & 0 ~ \\ ~ 0 & 0 & 1 ~ \end{bmatrix} \]Rotation
\[\mathbf R(\phi) = \begin{bmatrix} ~ \cos\phi & -\sin\phi & 0 ~ \\ ~ \sin\phi & \cos\phi & 0 ~ \\ ~ 0 & 0 & 1 ~ \end{bmatrix} \]Translation
\[\mathbf T(t_x, t_y) = \begin{bmatrix} ~ 1 & 0 & t_x ~\\ ~ 0 & 1 & t_y ~ \\ ~ 0 & 0 & 1 ~ \end{bmatrix} \]Inverse Transform
\[\mathbf x = \mathcal {M^{-1}} \mathbf x' \]Composite Transform
Transform Ordering Matters !
Cause matrix multiplication is not commutative, matrices are applied right to left.
When we have a sequence of affine transforms \(A_1, A_2, A_3,~...\) :
Pre-multiply \(n\) matrices to obtain a single matrix for performance
\[A_n(...A_2(A_1(\mathbf x))) = \mathbf A_n \cdots \mathbf A_2 ~\cdot~ \mathbf A_1 ~\cdot~ \begin{bmatrix} ~ x ~ \\ ~ y ~ \\ ~ 1 ~ \end{bmatrix} \]Decomposing Complex Transform
eg. rotate around a given point c
\[\mathbf T(\mathbf c) \cdot \mathbf R(\mathbf \phi) \cdot \mathbf T(\mathbf {-c}) \]3D transformations ? (#3)
Using homogeneous coordinates again !
- 3D point = \((x, y, z, 1)^T\)
- 3D vector = \((x, y, z, 0)^T\)
In general, \((x, y, z, w)~(w != 0)\) is the 3D point \((\cfrac{x}{w}, \cfrac{y}{w}, \cfrac{z}{w})\)
Use \(4 \times 4\) matrices for affine transformations
\[\begin{bmatrix} ~ x' ~ \\ ~ y' ~ \\ ~ z' ~ \\ ~ w' \end{bmatrix} = \begin{bmatrix} ~ a & b & c & t_x ~\\ ~ d & e & f & t_y ~ \\ ~ g & h & i & t_z ~ \\ ~ 0 & 0 & 0 & 1 ~ \end{bmatrix} \begin{bmatrix} ~ x ~ \\ ~ y ~ \\ ~ z ~ \\ ~ 1 ~ \end{bmatrix} \]标签:begin,end,Vert,Review,2D,vec,bmatrix,mathbf,GAMES101 来源: https://www.cnblogs.com/siesta/p/GAMES101-1-2.html