Practical 6: Write a program to apply various 2D transformations on a 2D object
AIM:
To implement the following 2D Geometric Transformations
a. Translation b. Rotation c. Scaling d. Reflection e. Shearing
f. Window-View Port
ALGORITHM:
1. Get the coordinates of triangle (x1, y1, x2, y2, x3, y3).
2. Draw the original triangle.
3. Print the menu for choosing 2D Geometric Transformation.
a. If users choose translation then get the translation factors(x, y) and draw the translated triangle
in the following coordinates (x1+x, y1+y, x2+x, y2+y, x3+x, y3+y).
b. (i) If user choose rotation then get the rotation angle (t) and reference point of the rotation
(rx, ry).
(ii) Change the t value to t = t * (3.14 / 180) and calculate the rotated coordinates by the
following formulae rx1 = rx + (x1 - rx) * cos (t) - (y1 - ry) * sin (t); ry1 = ry + (x1 - rx) *
sin (t) + (y1 - ry) *cos (t);
(iii) Similarly calculate the coordinates rx2, ry2, rx3, ry3 and draw the rotated triangle in the
following coordinates (rx1, ry1, rx2, ry2, rx3, ry3).
c. If user choose scaling then get the scaling factors(x, y) and draw the scaleded triangle in the
following coordinates (x1*x, y1*y, x2*x, y2*y, x3*x, y3*y).
d. If user choose reflection then rotate the triangle in 1800 at (x2, y2) and draw the rotated
triangle (which is reflected triangle).
e. If user choose shearing then get the shear value and draw the sheared triangle in the following
coordinates (x1, y1, x2+x, y2, x3, y3).
f. (i) if user choose window-view port then draw the rectangle in window port coordinates (w1,
w2, w3, w4) and draw the original triangle.
(ii) Calculate the x, y and view port coordinates by following formulae
x = (v3 - v1) / (w3 - w1);
y = (v4 - v2) / (w4 - w2);
vx1 = v1 + floor (((x1 - w1) * x) + 0.5);
vy1 = v2 + floor (((y1 - w2) * y) + 0.5);
(iii) Similarly calculate the coordinates vx2, vy2, vx3, vy3
(iv) Draw the rectangle in view port coordinates (v1, v2, v3, v4) and draw the triangle in view
Port by the following coordinates (vx1, vy1, vx2, vy2, vx3, vy3)
PROGRAM:
#include <graphics.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
float x1, y1, x2, y2, x3, y3, x, y, rx1, ry1, rx2, ry2, rx3, ry3, t, vx1, vy1, vx2, vy2, vx3, vy3;
floatw1 = 5, w2 = 5, w3 = 635, w4 = 465, v1 = 425, v2 = 75, v3 = 550, v4 = 250;
intgd, gm, ch;
void original ();
void triangle (float, float, float, float, float, float);
void rotate (float, float, float);
void main ()
{
clrscr ();
cout<< "\n\t\t ***** 2D Geometric Transformations *****";
cout<< "\n\n Enter the coordinates of triangle (x1,y1,x2,y2,x3, y3): \n";
cin>> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
original ();
closegraph ();
do
{
cout<< "\n\n Choose any one Transformation : ";
cout<< "\n\t [Link] \n\t [Link] \n\t [Link] \n\t [Link]";
cout<< "\n\t [Link] \n\t [Link]-Viewport";
cout<< "\n\n Enter your choice : \t";
cin>>ch;
switch (ch)
{
case 1:
cout<< "\n Enter the translation factors (x,y): \t\t";
cin>> x >> y;
original ();
triangle (x1+x, y1+y, x2+x, y2+y, x3+x, y3+y);
closegraph ();
break;
case 2:
cout<< "\n Enter the angle of rotation : \t\t";
cin>> t;
cout<< "\n Enter the reference point of rotation (rx,ry) : \t";
cin>> x >> y;
original ();
rotate (t, x, y);
closegraph ();
break;
case 3:
cout<< "\n Enter the scaling factors (x,y): \t\t";
cin>> x >> y;
original ();
triangle (x1*x, y1*y, x2*x, y2*y, x3*x, y3*y);
closegraph ();
break;
case 4:
original ();
rotate (180, x2, y2);
closegraph ();
break;
case 5:
cout<< "\n Enter the Shear Value : \t\t";
cin>> x;
original ();
triangle (x1, y1, x2+x, y2, x3, y3);
closegraph ();
break;
case 6:
initgraph (&gd, &gm, "C:\\TC\\BGI");
rectangle (w1, w2, w3, w4);
outtextxy (300, 10, "Window Port");
triangle (x1, y1, x2, y2, x3, y3);
x = (v3 - v1) / (w3 - w1);
y = (v4 - v2) / (w4 - w2);
vx1 = v1 + floor (((x1 - w1) * x) + 0.5);
vy1 = v2 + floor (((y1 - w2) * y) + 0.5);
vx2 = v1 + floor (((x2 - w1) * x) + 0.5);
vy2 = v2 + floor (((y2 - w2) * y) + 0.5);
vx3 = v1 + floor (((x3 - w1) * x) + 0.5);
vy3 = v2 + floor (((y3 - w2) * y) + 0.5);
rectangle (v1, v2, v3, v4);
outtextxy (450, 85, "View Port");
triangle (vx1, vy1, vx2, vy2, vx3, vy3);
closegraph ();
}
} while (ch<= 6);
getch ();
}
void original ( )
{
initgraph (&gd, &gm, "C:\\TC\\BGI");
triangle (x1, y1, x2, y2, x3, y3);
}
void triangle (float x1, float y1, float x2, float y2, float x3, float y3)
{
line (x1, y1, x2, y2);
line (x2, y2, x3, y3);
line (x3, y3, x1, y1);
getch ();
}
void rotate (float t, float rx, float ry)
{
t = t * (3.14 / 180);
rx1 = rx + (x1 - rx) * cos (t) - (y1 - ry) * sin (t);
ry1 = ry + (x1 - rx) * sin (t) + (y1 - ry) * cos (t);
rx2 = rx + (x2 - rx) * cos (t) - (y2 - ry) * sin (t);
ry2 = ry + (x2 - rx) * sin (t) + (y2 - ry) * cos (t);
rx3 = rx + (x3 - rx) * cos (t) - (y3 - ry) * sin (t);
ry3 = ry + (x3 - rx) * sin (t) + (y3 - ry) * cos (t);
triangle (rx1, ry1, rx2, ry2, rx3, ry3);
}
OUTPUT:
(If we choose 6 then we get)
(If we choose the number is in greater than 6 then we exit)