Showing posts with label Scaling. Show all posts
Showing posts with label Scaling. Show all posts

3D Transformations - Line - Graphics & Multimedia Lab - C++ Program

3D Transformations for a  Line 
Translation, Scaling , Rotation about X,Y & Z axis 
Graphics & Multimedia Lab - C++ Program

Sourcecode:
#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


2D Transformations: Translation, Scaling, Rotation - C++ Implementation - Graphics & Multimedia Lab

2D Transformations: Translation, Scaling, Rotation

  C++ program - Graphics & Multimedia Lab


Sourcecode:
#include<graphics.h>

#include<iostream>

#include<math.h>

#include<stdio.h>
using namespace std;

void translate();
void scale();
void rotate();

int main()
{
int gd=DETECT,gm,ch,exitp=0;
initgraph(&gd,&gm,NULL);
int x0,x1,y0,y1;
FILE *ip;
ip=fopen("2Dip.txt","r");
fscanf(ip,"%d %d %d %d",&x0,&y0,&x1,&y1);
rectangle(x0,y0,x1,y1);
fclose(ip);

translate();
scale();
rotate();

getch();
closegraph();
return 0;
}

void translate()
{
 int x0,x1,y0,y1;
 FILE *op,*ip1,*ip2;
 ip1=fopen("2Dip.txt","r");
 fscanf(ip1,"%d %d %d %d",&x0,&y0,&x1,&y1);
 setcolor(1);
 fclose(ip1);
 int tx,ty;
 ip2=fopen("2Dtransip.txt","r");
 fscanf(ip2,"%d %d",&tx,&ty);
 fclose(ip2);
 op=fopen("2Dop.txt","a+");
 rectangle(x0+tx,y0+ty,x1+tx,y1+ty);
 outtextxy((x0+tx+20),(y0+ty+20),(char*)"Translation");
 fprintf(op,"\nTranslation:\n");
 fprintf(op,"%d %d %d %d",x0+tx,y0+ty,x1+tx,y1+ty);
 fclose(op);
}

void scale()
{
 int x0,x1,y0,y1;
 FILE *op,*ip1,*ip2;
 int sx,sy;
 setcolor(2);
 ip1=fopen("2Dip.txt","r");
 fscanf(ip1,"%d %d %d %d",&x0,&y0,&x1,&y1);
 fclose(ip1);
 ip2=fopen("scaleip.txt","r");
 fscanf(ip2,"%d %d",&sx,&sy);
 op=fopen("2Dop.txt","a+");
 fclose(ip2);
 outtextxy(x0*sx+20,y0*sy+20,(char*)"Scaling");
 rectangle(x0*sx,y0*sy,x1*sx,y1*sy);
 fprintf(op,"\nSclaing:\n");
 fprintf(op,"%d %d %d %d",x0*sx,y0*sy,x1*sx,y1*sy);
 fclose(op);
}

void rotate()
{
 setcolor(3);
 FILE *op,*ip1,*ip2;
 float theta;
 int x0,x1,x2,x3,x4;
 int y0,y1,y2,y3,y4;
 int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
 int refx,refy;
 ip1=fopen("2Dip.txt","r");
 fscanf(ip1,"%d %d %d %d",&x0,&y0,&x1,&y1);
 fclose(ip1);
 ip2=fopen("Rotip.txt","r");
 fscanf(ip2,"%f",theta);
 fclose(ip2);
 op=fopen("2Dop.txt","a+");
 theta=theta*(3.14/180);
 fprintf(op,"\nRotation:\n");

 refx=100;
 refy=100;

 ax1=refy+(x0-refx)*cos(theta)-(y1-refy)*sin(theta);
 ay1=refy+(x0-refx)*sin(theta)+(y1-refy)*cos(theta);

 ax2=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
 ay2=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);

 ax3=refy+(x1-refx)*cos(theta)-(y0-refy)*sin(theta);
 ay3=refy+(x1-refx)*sin(theta)+(y0-refy)*cos(theta);

 ax4=refy+(x0-refx)*cos(theta)-(y0-refy)*sin(theta);
 ay4=refy+(x0-refx)*sin(theta)+(y0-refy)*cos(theta);


 line(ax1,ay1,ax2,ay2);
 fprintf(op,"%d %d %d %d\n",ax1,ay1,ax2,ay2);

 line(ax2,ay2,ax3,ay3);
 fprintf(op,"%d %d %d %d\n",ax2,ay2,ax3,ay3);

 line(ax3,ay3,ax4,ay4);
 fprintf(op,"%d %d %d %d\n",ax3,ay3,ax4,ay4);

 line(ax4,ay4,ax1,ay1);
 fprintf(op,"%d %d %d %d\n",ax4,ay4,ax1,ay1);


 fclose(op);
 outtextxy(ax1+20,ay1+20,(char*)"Rotation ");

}

Output:
nn@linuxmint ~ $ g++ lab6.cpp -lgraph
nn@linuxmint ~ $ ./a.out




"2Dip.txt"
50 60 100 75 


"2Dtransip.txt"
250 250


"scaleip.txt"
3 3


"Rotip.txt"
90.0


"2Dop.txt"
Translation:
300 310 350 325
Sclaing:
150 180 300 225
Rotation:
124 49 124 99
124 99 139 99
139 99 139 49
139 49 124 49
Related Posts Plugin for WordPress, Blogger...