- Forward


Transformation of 2D Shapes
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • What We Have:
    • Methods for representing triangles and convex polygons in 2D
  • What We Need:
    • The ability to transform these shapes in various ways
An Instructive Shape - The Unit Square
Back SMYC Forward
  • Visualization:
    • unitsquare2d
  • As a Matrix:
    • \(\left[ \begin{array}{r r r r} 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1\end{array}\right]\)
    • Remember that, in code, it is sometimes more convenient to use the transpose (in which each vertex is a row, not a column)
2D Transformations in General
Back SMYC Forward
  • Our Objective:
    • Operate on the \(2 \times n\) matrix representation of a shape
    • Get back a \(2 \times n\) matrix representation
  • Operations that Satisfy this Condition:
    • Multiplication by a scalar
    • Addition of a \(2 \times n\) matrix
    • Pre-multiplication by a \(2 \times 2\) matrix
Scaling
Back SMYC Forward
  • A Uniform Scaling:
    • Multiply by a scalar
  • Scaling Each Dimension Independently:
    • Multiply by a matrix of the form \(\left[ \begin{array}{r r} \alpha & 0 \\ 0 & \beta\end{array}\right]\)
Scaling (cont.)
Back SMYC Forward
  • An Example:
    • \( \left[ \begin{array}{r r} 2 & 0 \\ 0 & 0.5\end{array}\right] \left[ \begin{array}{r r r r} 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1\end{array}\right] = \left[ \begin{array}{r r r r} 0 & 2 & 2 & 0 \\ 0 & 0 & 0.5 & 0.5\end{array}\right] \)
  • Visualization:
    • scaling2d01
Scaling (cont.)
Back SMYC Forward
  • Be Careful:
    • \( \left[ \begin{array}{r r } 2 & 0 \\ 0 & 0.5\end{array}\right] \left[ \begin{array}{r r r r} 0.5 & 1.5 & 1.5 & 0.5 \\ 0.5 & 0.5 & 1.5 & 1.5\end{array}\right] = \left[ \begin{array}{r r r r} 1 & 3 & 3 & 1 \\ 0.25 & 0.25 & 0.75 & 0.75\end{array}\right] \)
  • Visualization:
    • scaling2d02
Reflection
Back SMYC Forward
  • Around the \(x\)-Axis:
    • \( \left[ \begin{array}{r r} 1 & 0 \\ 0 & -1\end{array}\right] \)
  • Visualization:
    • reflection2d01
Reflection (cont.)
Back SMYC Forward
  • Around the \(y\)-Axis:
    • \( \left[ \begin{array}{r r} -1 & 0 \\ 0 & 1\end{array}\right] \)
  • Visualization:
    • reflection2d02
Reflection (cont.)
Back SMYC Forward
  • Around the Line \(x = -y\):
    • \(\left[ \begin{array}{r r} 0 & -1 \\ -1 & 0\end{array}\right]\)
  • Visualization:
    • reflection2d03
Reflection (cont.)
Back SMYC Forward
  • Be Careful:
    • It looks like reflections do not change the area
    • It looks like reflections are scalings
  • In Fact:
    • They can change the sign of the area
    • In general, reflections are not scalings (they are Householder transformations)
Shears
Back SMYC Forward
  • Along the \(x\)-Axis:
    • \( \left[ \begin{array}{r r} 1 & d_{1} \\ 0 & 1\end{array}\right] \)
  • Visualization:
    • shear2d01
Shears (cont.)
Back SMYC Forward
  • Along the \(y\)-Axis:
    • \( \left[ \begin{array}{r r} 1 & 0 \\ d_{2} & 1\end{array}\right] \)
  • Visualization:
    • shear2d02
Shears (cont.)
Back SMYC Forward
  • One Use of Shears:
    • Italic fonts
  • Note:
    • Shears do not change the area
Rotation
Back SMYC Forward
  • An Interesting Transformation Matrix:
    • \( \left[ \begin{array}{r r} 0 & -1 \\ 1 & 0\end{array}\right] \left[ \begin{array}{r r r r} 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1\end{array}\right] = \left[ \begin{array}{r r r r} 0 & 0 & -1 & -1 \\ 0 & 1 & 1 & 0\end{array}\right] \)
  • Visualization:
    • rotation2d01
Rotation (cont.)
Back SMYC Forward
  • Some Intuition:
    • rotation2d02
  • What We Need:
    • \( \left[ \begin{array}{r} \cos \theta \\ \sin \theta \end{array}\right] = \bs{R} \left[ \begin{array}{r r} 1 \\ 0\end{array}\right] \)
      \( \left[ \begin{array}{r} -\sin \theta \\ \cos \theta\end{array}\right] = \bs{R} \left[ \begin{array}{r}0 \\ 1\end{array}\right] \)
  • The Transformation Matrix that Satisfies these Conditions:
    • \( \bs{R} = \left[ \begin{array}{r r} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta\end{array}\right] \)
Rotation (cont.)
Back SMYC Forward
  • Be Careful:
    • These rotations are around the origin
  • Note:
    • Rotations do not change the area
Translation
Back SMYC Forward
  • Defined:
    • Moving a shape without changing its orientation
  • Operation:
    • Add a value to the components of each point
Combining Transformations
Back SMYC Forward
  • An Important Observation:
    • Some transformations can be combined by multiplying the transformation matrixes
  • An Unfortunate Observation:
    • Scaling, reflection, shears and rotation all involve matrix multiplication
    • Translation involves vector addition
Homogeneous Coordinates
Back SMYC Forward
  • Recall:
    • The homogeneous form of a line is the set of points that satisfy:
    • \(h_{1}p_{1} + h_{2}p_{2} + 1 = 0\)
  • An Observation:
    • If we add a third coordinate to each point we can write this as:
    • \( \left[ \begin{array}{r} h_{1} \\ h_{2} \\ 1\end{array}\right] \cdot \left[ \begin{array}{r} p_{1} \\ p_{2} \\ 1\end{array}\right] = \left[ \begin{array}{r} 0 \\ 0 \\ 0\end{array}\right] \)
  • The Homogeneous Coordinate System:
    • Every point \([p_{1} \quad p_{2}]\) in Cartesian coordinates corresponds to a point \([p_{1} \quad p_{2} \quad 1]\) in homogeneous coordinates
Translation in Homogeneous Coordinates
Back SMYC Forward
  • An Observation:
    • \( \left[ \begin{array}{r r r} 1 & 0 & \tau_{1} \\ 0 & 1 & \tau_{2} \\ 0 & 0 & 1\end{array}\right] \left[ \begin{array}{r} p_{1} \\ p_{2} \\ 1 \end{array}\right] = \left[ \begin{array}{r} p_{1}+\tau_{1} \\ p_{2}+\tau_{2} \\ 1 \end{array}\right] \)
  • Interpretation:
    • In homogeneous coordinates, we can translate a point using matrix multiplication
Translation in Homogeneous Coordinates (cont.)
Back SMYC Forward
  • Translation Matrix:
    • \( \bs{T} = \left[ \begin{array}{r r r} 1 & 0 & \tau_{1} \\ 0 & 1 & \tau_{2} \\ 0 & 0 & 1\end{array}\right] \)
  • Inverse Translation Matrix:
    • \( \bs{T}^{-1} = \left[ \begin{array}{r r r} 1 & 0 & -\tau_{1} \\ 0 & 1 & -\tau_{2} \\ 0 & 0 & 1\end{array}\right] \)
Scaling in Homogeneous Coordinates
Back SMYC Forward
  • Scaling Matrix:
    • \( \bs{S} = \left[ \begin{array}{r r r} \sigma_{1} & 0 & 0 \\ 0 & \sigma_{2} & 0 \\ 0 & 0 & 1\end{array}\right] \)
  • Inverse Scaling Matrix:
    • \( \bs{S}^{-1} = \left[ \begin{array}{r r r} 1 / \sigma_{1} & 0 & 0 \\ 0 & 1/ \sigma_{2} & 0 \\ 0 & 0 & 1\end{array}\right] \)
Shears in Homogeneous Coordinates
Back SMYC Forward
  • Along the \(x\)-Axis:
    • \( \left[ \begin{array}{r r r} 1 & d_{1} & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{array}\right] \)
  • Along the \(y\)-Axis:
    • \( \left[ \begin{array}{r r r} 1 & 0 & 0 \\ d_{2} & 1 & 0 \\ 0 & 0 & 1\end{array}\right] \)
Rotation in Homogeneous Coordinates
Back SMYC Forward
  • Rotation Matrix (Around the Origin):
    • \( \bs{R} = \left[ \begin{array}{r r r} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1\end{array}\right] \)
  • Inverse Rotation Matrix:
    • Use the negative of the angle
Rotation in Homogeneous Coordinates (cont.)
Back SMYC Forward
  • Rotation Around a Point:
    • Translate the center of rotation to the origin, rotate, translate back
  • The Result:
    • \( \left[ \begin{array}{r r r} 1 & 0 & -x \\ 0 & 1 & -y \\ 0 & 0 & 1\end{array}\right] \left[ \begin{array}{r r r} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1\end{array}\right] \left[ \begin{array}{r r r} 1 & 0 & x \\ 0 & 1 & y \\ 0 & 0 & 1\end{array}\right] = \left[ \begin{array}{r r r} \cos \theta & -\sin \theta & -x(\cos \theta -1) + y \sin \theta \\ \sin \theta & \cos \theta & -x \sin \theta - y(\cos \theta -1) \\ 0 & 0 & 1\end{array}\right] \)
Reflection in Homogeneous Coordinates
Back SMYC Forward
  • Around the \(x\)-Axis:
    • \( \left[ \begin{array}{r r r} 1 & 0 & 0 \\ 0 & -1 & 0 \\ 0 & 0 & 1\end{array}\right] \)
  • Around the \(y\)-Axis:
    • \( \left[ \begin{array}{r r r} -1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{array}\right] \)
Combining Transformations in Homogeneous Coordinates
Back SMYC Forward
  • An Important Observation:
    • Different transformations can be combined by multiplying the transformation matrixes
  • Efficiency:
    • It is generally more efficient to combine the transformations
There's Always More to Learn
Back -