0% found this document useful (0 votes)
29 views

Computer Graphics Lecture 05

The document discusses various types of 2D object transformations including translation, scaling, rotation, reflection, and shearing. Transformation operations are used to reposition or modify graphics and play an important role in computer graphics and animation. The key transformation operations and their mathematical definitions are explained.

Uploaded by

d4rx5wsrw5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Computer Graphics Lecture 05

The document discusses various types of 2D object transformations including translation, scaling, rotation, reflection, and shearing. Transformation operations are used to reposition or modify graphics and play an important role in computer graphics and animation. The key transformation operations and their mathematical definitions are explained.

Uploaded by

d4rx5wsrw5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Computer Graphics

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.

 The shift is by (dx, dy).


◦ x’ = x + dx
◦ y’ = y + dy.

In matrix forms:  x'  x  dx


 y'   y  dy
Computer Graphics 7
Translatio
n To translate a line, translate its end
points.
 To translate a polygon, translate it
vertices.
 To translate a circle or ellipse its
center coordinates are transformed,
then the object is drawn (boundary
points) using 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.

x’=x, y’=y+1 (0,3)


(2,3)
Old New
Coordinate Coordinate (0,2) (2,2)
s s
(0,1) (2,1)
(0,0) (0,1)
(2,0) (2,1) (0,0)
(2,0)
(2,2) (2, 3)
(0,2) (0,3) Computer Graphics 9
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)
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.

 In general, scaling equations are:


◦ x’ = xf + (x-xf)*sx;
◦ y’ = yf + (y-yf)*sy;
◦ Where (xf, yf) is the reference point.

Computer Graphics 13
Scalin
gScaling 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)

(0,0) (2,0) (4,0)

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)

(1,1) (2,1) (3,1)


(1,1) (2,1)

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

rotates 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 21
Thus, the new coordinates (x′, y′) of a point (x, y) after rotation are

• The direction of vector rotation is counterclockwise


if θ is positive (e.g. 90°), and clockwise if θ is
negative (e.g. −90°). Thus, the clockwise rotation
matrix is found as

x' = x cos θ + y sin θ


y' = -x sin θ + y cos θ
Computer Graphics 22
Rotation
 The equation for rotation about a reference point
(xf,yf) is:

x'  x f  (x  x f ) cos  ( y  y f ) sin


y'  y f  (x  x f ) sin  ( y  y f ) cos

(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).

◦ Rotation 45o about the z-axis:


 glRotatef(45, 0.0, 0.0, 1.0).

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

You might also like