CG Lab Assignment No 4
CG Lab Assignment No 4
CG Lab Assignment No 4
Problem statement: Write C++/Java program to draw 2-D object and perform following basic
transformations,
a) Scaling
b) Translation
c) Rotation
Prerequisites:
Course objective:
1. To acquaint the learner with the basic concepts of Computer Graphics
2. To get familiar with mathematics behind the graphical transformations
Course outcome:
1. Develop scientific and strategic approach to solve complex problems in the domain of
Computer Graphics
Theory:
Transformations allow us to uniformly alter the entire picture. The geometric transformations
considered here - translation, scaling and rotation are expressed in terms of matrix multiplication.
Example:
Homogenous Coordinates
To shorten this process, we have to use 3×3 transformation matrix instead of 2×2 transformation
matrix. To convert a 2×2 matrix to 3×3 matrix, we have to add an extra dummy coordinate W.
In this way, we can represent the point by 3 numbers instead of 2 numbers, which is called
Homogenous Coordinate system. In this system, we can represent all the transformation
equations in matrix multiplication.
Scaling
Scaling refers to changing the size of the object either by increasing or decreasing. We will
increase or decrease the size of the object based on scaling factors along x and y-axis.
Scaling can be achieved by multiplying the original coordinates of the object with the scaling
factor to get the desired result.
The scaling factor SX, SY scales the object in X and Y direction respectively. The above
equations can also be represented in matrix form as below −
X Sx
’ X 0
Y
’ = Y 0
Sy
scaling is performed about the origin (0,0) not about the center of the line/polygon/whatever
uniform scaling: Sx = Sy
differential scaling Sx != Sy -> alters proportio
If we provide values less than 1 to the scaling factor S, then we can reduce the size of the object.
If we provide values greater than 1, then we can increase the size of the object.
Rotation
In rotation, we rotate the object at particular angle θ (theta) from its origin. From the following
figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate
with distance r from the origin.
Using standard trigonometric the original coordinate of point P(X, Y) can be represented as −
(
1
X = r cosϕ...... )
(2
Y = r sinϕ...... )
Same way we can represent the point P’ (X’, Y’) as −
x′ = r cos(ϕ+θ) = r cosϕ cosθ − r sinϕ (3
sinθ ....... )
(
y′ = r sin(ϕ+θ) = r cosϕ sinθ + r sinϕ 4
cosθ....... )
Substituting equation (1) & (2) in (3) & (4) respectively, we will
get
x′ = x cosθ –y sinθ
y′ = x sinθ + y
cosθ
Representing the above equation in matrix form,
X’
cos θ -sin θ
Y’ X’
= Y’ sin θ cos θ
OR P’ = P . R
The rotation angle can be positive and negative.
cos -sin
θ θ
R= cos
sin θ θ
For positive rotation angle, we can use the above rotation matrix. However, for negative angle
rotation, the matrix will change as shown below –
Cos(-
θ) -sin (-θ)
R sin (-
= θ) cos (-θ)
cos θ sin θ
R= -sin θ cos θ
Translation
A translation moves an object to a different position on the screen. You can translate a point in
2D by adding translation coordinate (tx, ty) to the original coordinate (X, Y) to get the new
coordinate (X’, Y’).
Translation Distance: It is nothing but by how much units we should shift the object from one
location to another along x, y-axis.
X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector. The above equations can also be
represented using the column vectors.
P = [X] / [Y]
p' = [X′] / [Y′]
T = [tx] / [ty]
We can write it as - P’ = P + T
Shear
A transformation that slants the shape of an object is called the shear transformation. There are
two shear transformations X-Shear and Y-Shear. One shift X coordinate’s values and other shifts
Y coordinate values. However; in both the cases only one coordinate changes its coordinates.
X-Shear
The X-Shear preserves the Y coordinate and changes are made to X coordinates, which causes
the vertical lines to tilt right or left as shown in below figure.
1 0 0
Xsh = Shx 1 0
0 0 1
X' = X + Shx . Y
Y’ = Y
Y-Shear
The Y-Shear preserves the X coordinates and changes the Y coordinates which causes the
horizontal lines to transform into lines which slopes up or down as shown in the following
figure.
The Y-Shear can be represented in matrix from as −
1 Shy 0
0 1 0
Ysh =
0 0 1
Y’ = Y + Shy . X
X’ = X
Reflection
Reflection in computer graphics is used to emulate reflective objects like mirrors and shiny
surfaces.
Homogeneous Matrices:
Translation Scaling
Sx 0 0
1 0 0
0 Sy 0
0 1 0
0 0 1
Tx Ty 1
X- Shear Y
-
S
h
e
a
r
1 0 0 1 Shy 0
Shx 1 0 0 1 0
0 0 1 0 0 1
Algorithm:
1. Read input object in the form of matrix
2. Multiply input matrix with transformation matrix
3. Repeat same for all transformations
4. Display transformed object
5. stop
OOP’s concepts used
1. Class
2. Operator Overloading
FAQs:
0
1. Perform a counterclockwise 45 rotation of triangle A(2,3), B(5,5), C(4,3) about
point (1,1).
0
2. A point (4,3) is rotated counterclockwise by an angle of 45 . Find the rotation matrix
and the resultant point.
3. Translate the polygon with co-ordinates A(2,5), B(7,10) and C(10,2) by 3 units
in x direction and 4 units in y direction.