Computer Graphics Lecture 05
Computer Graphics Lecture 05
Lecture 6 – Object
Transformation
Department of Computer
Science Taif University - Taif –
Saudi Arabia
Computer Graphics 1
Transformation
• Transformation means changing some
graphics into something else by applying
rules.
• We can have various types of
transformations such as translation,
scaling up or down, rotation, shearing, etc.
• When a transformation takes place on a
2D plane, it is called 2D transformation.
• Transformations play an important role in
computer graphics to reposition the
graphics on the screen and change their
size or orientation.
Computer Graphics 2
Why use transformations?
• Position objects in a scene (modeling)
• Change the shape of objects
• Create multiple copies of objects
• Projection for virtual cameras
• Animations
Computer Graphics 3
Computer Graphics 4
Objects
Transformation
5-type of object Transformation:
◦ Translation or Shifting: moving objects
◦ Scaling: change size.
◦ Rotation: moving about a point by an
angle.
◦ Reflection: mirror of an object.
◦ Shearing: distort the object.
Computer Graphics 5
Computer Graphics 6
Translatio
n The object is shifted or moved to a
new coordinates.
Computer Graphics 8
Translation Example 1
Draw a square of length 2. One side of the square
vertices coordinates are (0,0), (2,0), (2,2), (0,2),
then translate it toward y-axis by 1.
Solution:
◦ Tx=0, Ty=1.
Solution: (3,3)
◦ x’= x + 1 and y’ = y+1. (2,2)
Old New
Coordinate Coordinate (4,1)
s s (2,1)
(1,0) (2,1) (1,0) (3,0)
(3,0) (4,1)
(2,2) (3,3)
Computer Graphics 10
Translation In OpenGL
glTranslate[fd](Tx, Ty, Tz): it will shift the whole coordinates
by Tx, Ty, Tz.
Example 1:
glColor3ub(255, 0, 0);
◦ glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(2,0);
glVertex2i(2,2);
glVertex2i(0,2);
◦ glEnd();
◦ glColor3ub(0, 0, 255);
◦ glTranslatef(1.0,1.0,0);
◦ glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(2,0);
glVertex2i(2,2);
glVertex2i(0,2);
◦ glEnd();
Computer Graphics 11
Translation In OpenGL
OpenGL Output
Computer Graphics 12
Scalin
g increase/decrease size of object
To scale an object we do the following:
◦ Shift object to point (0,0).
◦ Scale by (sx,sy) using the equation:
x’ = x*sx;
y’ = y*sy;
◦ Shift object back to its original location.
Computer Graphics 13
Scalin
gScaling is called uniform if sx=sy, otherwise
it is called differential.
In matrix form:
Computer Graphics 14
Scaling
Example:
A rectangle with coordinates (0,0), (4,0), (4,3), (0,3)
what it is the new coordinates if:
◦ Sx=Sy=2
◦ Sx=Sy=0.5
Solution:
(0,6) (8,6)
◦ Sx=Sy=2:
Old New
(4,3
Coordinates Coordinates (0,3) )
(0,0) (0,0)
(4,0) (8,0)
(4,3) (8,6) (0,0) (4,0) (8,0)
(0,3) (0, 6)
Computer Graphics 15
Scaling
Example:
Sx=Sy=0.5:
Old New
Coordinates Coordinates
(0,0) (0,0)
(4,0) (2,0)
(4,3) (2,1.5)
(0,3) (0, 1.5)
(4,3
(0,4) )
(0,1.5) (2,1.5)
Computer Graphics 16
Scaling Example
2 A square with coordinates: (1,1), (2,1), (2,2) ,(1,2).
Scale it by Sx=Sy=2, with (xf,yf)=(1,1).
Solution:
◦ Sx=Sy=2:
◦ Shift point (1,1) to (0,0)
(Xf,yf) = (1,1). Old New
Coordinates Coordinates
◦ x’ = xf + (x-xf)*sx;
(1,1) (1,1)
◦ y’ = yf + (y-yf)*sy
(2,1) (3,1)
(2,2) (3,3)
(1,2) (1,3)
Computer Graphics 17
Scaling
Example:
O u t pu t :
(1,3) (3,3)
(1,2) (2,2) (1,2)
(2,2)
Computer Graphics 18
OpenGL
Scaling
glScale[fd](Sx, Sy, Sz)
glBegin(GL_LINE_LOOP);
◦ glVertex2i(0,0);
◦ glVertex2i(4,0);
◦ glVertex2i(4,4);
◦ glVertex2i(0,4);
glEnd();
glScaled(2,2,1);
glBegin(GL_LINE_LOOP);
◦ glVertex2i(0,0);
◦ glVertex2i(4,0);
◦ glVertex2i(4,4);
◦ glVertex2i(0,4);
glEnd();
Computer Graphics 19
OpenGL
Scaling:
O ut p u
t:
Computer Graphics 20
Rotation
In linear algebra, a rotation matrix is a
transformation matrix that is used to perform a rotation in
Euclidean space. For example, using the convention below,
the matrix
Computer Graphics 21
Thus, the new coordinates (x′, y′) of a point (x, y) after rotation are
(x’,y’)
(x,y
)
(xf,yf)
Computer Graphics 23
Rotation
Example
Consider a rectangle with coordinates (1,1), (3,1),
(3,5), (1,5). Rotate it 45o about the point (1,1).
Solution:
◦ (xf,yf)=(1,1), = 45o.
◦ So, x’ = xf + (x-xf)*cos - (y-yf)sin
◦ And, y’ = yf + (x-xf)sin + (y-yf)*cos
Old Coordinates New Coordinates
(1,1) (1,1)
(3,1) (2.4142, 2.4142)
(3,5) (-0.4142 5.2426)
(1,5) (-1.828, 3.828)
Computer Graphics 24
Computer Graphics 25
Rotation
In Opengl
◦ glRotatef(angle, x, y, z).
Computer Graphics 26
Rotation
glBegin(GL_LINE_LOOP);
◦ glVertex2i(0,0);
◦ glVertex2i(4,0);
◦ glVertex2i(4,4);
◦ glVertex2i(0,4);
glEnd();
glRotatef(45, 0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
◦ glVertex2i(0,0);
◦ glVertex2i(4,0);
◦ glVertex2i(4,4);
◦ glVertex2i(0,4);
glEnd();
Computer Graphics 27
Rotation
Output:
Computer Graphics 28
Reflectio
n The equation for reflection with respect to x-axis
◦ For each point (x, y)
◦ New point (x, -y).
Computer Graphics 29
Reflectio
n The equation for reflection with respect to y-axis
◦ For each point (x, y)
◦ New point (-x, y).
Computer Graphics 30
Reflectio
n The equation for reflection with respect to origin
◦ For each point (x, y)
◦ New point (-x, -y).
Computer Graphics 31
Reflection
Example
Consider a rectangle with coordinates (1,1), (3,1),
(3,5), (1,5). Reflect it with respect to x-axis
Solution:
Old Coordinates New Coordinates
(1,1) (1, -1)
(3,1) (3, -1)
(3,5) (3, -5)
(1,5) (1, -5)
Computer Graphics 32
Reflection
Example
Consider a rectangle with coordinates (1,1), (3,1),
(3,5), (1,5). Reflect it with respect to y-axis
Solution:
Old Coordinates New Coordinates
(1,1) (-1, 1)
(3,1) (-3, 1)
(3,5) (-3, 5)
(1,5) (-1, 5)
Computer Graphics 33
Reflection
Example
Consider a rectangle with coordinates (1,1), (3,1),
(3,5), (1,5). Reflect it with respect to origin
Solution:
Old Coordinates New Coordinates
(1,1) (-1, -1)
(3,1) (-3, -1)
(3,5) (-3, -5)
(1,5) (-1, -5)
Computer Graphics 34
Homogeneous Coordinates
Computer Graphics 36
Computer Graphics 37
Computer Graphics 38
Computer Graphics 39
Computer Graphics 40
Computer Graphics 41
Computer Graphics 42
Computer Graphics 43
Computer Graphics 44