Transformations computer graphics on Geometric Figures using C++ Language
Write a program to Transformations computer graphics using C++ Language like ..
- Rotation
- Shear
- Translation
Transformations computer graphics using C++ Language Code
#include #include #include #include class transformation { int edge,v[50],sx,sy,input[20][3],scale[3][3],result[20][3],clk,midx,midy; float ang,scale1[3][3],result1[20][3]; public: void input1() { clrscr(); cout<<"\nENTER NO OF EDGES"; while(1) { cin>>edge; if(edge<3) cout<<"\nEDGE SHOULD NOT LESS THAN 3"; else break; } cout<<"\nENTER VERTICES"; int j=0; for(int i=0;i<edge;i++) <br=""> { cout<<"x-cordinate"; cin>>v[j]; input[i][0]=v[j++]; cout<<"y-cordinate"; cin>>v[j]; input[i][1]=v[j++]; input[i][2]=1; } } void display() { clrscr(); int j=0; midx=getmaxx()/2; midy=getmaxy()/2; setcolor(WHITE); line(0,midy,2*midx,midy); line(midx,0,midx,2*midy); setcolor(BLUE); for(int i=0;i<edge;i++) <br=""> { if(edge-i!=1) line(v[j]+midx,midy-v[j+1],v[j+2]+midx,midy-v[j+3]); else line(v[j]+midx,midy-v[j+1],v[0]+midx,midy-v[1]); j=j+2; } getch(); } void display1() { int j=0; midx=getmaxx()/2; midy=getmaxy()/2; setcolor(WHITE); line(0,midy,2*midx,midy); line(midx,0,midx,2*midy); setcolor(GREEN); for(int i=0;i<edge;i++) <br=""> { if(edge-i!=1) line(result[j][0]+midx,midy-result[j][1],result[j+1][0]+midx,midy-result[j+1][1]); else line(result[j][0]+midx,midy-result[j][1],result[0][0]+midx,midy-result[0][1]); j=j+1; } getch(); } void display2() { int j=0; midx=getmaxx()/2; midy=getmaxy()/2; setcolor(WHITE); line(0,midy,2*midx,midy); line(midx,0,midx,2*midy); setcolor(BLUE); for(int i=0;i<edge;i++) <br=""> { if(edge-i!=1) line(result1[j][0]+midx,midy-result1[j][1],result1[j+1][0]+midx,midy-result1[j+1][1]); else line(result1[j][0]+midx,midy-result1[j][1],result1[0][0]+midx,midy-result1[0][1]); j=j+1; } getch(); } void refx() { scale[0][0]=1; scale[0][1]=scale[1][0]=0; scale[1][1]=-1; scale[0][2]=scale[1][2]=scale[2][0]=scale[2][1]=0; scale[2][2]=1; multiply(input,scale,result); display(); display1(); } void refy() { scale[0][0]=-1; scale[0][1]=scale[1][0]=0; scale[1][1]=1; scale[0][2]=scale[1][2]=scale[2][0]=scale[2][1]=0; scale[2][2]=1; multiply(input,scale,result); display(); display1(); } void refo() { scale[0][0]=-1; scale[0][1]=scale[1][0]=0; scale[1][1]=-1; scale[0][2]=scale[1][2]=scale[2][0]=scale[2][1]=0; scale[2][2]=1; multiply(input,scale,result); display(); display1(); } void refxy() { scale[0][1]=1; scale[0][0]=scale[1][1]=0; scale[1][0]=1; scale[0][2]=scale[1][2]=scale[2][0]=scale[2][1]=0; scale[2][2]=1; multiply(input,scale,result); display(); display1(); } void refxy1() { scale[0][1]=-1; scale[0][0]=scale[1][1]=0; scale[1][0]=1; scale[0][2]=scale[1][2]=scale[2][0]=scale[2][1]=0; scale[2][2]=1; multiply(input,scale,result); display(); display1(); } void shear() { cout<<"SHEAR X FACTOR"; cin>>sx; cout<<"SHEAR Y FACTOR"; cin>>sy; scale[0][1]=sy; scale[1][0]=sx; scale[0][0]=scale[1][1]=1; scale[0][2]=scale[1][2]=scale[2][0]=scale[2][1]=0; scale[2][2]=1; multiply(input,scale,result); display(); display1(); } void scal() { cout<<"SCALE X FACTOR"; cin>>sx; cout<<"SCALE Y FACTOR"; cin>>sy; scale[0][1]=scale[1][0]=0; scale[0][0]=sx; scale[1][1]=sy; scale[0][2]=scale[1][2]=scale[2][0]=scale[2][1]=0; scale[2][2]=1; multiply(input,scale,result); display(); display1(); } void trans() { cout<<" X FACTOR"; cin>>sx; cout<<" Y FACTOR"; cin>>sy; scale[0][0]=scale[1][1]=scale[2][2]=1; scale[0][1]=scale[0][2]=scale[1][0]=scale[1][2]=0; scale[2][0]=sx; scale[2][1]=sy; multiply(input,scale,result); display(); display1(); } void rot() { cout<<"\nENTER THE ANGLE"; cin>>ang; cout<<"\nENTER DIRECTION\n1. ANTICLOCKWISE\n2.CLOCKWISE"; cin>>clk; scale1[0][0]=scale1[1][1]=cos((ang*3.14)/180); scale1[0][2]=scale1[1][2]=scale1[2][0]=scale1[2][1]=0; scale1[2][2]=1; if(clk==1) { scale1[0][1]=sin((ang*3.14)/180); scale1[1][0]=-sin((ang*3.14)/180); } else { scale1[0][1]=-sin((ang*3.14)/180); scale1[1][0]=sin((ang*3.14)/180); } multiply1(input,scale1,result1); display(); display2(); } void rot1() { int x,y; cout<<"\nENTER THE ANGLE"; cin>>ang; cout<<"\nenter the x and y coordinates about which to rotate"; cin>>x; cin>>y; cout<<"\nENTER DIRECtION\nENTER 1 FOR ANTICLOCKWISE"; cin>>clk; scale1[0][0]=scale1[1][1]=cos((ang*3.14)/180); scale1[0][2]=scale1[1][2]=0; scale1[2][0]=((-x)*cos((ang*3.14)/180)+y*sin((ang*3.14)/180)+y); scale1[2][1]=((-x)*sin((ang*3.14)/180)-y*cos((ang*3.14)/180)+y); scale1[2][2]=1; if(clk==1) { scale1[0][1]=sin((ang*3.14)/180); scale1[1][0]=-sin((ang*3.14)/180); } else { scale1[0][1]=-sin((ang*3.14)/180); scale1[1][0]=sin((ang*3.14)/180); } multiply1(input,scale1,result1); display(); display2(); } void multiply(int input[][3],int b[][3],int result[][3]) { int i=0; for(i=0;i<edge;i++) <br=""> { result[i][0]=input[i][0]*b[0][0]+input[i][1]*b[1][0]+input[i][2]*b[2][0]; result[i][1]=input[i][0]*b[0][1]+input[i][1]*b[1][1]+input[i][2]*b[2][1]; result[i][2]=input[i][0]*b[0][2]+input[i][1]*b[1][2]+input[i][2]*b[2][2]; } } void multiply1(int input[][3],float b1[][3],float result1[][3]) { int i=0; for(i=0;i<edge;i++) <br=""> { result1[i][0]=input[i][0]*b1[0][0]+input[i][1]*b1[1][0]+input[i][2]*b1[2][0]; result1[i][1]=input[i][0]*b1[0][1]+input[i][1]*b1[1][1]+input[i][2]*b1[2][1]; result1[i][2]=input[i][0]*b1[0][2]+input[i][1]*b1[1][2]+input[i][2]*b1[2][2]; } } }; void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); int ch; transformation t; do { clrscr(); cout<<"\n main menu"; cout<<"\n1.input"; cout<<"\n2.display"; cout<<"\n3.scaling"; cout<<"\n4.translation"; cout<<"\n5.rotation"; cout<<"\n6.reflection"; cout<<"\n7. shear"; cout<<"\n8. exit"; cout<<"\n enter ur choice"; cin>>ch; switch(ch) { case 1: cout<<"\ninput"; t.input1(); break; case 2: cout<<"\ndisplay"; t.display(); break; case 3: cout<<"\nscaling"; t.scal(); break; case 4: cout<<"\ntranslation"; t.trans(); break; case 5: int ch1; do { clrscr(); cout<<"\nrotation\n1.about origin\n2.arbitary point\n3.return"; cin>>ch1; switch(ch1) { case 1: cout<<"\nabout origin"; t.rot(); break; case 2: cout<<"\narbitary point"; t.rot1(); break; case 3: break; } }while(ch1!=3); break; case 6: do { clrscr(); cout<<"\n menu"; cout<<"\n1. reflection about x axix"; cout<<"\n2. reflection about y axix"; cout<<"\n3.reflection about origin"; cout<<"\n4.refletion about y=x line"; cout<<"\n5.reflection about y=-x line"; cout<<"\n6.exit"; cout<<"\n enter ur choice"; cin>>ch1; switch(ch1) { case 1: cout<<"\nreflection about x axix"; t.refx(); break; case 2: cout<<"\nreflection about y axix"; t.refy(); break; case 3: cout<<"\nreflection about origin"; t.refo(); break; case 4: cout<<"\nrefletion about y=x line"; t.refxy(); break; case 5: cout<<"\nreflection about y=-x line"; t.refxy1(); break; case 6: break; } }while(ch1!=6); break; case 7: cout<<"\nshear"; t.shear(); break; case 8: break; } }while(ch!=8); }
Leave a Reply