Files
2026-10-04 15:24:17 +01:00

152 lines
3.5 KiB
Markdown

# Transformations
### Translation
Translation is done by adding/subtracting the translation distance to either the x or y (or both) component
A translation of $(3,2)$ done on vector $u=\begin{pmatrix} u_0\\u_1\end{pmatrix}$
$u'=\begin{pmatrix} u_0+3\\u_1+2\end{pmatrix}$
This can be applied to a triangle, where every vertex is translated by the same amount.
### Rotation
Vector $v$ can be rotated by angle $\theta$ radians anticlockwise as follows
$$
rot(v) = \begin{pmatrix}
cos\theta * v_0 - sin\theta * v_1 \\
sin\theta * v_0 - cos\theta * v_1
\end{pmatrix}
$$
![1646415138.png](img/1646415138.png)
We can rotate triangles by rotating each vertex
### Scale
The vector $v$ can be scaled by scalar $s$ in each dimension independently
$$
v \cdot s = \begin{pmatrix} v_0 \cdot s_0 \\ v_1\cdot s_1 \end{pmatrix}
$$
We can scale triangles by scaling each of their vertices
### Transformation Matrix
#### Translation
This is where we represent a transformation in the form of a matrix
- The translation matrix, **T**, which translates by some vector **t** $= (t_x, t_y, t_z)$
$$
T(t) = \begin{pmatrix}
1 \quad 0 \quad 0 \quad t_x \\
0 \quad 1 \quad 0 \quad t_y \\
0 \quad 0 \quad 1 \quad t_z \\
0 \quad 0 \quad 0 \quad 1
\end{pmatrix}
$$
#### Rotation
- The rotations around each axis by some angle $\theta$ are represented as matrices $R_x, R_y, R_z$
- This is in 2 dimensions
![1646415811.png](img/1646415811.png)
#### Scale
$$
S(t) = \begin{pmatrix}
s_x \quad 0 \quad 0 \quad 0 \\
0 \quad s_y \quad 0 \quad 0 \\
0 \quad 0 \quad s_z \quad 0 \\
0 \quad 0 \quad 0 \quad 1
\end{pmatrix}
$$
### Homogeneous Coordinates
- Vector defines a **direction** or a **position**
- We can rotate directions and positions
- We can scale directions and positions
- We can only translate positions
A homogeneous vector is $p=(p_x, p_y, p_z, p_w)$
- Transforming a point $p$ is done by multiplying the vector (with $w$ set to 1) by the transformation matrix $M$
- To transform vectors the $w$ must be set to 1
![1646416149.png](img/1646416149.png)
The translation matrix can be applied to point $p$ by multiplying the point by the matrix
![1646416282.png](img/1646416282.png)
The scale matrix can be applied to a point $p$ by multiplying the point by the matrix
![1646416316.png](img/1646416316.png)
The rotation around x matrix can be applied to a point $p$ by multiplying the point by the matrix
![1646416447.png](img/1646416447.png)
y:
![1646416456.png](img/1646416456.png)
z:
![1646416468.png](img/1646416468.png)
#### Combining Transformations
For example, if point $p$ needs to be scaled by $s=(2,1,1)$ and then translated by $t=(1,0,0)$
$$
S = \begin{pmatrix}
2 \quad 0 \quad 0 \quad 0 \\
0 \quad 1 \quad 0 \quad 0 \\
0 \quad 0 \quad 1 \quad 0 \\
0 \quad 0 \quad 0 \quad 1
\end{pmatrix}
\\\\
T = \begin{pmatrix}
1 \quad 0 \quad 0 \quad 1 \\
0 \quad 1 \quad 0 \quad 0 \\
0 \quad 0 \quad 1 \quad 0 \\
0 \quad 0 \quad 0 \quad 1
\end{pmatrix}
\\\\
TS = T\times S =
\begin{pmatrix}
2 \quad 0 \quad 0 \quad 1 \\
0 \quad 1 \quad 0 \quad 0 \\
0 \quad 0 \quad 1 \quad 0 \\
0 \quad 0 \quad 0 \quad 1
\end{pmatrix}
$$
Matrix multiplication is read from right to left
The order of the transformations makes a difference and can change the resulting vector for example
$$
ST = S\times T =
\begin{pmatrix}
2 \quad 0 \quad 0 \quad 2 \\
0 \quad 1 \quad 0 \quad 0 \\
0 \quad 0 \quad 1 \quad 0 \\
0 \quad 0 \quad 0 \quad 1
\end{pmatrix}
$$
![1646417286.png](img/1646417286.png)
The difference between translating and scaling vs scaling and translating vector $v$