5. 2D & 3D Transformations

Brock University
COSC 3P98 Computer Graphics
Instructor: Brian Ross



2d transformations


2d transforms: Translation

1. Translate - move (x,y) coords to new locations, offset by (dx, dy)

P' = P + T =   | X | + | dx |
               | Y |   | dy |

x' = x + dx
y' = y + dy


2d transforms: Scaling

2. Scaling: changing size by a factor along x, y axis

P' = S * P =  | Sx   0 | * | X |
              | 0   Sy |   | Y |

X' = Sx * x
Y' = Sy * y

eg. make polygon half size: sx = 0.5, sy = 0.5

eg. squashing a polygon: sx = 4, sy = 0.1


2d transforms: rotation

3. Rotation: rotate each (x, y) through angle counterclockwise around origin

P' = R * P =  | cos A  -sin A | * | X |
              | sin A   cos A |   | Y |

x' = x cos A - y sin A
y' = x sin A + y cos A

for clockwise: cos(- A) = cos A, sin(- A ) = - sin(A )


2D rotation derivation

x = r*cos A   
y = r*sin A

So...
x' = r*cos(A+B) = r*cos(A)*cos(B) - r*sin(A)*sin(B)
y' = r*sin(A+B) = r*cos(A)*sin(B) + r*sin(A)*cos(B)

So: x' = x*cos(B) - y*sin(B)
    y' = x*sin(B) + y*cos(B)


2d transforms


2d homogeneous transformations

| x' |   | 1  0  dx |   | x |
| y' | = | 0  1  dy | * | y | = T(dx, dy) * P
| 1  |   | 0  0  1  |   | 1 |
T(dx2, dy2)* T(dx1,dy1) * P
  | 1  0  dx2 |   | 1  0  dx1 |  
= | 0  1  dy2 | * | 0  1  dy1 | * P
  | 0  0  1   |   | 0  0  1   |  

  | 1  0  dx1+dx2 |
= | 0  1  dy1+dy2 | * P
  | 0  0     1    |

= T(dx2+dx2, dy1+dy2) * P
| x' |   | sx   0  0 |   | x |
| y' | = |  0  sy  0 | * | y | = S(sx, sy) * P
| 1  |   |  0   0  1 |   | 1 |
S(sx2, sy2)* S(sx1,sy1) * P
= ...

  | sx1*sx2     0      0 |
= |    0     sy1*sy2   0 | * P = S(sx1*sx2, sy1*sy2) * P
  |    0        0      1 |
| x' |   | cos A  -sin A  0 |   | x |
| y' | = | sin A   cos A  0 | * | y | = R(A) * P
| 1  |   |   0       0    1 |   | 1 |


Composing 2d transforms

  1. translate so that P at origin (0,0)
  2. rotate
  3. translate so P back at original

T(x1,y1) * R(A) * T(-x1,-y1)
  | 1  0  x1 |   | cos A  -sin A  0 |   | 1  0  -x1 |
= | 0  1  y1 | * | sin A   cos A  0 | * | 0  1  -y1 |
  | 0  0  1  |   |    0       0   1 |   | 0  0   1  |

  | cos A  -sin A    x1(1-cos A)+y1*sin A |
= | sin A   cos A    y1(1-cos A)-x1*sin A |
  |    0       0                1         |

2d transform composition

 M1

 M2

 translate

translate

scale

scale

rotate

rotate

scale (sx = sy)

rotate


Combined form for 2d Transforms

    |  r11  r12  tx  |
M = |  r21  r22  ty  |
    |   0    0    1  |


Coordinate systems: Local and Global


2d transforms: OpenGL implementation


2d transforms: OpenGL


OpenGL: Order of transformation operations

glScaled(1.0, 1.0, 3.0);
glRotatef(45.0, 0.0, 1.0, 0.0);
glTranslatef(3.0, 2.0, 0.0);
draw_my_triangle();


2D Transformation inversions

T(-dx, -dy) * T(dx, dy) = I

R(-A) * R(A) = I

S(1/sx, 1/sy) * S( sx, sy) = I

where I is the identity matrix.

Consider a sequence of transformations M:
P' = M * P
= (Tr1 * Tr2 * ... * TrK) * P

To invert M...

M^(-1) * P
= (Tr1 * Tr2 * ... * TrK)^(-1) * P
= TrK^(-1) * ... * Tr2^(-1) * Tr1^(-1) * P

Apply M^(-1) to M:

M^(-1) * M * P
= (TrK^(-1) * ... * Tr2^(-1) * Tr1^(-1)) * (Tr1 * Tr2 * ... * TrK) * P
(remove outer parentheses)...
= TrK^(-1) * ... * Tr2^(-1) * Tr1^(-1) * Tr1 * Tr2 * ... * TrK * P
= TrK^(-1) * ... * Tr2^(-1) * I * Tr2 * ... * TrK * P
= TrK^(-1) * ... * Tr2^(-1) * Tr2 * ... * TrK * P
= ...
= I * P


OpenGL: Saving and restoring contexts


OpenGL Matrix Modes


Properties of affine transformation matrix M


2D transforms: Shear transformations ("shearing")

 

       | 1  a  0 |            | 1  0  0 |
SHx =  | 0  1  0 |     SHy =  | b  1  0 |
       | 0  0  1 |            | 0  0  1 |

x' = x + ay             x' = x
y' = y                  y' = bx + y


3D Transforms


3D transforms

               | 1 0 0 dx |
T(dx,dy,dz) =  | 0 1 0 dy |
               | 0 0 1 dz |
               | 0 0 0 1  |
               | sx  0  0  0 |
S(sx,sy,sz) =  |  0 sy  0  0 |
               |  0  0 sz  0 |
               |  0  0  0  1 |
        | 1      0        0   0 |
Rx(A) = | 0  cos A   -sin A   0 |
        | 0  sin A    cos A   0 |
        | 0      0        0   1 |
 
        | cos A   0   sin A   0 |
Ry(A) = |     0   1       0   0 |
        | -sin A  0   cos A   0 |
        |     0   0       0   1 |

        | cos A  -sin A   0   0 |
Rz(A) = | sin A   cos A   0   0 |
        |     0       0   1   0 |
        |     0       0   0   1 |


3d transforms

z' = z cos A - x sin A

x' = z sin A + x cos A (as before)

--> but when you put these in matrix form, the X and Z are in reversed order, hence the sign change


3D transforms

    | r11  r12  r13  tx |
M = | r21  r22  r23  ty |
    | r31  r32  r33  tz |
    |   0    0    0   1 |
    | r11  r12  r13 |
R = | r21  r22  r23 |
    | r31  r32  r33 |
    
                    |  r11  -r21  r13 |
R^(-1) = (1/Det R)* | -r12  r22  -r32 |
                    |  r31  -r23  r33 |


3D transform properties


Non-affine transform: Fish-eye (angle halver)


Non-affine transforms: inversion in a unit circle


References



Back to COSC 3P98 index

COSC 3P98 Computer Graphics
Brock University
Dept of Computer Science
Copyright © 2020 Brian J. Ross (Except noted figures).
http://www.cosc.brocku.ca/Offerings/3P98/course/lectures/2d_3d_xforms/
Last updated:March 2, 2023