COMPUTER GRAPHICS
501472-3
Lecture 6- 2D/3D transformation
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.
• 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
Why Transformations?
■ In graphics, once we have an object described, transformations are used to move
that objects, scale it and rotate it.
Computer Graphics 4
ObjectsTransformation
• 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 (deformation).
Reflect Translate Scale Rotate Shear
TRANSLATION
Translation
■ Simply moves an object from position to another
Computer Graphics 7
Translation
• The object is shifted or moved to a new coordinates.
• The shift is by (dx, dy).
• x’ = x + dx
• y’ = y + dy.
• In matrix forms: é x'ù é xù édxù
ê y'ú = ê yú + êdyú
ë û ë û ë û
Translation
• To translate a line, translate its end points.
• To translate a polygon, translate its vertices.
• To translate a circle or ellipse, its center coordinates are
transformed, then the object is drawn (boundary points)
using new coordinates.
Computer Graphics 9
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: x’=x, y’=y+1
• dx=0, dy=1. Old New
(0,3)
Coordinates Coordinates (2,3)
(0,0) (0,1)
(0,2) (2,2)
(2,0) (2,1)
(2,2) (2,3) (0,1) (2,1)
(0,2) (0,3)
(0,0)
(2,0)
Computer Graphics 10
Translation Example 2:
■ A triangle with vertices coordinates: (1,0), (3,0), (2,2). What its new coordinates
after shifting it by 1 in the x-axis and y-axis.
■ Solution:
(3,3)
– x’= x + 1 and y’ = y+1.
(2,2)
Old New
Coordinate Coordinate (4,1)
(2,1)
s s
(1,0) (2,1) (1,0) (3,0)
(3,0) (4,1)
(2,2) (3,3)
Computer Graphics 11
Translation In OpenGL
glTranslate[fd](Tx, Ty, Tz): It will shift the whole coordinates by Tx, Ty, Tz.
■ glTranslated() takes double-precision floating-point values.
■ glTranslatef() takes single-precision floating-point values.
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); // Translate the coordinate system by
(1.0, 1.0, 0)
glBegin(GL_LINE_LOOP);
glVertex2i(0,0); glVertex2i(2,0); glVertex2i(2,2);
glVertex2i(0,2);
glEnd();
Computer Graphics 12
SCALING
Scaling
• Scaling: increase/decrease size of object
• To scale an object, we do the following:
1. Shift object to point (0,0).
2. Scale by (sx, sy) using the equations: x’ = x*sx; y’ = y*sy;
3. Shift object back to its original location.
• In general, scaling equations are:
• x’ = xf + (x-xf)*sx; y’ = yf + (y-yf)*sy;
• Where (xf, yf) is the reference point.
Computer Graphics 14
Scaling
• Scaling is called uniform if sx=sy, otherwise it is called differential.
• In matrix form:
x′ Sx 0 x
=
y′ 0 Sy y
■ To apply the scaling transformation to a point (x,y,1), we use matrix multiplication
Computer Graphics 15
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 Old New
Coordinates Coordinates (8,6)
• Solution: (0,6)
(0,0) (0,0)
• Sx=Sy=2:
(4,0) (8,0) (4,3)
(0,3)
(4,3) (8,6)
(0,3) (0,6)
(0,0) (4,0) (8,0)
Scaling Example:
■ Sx=Sy=0.5:
Old New
Coordinates Coordinates
(0,0) (0,0)
(4,0) (2,0) (4,3)
(0,4)
(4,3) (2,1.5)
(0,3) (0,1.5) (0,1.5) (2,1.5)
(0,0) (2,0) (4,0)
Computer Graphics 17
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:
Old New
◦ Sx=Sy=2:
Coordinates Coordinates
◦ Shift point (1,1) to (0,0)
(1,1) (1,1)
■ (Xf,yf) = (1,1).
(2,1) (3,1)
◦ x’ = xf + (x-xf)*sx;
(2,2) (3,3)
◦ y’ = yf + (y-yf)*sy
(1,2) (1,3)
Computer Graphics 18
Scaling Example 2
(1,3) (3,3)
(1,2) (2,2) (1,2)
(2,2)
(1,1) (2,1) (3,1)
(1,1) (2,1)
Computer Graphics 19
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); // Apply scaling with scaling factors (2, 2, 1)
glBegin(GL_LINE_LOOP);
glVertex2i(0,0); glVertex2i(4,0); glVertex2i(4,4); glVertex2i(0,4);
glEnd();
Computer Graphics 20
OpenGL Scaling:
ROTATION
Rotation
• In linear algebra, a rotation matrix is used to perform
a rotation in Euclidean space.
• For example, rotating points in the xy plane
counterclockwise through an angle θ with respect to
the positive x axis about the origin of a 2D Cartesian
coordinate system.
• By means of the following matrix multiplication,
Computer Graphics 24
Rotation
• Thus, the new coordinates (xʹ, yʹ) of a point (x, y) after a 2D rotation
around the origin (0,0) by an angle θ in the counterclockwise
direction is:
• x' = x cos θ - y sin θ ; y' = x sin θ + y cos θ
• The equation for rotation about a reference point (xf,yf) is:
(x’,y’)
• x' = x f + (x - x f ) cosq - ( y - y f ) sinq (x,y)
q
• y' = y f + (x - x f ) sinq + ( y - y f ) cosq
(xf,yf)
Rotation Example
• Consider a rectangle with coordinates (1,1), (3,1), (3,5), (1,5).
Rotate it 45o about the point (1,1).
Old Coordinates New Coordinates
• Solution:
(1,1) (1,1)
• (xf,yf)=(1,1), q = 45o.
(3,1) (2.4142, 2.4142)
• cos 45 = 0.71, sin 45 = 0.71,
(3,5) (-0.4142 5.2426)
• So, x’ = xf + (x-xf) * cos q - (y-yf)sin q (1,5) (-1.828, 3.828)
• And y’ = yf + (x-xf) * sin q + (y-yf)*cosq
Computer Graphics 26
Rotation In Opengl
• glRotatef(angle, x, y, z).
• Rotation 45o about the z-axis:
• glRotatef(45, 0.0, 0.0, 1.0).
Computer Graphics 27
Rotation In Opengl
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 28
Rotation In Opengl
Computer Graphics 29
REFLECTION
Reflection
• The equation for reflection with respect to x-axis
• For each point (x, y)
• New point (x, -y).
Computer Graphics 34
Reflection
• The equation for reflection with respect to y-axis
• For each point (x, y)
• New point (-x, y).
Computer Graphics 35
Reflection
• The equation for reflection with respect to origin
• For each point (x, y)
• New point (-x, -y).
Computer Graphics 36
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 37
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 38
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 39