2D transformations using OpenGL


Transformations are a fundamental part of computer graphics. Transformations are used to position objects, to shape objects, to change viewing positions, and even to change how something is viewed (e.g. the type of perspective that is used).

There are 5 main types of transformations that one can perform in 2 dimensions:

  • translations
  • scaling
  • rotation
  • shearing
  • reflection

These basic transformations can also be combined to obtain more complex transformations. In order to make the representation of these complex transformations easier to understand and more efficient, the idea of homogeneous coordinates is used.

Translation in 2D

Point (X,Y) is to be translated by amount Tx and Ty to a new location (X’,Y’)

or P’ = T * P where

      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _    _
T  = |  1  0  Tx  |
     |  0  1  Ty  |
     |  0  0  1   |
      -    -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -

Scaling in 2D

Point (X,Y) is to be scaled by amount Sx and Sy to location (X’,Y’) with reference to co-ordinates (H,K)

P’ = S * P where

      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _               _
S  = |  Sx  0   H(1-Sx)|
     |  0   Sy  K(1-Sy)|
     |  0   0      1   |
      -               -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -

Rotation in 2D

Point (X,Y) is to be rotated about the coordinates (H,K) by angle “theta” to location (X’,Y’)

or P’ = R * P where

      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _                                                   _
R  = |  cos(theta) -sin(theta)   H(1-Cos(theta)+KSin(theta)|
     |  sin(theta) cos(theta)    K(1-Cos(theta)-HSin(theta)|
     |      0           0                    1             |
      -                                                   -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -

Reflection in 2D

Point (X,Y) is to be reflected about the X-axis to location (X’,Y’)

or P’ = R * P where

      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _         _
R  = |  1   0   0|
     |  0  -1   0|
     |  0   0   1|
      -         -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -

Point (X,Y) is to be reflected about the Y-axis to location (X’,Y’)

or P’ = R * P where

      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _         _
R  = | -1   0   0|
     |  0   1   0|
     |  0   0   1|
      -         -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -

Shearing in 2D

Point (X,Y) is to be sheared about the coordinates (H,K) with shear coefficients (SHx,SHy) to location (X’,Y’)

or P’ = S * P where

      _   _
P' = |  X' |
     |  Y' |
     |  1  |
      -   -
      _   _
P  = |  X  |
     |  Y  |
     |  1  |
      -   -
X Direction Shear
       _                _
Sx  = |  1   SHx   -SHx*K|
      |  0    1       0  |
      |  0    0       1  |
       -                -
Y Direction Shear
       _                _
Sy  = |  1     0      0  |
      |  SHy   1   -SHy*H|
      |  0     0      1  |
       -                -

Next:

Leave a comment