0
632views
Write a program in object oriented language for 2D transformation which include functions (i) translation (ii) scaling (iii) Rotation
1 Answer
written 5.4 years ago by |
Answer:
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main()
{
int x1,y1,x2,y2,x3,y3,option;
float tx,ty;
float sx,sy;
float deg,th;
float xt1,yt1,xt2,yt2,xt3,yt3;
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,”C:\TC\BGI”);
cout<<“Enter coordinates of a triangle: “;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
cout<<“\nEnter 1 for Translation, 2 for Scaling and 3 for Rotation : “;
cin>>option;
if(option==1)
{
cout<<“Enter tx & ty: “;
cin>>tx>>ty;
xt1=x1+tx;
yt1=y1+ty;
xt2=x2+tx;
yt2=y2+ty;
xt3=x3+tx;
yt3=y3+ty;
line(xt1,yt1,xt2,yt2);
line(xt2,yt2,xt3,yt3);
line(xt3,yt3,xt1,yt1);
}
else if(option==2)
{
cout<<“Enter sx & sy: “;
cin>>sx>>sy;
xt1=x1*sx;
yt1=y1*sy;
xt2=x2*sx;
yt2=y2*sy;
xt3=x3*sx;
yt3=y3*sy;
line(xt1,yt1,xt2,yt2);
line(xt2,yt2,xt3,yt3);
line(xt3,yt3,xt1,yt1);
}
else if(option==3)
{
cout<<“Enter angle: “;
cin>>deg;
th = deg*3.14/180;
xt1 = x1*cos(th)-y1*sin(th);
yt1 = x1*sin(th)+y1*cos(th);
xt2 = x2*cos(th)-y2*sin(th);
yt2 = x2*sin(th)+y2*cos(th);
xt3 = x3*cos(th)-y3*sin(th);
yt3 = x3*sin(th)+y3*cos(th);
line(xt1,yt1,xt2,yt2);
line(xt2,yt2,xt3,yt3);
line(xt3,yt3,xt1,yt1);
}
else
{
cout<<“Invalid choice”;
}
getch();
}