Transformations in 2 Dimensions

References:

  1. Andy Johnson's CS 488 Course Notes, Lecture 5
  2. Foley, Van Dam, Feiner, and Hughes, "Computer Graphics - Principles and Practice", Chapter 5

One of the most common and important tasks in computer graphics is to transform the coordinates ( position, orientation, and size ) of either objects within the graphical scene or the camera that is viewing the scene. It is also frequently necessary to transform coordinates from one coordinate system to another, ( e.g. world coordinates to viewpoint coordinates to screen coordinates. ) All of these transformations can be efficiently and succintly handled using some simple matrix representations, which we will see can be particularly useful for combining multiple transformations into a single composite transform matrix.

We will look first at simple translation, scaling, and rotation in 2D, then extend our results to 3D, and finally see how multiple transformations can be easily combined into a composite transform.

Translation in 2D

point (X,Y) is to be translated by amount Dx and Dy to a new location (X',Y')

X' = Dx + X
Y' = Dy + Y

or P' = T + P where

      _   _
P' = |  X' |
     |  Y' |
      -   -
      _    _
T  = |  Dx  |
     |  Dy  |
      -    -
      _   _
P  = |  X  |
     |  Y  |
      -   -


Scaling in 2D

point (X,Y) is to be scaled by amount Sx and Sy to location (X',Y')

X' = Sx * X
Y' = Sy * Y

or P' = S * P where

      _   _
P' = |  X' |
     |  Y' |
      -   -
      _       _
S  = |  Sx  0  |
     |  0   Sy |
      -       -
      _   _
P  = |  X  |
     |  Y  |
      -   -

scaling is performed about the origin (0,0) not about the center of the line/polygon/whatever

Scale > 1 enlarge the object and move it away from the origin.
Scale = 1 leave the object alone
Scale< 1 shrink the object and move it towards the origin.

uniform scaling: Sx = Sy
differential scaling Sx != Sy -> alters proportions


Rotation in 2D

point (X,Y) is to be rotated about the origin by angle theta to location (X',Y')

X' = X * cos(theta) - Y * sin(theta)
Y' = X * sin(theta) + Y *cos(theta)

note that this does involve sin and cos which are much more costly than addition or multiplication

or P' = R * P where

      _   _
P' = |  X' |
     |  Y' |
      -   -
      _                       _
R  = |  cos(theta) -sin(theta) |
     |  sin(theta) cos(theta)  |
      -                       -
      _   _
P  = |  X  |
     |  Y  |
      -   -

rotation is performed about the origin (0,0) not about the center of the line/polygon/whatever

Derivation of the 2D Rotation Equations

Where does this matrix come from?

(X,Y) is located r away from (0,0) at a CCW angle of phi from the X axis.
(X',Y') is located r away from (0,0) at a CCW angle of theta+phi from the X axis.

Since rotation is about the origin, (X',Y') must be the same distance from the origin as (X,Y).

from trigonometry we have:

X = r * cos(phi)
Y = r * sin(phi)

and

X' = r * cos(theta+phi)
Y' = r * sin(theta+phi)

Now making use of the following trigonometric identities:

cos(a+b) = cos(a) * cos(b) - sin(a) * sin(b)
sin(a+b) = sin(a) * cos(b) + cos(a) * sin(b)

and substituting in for the above equations for X' and Y', we get:

X' = r * cos(theta) * cos(phi) - r * sin(theta) * sin(phi)
Y' = r * sin(theta) * cos(phi) + r * cos(theta) * sin(phi)

Then we substitute in X and Y from their definitions above, and the final result simplifies to:

X' = X * cos(theta) - Y * sin(theta)
Y' = X * sin(theta) + Y * cos(theta)


Homogeneous Coordinates in 2 Dimensions

Scaling and rotations are both handled using matrix multiplication, which can be combined as we will see shortly. The translations cause a difficulty, however, since they use addition instead of multiplication.

We want to be able to treat all 3 transformations (translation, scaling, rotation) in the same way - as multiplications.

The solution is to give each point a third coordinate (X, Y, W), which will allow translations to be handled as a multiplication also.

( Note that we are not really moving into the third dimension yet. The third coordinate is being added to the mathematics solely in order to combine the addition and multiplication of 2-D coordinates. )

Two triples (X,Y,W) and (X',Y',W') represent the same point if they are multiples of each other e.g. (1,2,3) and (2,4,6).

At least one of the three coordinates must be nonzero.

If W is 0 then the point is at infinity. This situation will rarely occur in practice in computer graphics.

If W is nonzero we can divide the triple by W to get the cartesian coordinates of X and Y which will be identical for triples representing the same point (X/W, Y/W, 1). This step can be considered as mapping the point from 3-D space onto the plane W=1.

Conversely, if the 2-D cartesian coordinates of a point are known as ( X, Y ), then the homogenous coordinates can be given as ( X, Y, 1 )

So, how does this apply to translation, scaling, and rotation of 2D coordinates?


Translation of 2D Homogenous Coordinates

point (X,Y) is to be translated by amount Dx and Dy to location (X',Y')

X' = Dx + X
Y' = Dy + Y

or P' = T * P where

      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _        _
T  = |  1 0 Dx  | = T(Dx,Dy)
     |  0 1 Dy  |
     |  0 0  1  |
      -        -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -

Hey Look! Translation is now a multiplication instead of an addition!

Scaling of 2D Homogenous Coordinates

P' = S * P where
      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _        _
S  = |  Sx 0 0  | = S(Sx,Sy)
     |  0 Sy 0  |
     |  0  0 1  |
      -        -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -

Rotation of 2D Homogenous Coordinates

P' = R * P where
      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _                         _
R  = |  cos(theta) -sin(theta) 0 | = R(theta)
     |  sin(theta)  cos(theta) 0 |
     |       0           0     1 |
      -                         -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -


Composition of 2D Transformations

There are many situations in which the final transformation of a point is a combination of several ( often many ) individual transformations. For example, the position of the finger of a robot might be a function of the rotation of the robots hand, arm, and torso, as well as the position of the robot on the railroad train and the position of the train in the world, and the rotation of the planet around the sun, and . . .

Applying each transformation individually to all points in a model would take a lot of time. Instead of applying several transformations matrices to each point we want to combine the transformations to produce 1 matrix which can be applied to each point.

In the simplest case we want to apply the same type of transformation (translation, rotation, scaling) more than once.

translation is additive as expected
scaling is multiplicative as expected
rotation is additive as expected

But what if we want to combine different types of transformations?

a very common reason for doing this is to rotate a polygon about an arbitrary point (e.g. the center of the polygon) rather than around the origin.

note the order of operations here is right to left:

P' = T(Dx,Dy) * R(theta) * T(-Dx,-Dy) * P
i.e.
P' = T(Dx,Dy) * { R(theta) * [ T(-Dx,-Dy) * P ] }
i.e.
P' = [ T(Dx,Dy) * R(theta) * T(-Dx,-Dy) ] * P

The matrix that results from these 3 steps can then be applied to all of the points in the polygon.

another common reason for doing this is to scale a polygon about an arbitrary point (e.g. the center of the polygon) rather than around the origin.

How do we determine the 'center' of the polygon?


Window to Viewport

Generally user's prefer to work in world-coordinates.

These coordinates must then be translated to screen coordinates to be displayed in a rectangular region of the screen called the viewport

The objects are in world coordinates (with n dimensions)
The viewport is in screen coordinates (with n=2)

Want one matrix that can be applied to all points:
rectangular area of world from (Xmin,Ymin) to (Xmax,Ymax) - world-coordinate window
rectangular area of screen from (Umin,Vmin) to (Umax,Vmax) - viewport

need to rescale the world-coordinate rectangle to the screen rectangle

1. translate world-coordinate window to the origin of the world coordinate system.
2. rescale the window to the size and aspect ratio of the viewport.
3. translate the viewport to its position on the screen in the screen coordinate system.

Pscreen = M * Pworld
M = T(Umin,Vmin) * S(deltaU/deltaX, deltaV/deltaY) * T(-Xmin, -Ymin)