2 D Translation, Rotation and Scaling Apr 10, 2006 • Rohit Graphics #include <graphics.h> #include <math.h> #include <conio.h> #include <iostream.h> const int NO=3; int col=1; float a[3][3]={1,0,0,0,1,0,0,0,1}; int pts[NO][3]; void changepts(int p[NO][3]); void mult(float b[3][3]) { float c[3][3]; for (int i=0;i<3;i++) for (int j=0;j<3;j++) { c[i][j]=0; for (int k=0;k<3;k++) c[i][j]+=b[i][k]*a[k][j]; } for (i=0;i<3;i++) for(int j=0;j<3;j++) a[i][j]=c[i][j]; } void trans(int x, int y) { float b[3][3]={1,0,x,0,1,y,0,0,1}; mult(b); changepts(pts); } void scale(float sx, float sy) { float b[3][3]={sx,0,0,0,sy,0,0,1}; mult(b); changepts(pts); } void rot(float angle) { angle*=M_PI/180; float b[3][3]={cos(angle),-sin(angle),0,sin(angle),cos(angle),0,0,0,1}; mult(b); changepts(pts); } void changepts(int p[NO][3]) { setcolor(col++); for (int i=0;i<NO;i++) { int q[3]; q[0]=(p[i][0]*a[0][0]+p[i][1]*a[0][1]+a[0][2]); q[1]=(p[i][0]*a[1][0]+p[i][1]*a[1][1]+a[1][2]); q[2]=1; for (int j=0;j<3;j++) p[i][j]=q[j]; } a[0][0]=1; a[0][1]=0; a[0][2]=0; a[1][0]=0; a[1][1]=1; a[1][2]=0; a[2][0]=0; a[2][1]=0; a[2][2]=1; } void dpoly(int p[NO][3]) { for (int i=0;i<NO;i++) line(p[i][0]+getmaxx()/2, getmaxy()/2-p[i][1], p[(i+1)%NO][0]+getmaxx()/2,getmaxy()/2-p[(i+1)%NO][1]); } void main() { int gdriver = DETECT, gmode = VGAHI; initgraph(&gdriver, &gmode, ""); for (int i=0;i<NO;i++) { cout<<"Enter data for point "<<i+1; cin>>pts[i][0]>>pts[i][1]; pts[i][2]=1; } cleardevice(); dpoly(pts); int cho; do { cout<<"Enter 1 for translation\n\t2 for scaling\n\t3 for rotation "; cho=getche(); cout<<endl; switch(cho) { case '1': { int x1,y1; cout<<"Enter the value of x "; cin>>x1; cout<<"Enter the value of y "; cin>>y1; trans(x1,y1); dpoly(pts); break; } case '2': { float sx,sy; int x1,y1; cout<<"Enter the value of x "; cin>>x1; cout<<"Enter the value of y "; cin>>y1; trans(-x1,-y1); cout<<"Enter the value of x scale"; cin>>sx; cout<<<span class='cpp06'>"Enter the value of y scale"</span>; cin>>sy; scale(sx,sy); trans(x1,y1); dpoly(pts); break; } case '3': { int x1,y1; cout<<"Enter the value of x "; cin>>x1; cout<<"Enter the value of y "; cin>>y1; trans(-x1,-y1); float angle; cout<<"Enter the angle in degrees "; cin>>angle; rot(angle); trans(x1,y1); dpoly(pts); break; } } }while (cho!='0'); getch(); } Add reply Click here to reply to the blog post instead Send