Lecture7 Transformation
Lecture7 Transformation
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?
Computer Graphics 3
Why Transformations?
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).
Computer Graphics 7
Translation
Computer Graphics 9
Translation Example 1
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
Computer Graphics 15
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)
Computer Graphics 17
Scaling Example 2
■ 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)
Computer Graphics 19
OpenGL Scaling
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.
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 θ
Computer Graphics 26
Rotation In Opengl
• glRotatef(angle, x, y, z).
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
Computer Graphics 34
Reflection
Computer Graphics 35
Reflection
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
Computer Graphics 39