3 D Translation, Rotation and Scaling Apr 10, 2006 • Rohit Graphics #include <graphics.h> #include <conio.h> #include <math.h> #include <iostream.h> float p[8][4]={0,0,0,100, 100,0,0,1, 0,100,0,1, 100,100,0,1, 0,0,100,1, 100,0,100,1, 0,100,100,1, 100,100,100,1}; void change(float b[4][4]) { for (int i=0;i<8;i++) { float q[4]; q[0]=p[i][0]*b[0][0]+p[i][1]*b[0][1]+p[i][2]*b[0][2]+p[1][3]*b[0][3]; q[1]=p[i][0]*b[1][0]+p[i][1]*b[1][1]+p[i][2]*b[1][2]+p[1][3]*b[1][3]; q[2]=p[i][0]*b[2][0]+p[i][1]*b[2][1]+p[i][2]*b[2][2]+p[1][3]*b[2][3]; q[3]=p[i][0]*b[3][0]+p[i][1]*b[3][1]+p[i][2]*b[3][2]+p[1][3]*b[3][3]; for (int j=0;j<4;j++) p[i][j]=q[j]/q[3]; } } void scale(float x, float y, float z) { float b[4][4]={x,0,0,0,0,y,0,0,0,0,z,0,0,0,0,1}; change(b); } void rotx(float a) { float b[4][4]={1,0,0,0, 0,cos(a),-sin(a),0, 0,sin(a),cos(a),0, 0,0,0,1}; change(b); } void roty(float a) { float b[4][4]={cos(a),0,sin(a),0, 0,1,0,0, -sin(a),0,cos(a),0, 0,0,0,1}; change(b); } void rotz(float a) { float b[4][4]={cos(a),-sin(a),0,0, sin(a),cos(a),0,0, 0,0,1,0, 0,0,0,1}; change(b); } void disp() { line(p[0][0]+getmaxx()/2,getmaxy()/2-p[0][1],p[1][0]+getmaxx()/2,getmaxy()/2-p[1][1]); line(p[0][0]+getmaxx()/2,getmaxy()/2-p[0][1],p[2][0]+getmaxx()/2,getmaxy()/2-p[2][1]); line(p[3][0]+getmaxx()/2,getmaxy()/2-p[3][1],p[1][0]+getmaxx()/2,getmaxy()/2-p[1][1]); line(p[3][0]+getmaxx()/2,getmaxy()/2-p[3][1],p[2][0]+getmaxx()/2,getmaxy()/2-p[2][1]); int c=4; line(p[0+c][0]+getmaxx()/2,getmaxy()/2-p[0+c][1],p[1+c][0]+getmaxx()/2,getmaxy()/2-p[1+c][1]); line(p[0+c][0]+getmaxx()/2,getmaxy()/2-p[0+c][1],p[2+c][0]+getmaxx()/2,getmaxy()/2-p[2+c][1]); line(p[3+c][0]+getmaxx()/2,getmaxy()/2-p[3+c][1],p[1+c][0]+getmaxx()/2,getmaxy()/2-p[1+c][1]); line(p[3+c][0]+getmaxx()/2,getmaxy()/2-p[3+c][1],p[2+c][0]+getmaxx()/2,getmaxy()/2-p[2+c][1]); line(p[1][0]+getmaxx()/2,getmaxy()/2-p[1][1],p[1+c][0]+getmaxx()/2,getmaxy()/2-p[1+c][1]); line(p[2][0]+getmaxx()/2,getmaxy()/2-p[2][1],p[2+c][0]+getmaxx()/2,getmaxy()/2-p[2+c][1]); line(p[3][0]+getmaxx()/2,getmaxy()/2-p[3][1],p[3+c][0]+getmaxx()/2,getmaxy()/2-p[3+c][1]); line(p[4][0]+getmaxx()/2,getmaxy()/2-p[4][1],p[4+c][0]+getmaxx()/2,getmaxy()/2-p[4+c][1]); } void main() { int gd=DETECT, gm=VGAHI; initgraph(&gd,&gm,""); char cho='1'; disp(); while (cho!='0') { cout<<"Enter choice "; cin>>cho; float ang; switch (cho) { case '1': float x,y,z; cout<<"Enter x,y,z"; cin>>x>>y>>z; scale(x,y,z); break; case '2': cout<<"Enter x angle "; cin>>ang; ang*=M_PI/180; rotx(ang); break; case '3': cout<<"Enter y angle "; cin>>ang; ang*=M_PI/180; roty(ang); break; case '4': cout<<"Enter z angle "; cin>>ang; ang*=M_PI/180; rotz(ang); break; } cleardevice(); disp(); } getch(); } Add reply Click here to reply to the blog post instead Send