3D Transformations for a Line
Translation, Scaling , Rotation about X,Y & Z axis
Graphics & Multimedia Lab - C++ Program
Sourcecode:
input3d.txt
transinput.txt
scaleinput.txt
rotninput.txt
Output:
#include<graphics.h> #include<iostream> #include<math.h> using namespace std; int gd=DETECT,gm; int x1=0,y11=0,x2=0,y2=0,z1=0,z2=0,dx=0,dy=0,dz=0,px=0,c,y; float r,r1,rx1,ry1,rx2,ry2,rz1,rz2; FILE *fp,*op; void trans(); void scale(); void rotate_x(); void rotate_y(); void rotate_z(); int main() { initgraph(&gd,&gm,NULL); fp=fopen("input3d.txt","r"); op=fopen("output3d.txt","w"); fscanf(fp,"%d %d %d %d %d %d",&x1,&y11,&z1,&x2,&y2,&z2); y=getmaxy(); fprintf(op,"%s","\n-----3D TRANSFORMATION FOR LINE-----\n"); fprintf(op,"%s","\nEnd Points of Input Line:\n"); fprintf(op,"( %d %d %d) ( %d %d %d) \n",x1,y11,z1,x2,y2,z2); // Scanning coordinates of end points of input line form the file input3d.txt printf("\n\t\t-----3D TRANSFORMATION FOR LINE-----\nScanning coordinates of end points of input line from the file input3d.txt..."); printf("\n\nTranslation:"); trans(); printf("\n\nScaling:"); scale(); printf("\n\nRotation about X-axis:"); rotate_x(); printf("\n\nRotation about Y-axis:"); rotate_y(); printf("\n\nRotation about Z-axis:"); rotate_z(); fclose(fp); fclose(op); getch(); closegraph(); return 0; } void trans() { fp=fopen("transinput.txt","r"); fscanf(fp,"%d %d %d",&dx,&dy,&dz); printf("\nScanning translation amounts from the file transinput.txt..."); fprintf(op,"%s","\nTranslated Points:\n"); fprintf(op,"(%d %d %d) (%d %d %d)\n\n",x1+dx,y11+dy,z1+dz,x2+dx,y2+dy,z2+dz); } void scale() { printf("\nScanning scale factors from the file scaleinput.txt..."); fp=fopen("scaleinput.txt","r"); fscanf(fp,"%d%d%d",&dx,&dy,&dz); fprintf(op,"%s","Scaled Points:\n"); fprintf(op,"(%d %d %d) (%d %d %d)\n\n",x1*dx,y11*dy,z1*dz,x2*dx,y2*dy,z2*dz); } void rotate_x() { printf("\nScanning angle of rotation from the file rotninput.txt..."); fp=fopen("rotninput.txt","r"); fscanf(fp,"%f",&r); r1=r*3.14/180.; rx1=x1; ry1=y11*cos(r1)-z1*sin(r1); rz1=y11*sin(r1)+z1*cos(r1); rx2=x2; ry2=y2*cos(r1)-z2*sin(r1); rz2=y2*sin(r1)+z2*cos(r1); fprintf(op,"%s","Points after rotation about X-axis:\n"); fprintf(op,"%f %f %f %f %f %f\n\n",rx1,ry1,rz1,rx2,ry2,rz2); } void rotate_y() { printf("\nScanning angle of rotation from the file rotninput.txt..."); fp=fopen("rotninput.txt","r"); fscanf(fp,"%f",&r); r1=r*3.14/180.; rx1=z1*sin(r1)+x1*cos(r1); ry1=y11; rz1=z1*cos(r1)-x1*sin(r1); rx2=z2*sin(r1)+x2*cos(r1); ry2=y2; rz2=z2*cos(r1)-x2*sin(r1); fprintf(op,"%s","Points after rotation about Y-axis:\n"); fprintf(op,"%f %f %f %f %f %f\n\n",rx1,ry1,rz1,rx2,ry2,rz2); } void rotate_z() { printf("\nScanning angle of rotation from the file rotninput.txt..."); fp=fopen("rotninput.txt","r"); fscanf(fp,"%f",&r); r1=r*3.14/180.; rx1=x1*cos(r1)-y11*sin(r1); ry1=x1*sin(r1)+y11*cos(r1); rz1=z1; rx2=x2*cos(r1)-y2*sin(r1); ry2=x2*sin(r1)+y2*cos(r1); rz2=z2; fprintf(op,"%s","Points after rotation about Z-axis:\n"); fprintf(op,"%f %f %f %f %f %f\n\n",rx1,ry1,rz1,rx2,ry2,rz2); }Input:
input3d.txt
33 44 55 132 111 121
transinput.txt
10 20 30
scaleinput.txt
10 10 10
rotninput.txt
30.00
Output:
nn@linuxmint ~ $ g++ lab12.cpp -lgraph nn@linuxmint ~ $ ./a.out nn@linuxmint ~ $
-----3D TRANSFORMATION FOR LINE----- End Points of Input Line: ( 33 44 55) ( 132 111 121) Translated Points: (43 64 85) (142 131 151) Scaled Points: (330 440 550) (1320 1110 1210) Points after rotation about X-axis: 33.000000 10.623602 69.628578 132.000000 35.671371 160.279617 Points after rotation about Y-axis: 56.070572 44.000000 31.146282 174.805054 111.000000 38.835480 Points after rotation about Z-axis: 6.593334 54.603371 55.000000 58.858391 162.113205 121.000000