Search This Blog

PRACTICAL NO. 3 TITLE: Write C++/Java program to draw 2-D object and perform following basic transformations, a) Scaling b) Translation c) Rotation Use operator overloading.

 

PRACTICAL NO. 3

TITLE: Write C++/Java program to draw 2-D object and perform following basic transformations,
a) Scaling
b) Translation
c) Rotation
Use operator overloading.

#include<iostream> #include<graphics.h> #include<math.h> using namespace std; void Display(int n,float c[10][3]) { float maxx,maxy; int i; maxx=getmaxx(); maxy=getmaxy(); maxx=maxx/2; maxy=maxy/2; i=0; while(i<n-1) { line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]); i++; } i=n-1; line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]); line(0,maxy,maxx*2,maxy); line(maxx,0,maxx,maxy*2); } void Multi(int n,float b[10][3],float c[10][3],float a[10][3]) { int i,j,k; for(i=0;i<n;i++) { for(j=0;j<3;j++) { a[i][j]=0; } } for(i=0;i<n;i++) { for(j=0;j<3;j++) { for(k=0;k<3;k++) { a[i][j]=a[i][j]+(c[i][k]*b[k][j]); } } } } void Translation(int n,float c[][3],float tx,float ty) { float b[10][3],a[10][3]; int i=0,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; } } b[0][0]=1; b[1][1]=1; b[2][0]=tx; b[2][1]=ty; b[2][2]=1; Multi(n,b,c,a); Display(n,a); delay(5000); } void Scaling(int n,float c[][3],float sx,float sy) { float b[10][3],a[10][3]; int i=0,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; } } b[0][0]=sx; b[1][1]=1; b[0][1]=sy; b[0][2]=1; Multi(n,b,c,a); Display(n,a); delay(5000); } void Rotation(int n,float c[][3],float ra) { int i=0,j; float b[10][3],xp,yp,a[10][3]; xp=c[0][0]; yp=c[0][1]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; } } b[0][0]=b[1][1]=cos(ra*3.14/180); b[0][0]=sin(ra*3.14/180); b[1][0]=-sin(ra*3.14/180); b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp; b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp; b[2][2]=1; Multi(n,b,c,a); Display(n,a); delay(5000); } int main() { int i,j,k,iCh,gd=DETECT,n,gm,ch,a; float c[10][3],tx,ty,sx,sy,R,ra; cout<<"\n Enter number of co-ordinates :"; cin>>n; for(i=0;i<n;i++) { cout<<"\n Enter the co-ordinates of vertex:"; cin>>c[i][0]>>c[i][1]; c[i][2]=1; } initgraph(&gd,&gm,NULL); Display(n,c); delay(5000); closegraph(); do { cout<<"*********************MENU**********************"; cout<<"\n1. Translation..."; cout<<"\n2. Scaling..."; cout<<"\n3. Rotation..."; cout<<"\n4. Exit..."; cout<<"\nEnter your choice: "; cin>>ch; switch(ch) { case 1: cout<<"\n Enter the translation factor for x and y axis:"; cin>>tx>>ty; initgraph(&gd,&gm,NULL); Translation(n,c,tx,ty); delay(5000); closegraph(); break; case 2: cout<<"\n Enter the scaling factor for x and y axis:"; cin>>sx>>sy; initgraph(&gd,&gm,NULL); Scaling(n,c,sx,sy); delay(5000); closegraph(); break; case 3: cout<<"\n Enter the angle of rotation:"; cin>>ra; initgraph(&gd,&gm,NULL); Rotation(n,c,ra); delay(5000); closegraph(); break; case 4: exit(0); break; } }while(ch!=4); closegraph(); delay(5000); return 0; }

No comments:

Post a Comment