Assignment No 4
Assignment No 4
4
Title Basic 2-D Transformations.
Aim/Problem a) Write C++ program to draw 2-D object and perform following basic
Statement transformations: Scaling, Translation, Rotation. Apply the concept
of operator
overloading.
OR
b) Write C++ program to implement translation, rotation and scaling
transformations on equilateral triangle and rhombus. Apply the concept
of
operator overloading.
CO Mapped CO 4
Pre-requisite 1. Basic programming skills of C++
2. 64-bit Open source Linux
3. Open Source C++ Programming tool like G++/GCC
Learning Objective To learn and apply basic transformations on 2-D objects.
Theory:
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,
reflection 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. Translation, Scaling and Rotation are basic
transformations.
1) Translation:
A translation moves an object to a different position on the screen. You can translate a point in
2D by adding translation coordinate or translation vector (Tx, Ty) to the original coordinates.
Consider
Initial coordinates of the object O = (Xold, Yold)
New coordinates of the object O after translation = (Xnew, Ynew)
Translation vector or Shift vector = (Tx, Ty)
This translation is achieved by adding the translation coordinates to the old coordinates of the
object as-
2) Rotation:
In rotation, we rotate the object at particular angle θ (theta) from its original position. Consider
- Initial coordinates of the object O = (Xold, Yold)
- Initial angle of the object O with respect to origin = Φ
- Rotation angle = θ
- New coordinates of the object O after rotation = (Xnew, Ynew)
3) Scaling:
Scaling transformation is used to change the size of an object. In the scaling process, you either
expand or compress the dimensions of the object. Scaling can be achieved by multiplying the
original coordinates of the object with the scaling factor (S x, Sy). If scaling factor > 1, then the
object size is increased. If scaling factor < 1, then the object size is reduced. Consider
-
Initial coordinates of the object O = (Xold, Yold)
-
Scaling factor for X-axis = Sx
-
Scaling factor for Y-axis = Sy
-
New coordinates of the object O after scaling = (Xnew, Ynew)
This scaling is achieved by using the following scaling equations-
Xnew = Xold x Sx
Ynew = Yold x Sy
Homogeneous Coordinates:
Matrix multiplication is easier to implement in hardware and software as compared to matrix
addition. Hence we want to replace matrix addition by multiplication while performing
transformation operations. So the solution is homogeneous coordinates , which allows us to
express all transformations (including translation) as matrix multiplications.
To obtain homogeneous coordinates we have to represent transformation matrices in 3x3
matrices instead of 2x2. For this we add dummy coordinate. Each 2 dimensional position (x,y) can
be represented by homogeneous coordinate as (x,y,1).
Consider that coordinates of vertices of equilateral triangle are (X1,Y1), (X2,Y2) and
(X3,Y3). After applying basic transformations, we will get corresponding coordinates as
(X1’,Y1’), (X2’,Y2’) and (X3’,Y3’) respectively. Following multiplication will give the
translation on this equilateral triangle:
Consider that coordinates of vertices of rhombus are (X1,Y1), (X2,Y2), (X3,Y3) and (X4,Y4)
applying basic transformations, we will get corresponding coordinates as (X1’,Y1’),
(X2’,Y2’), (X3’,Y3’) and (X4’,Y4’) respectively. Following multiplication will give the
translation on this rhombus:
Conclusion:
Questions:
1. How to rotate any 2-D object about an arbitrary point? Explain in brief.
2. Explain the concept of operator overloading with example.