Headlines
Loading...
Computer graphics program - 3-D Transformations (Translation, Rotation and scaling)

Computer graphics program - 3-D Transformations (Translation, Rotation and scaling)

3 -D Transformations (Translation, Rotation and scaling)
Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

int xp[2],yp[2],z;
void display();
void translate();
void scaling();
void rotation();
void matrixmul(int [4][4]);

void main()
{
  int gd=DETECT,gm;
  int ch,i;
  initgraph(&gd,&gm,"c:/tcc/bgi");
  for(i=0;i<2;i++)
  {
    printf("\nEnter X-coordinate of vertex %d  : ",i+1);
    scanf("%d",&xp[i]);
    printf("\nEnter Y-coordinate of vertex %d  : ",i+1);
    scanf("%d",&yp[i]);
  }
  printf("\nEnter The Z-axis For 3d Figure : ");
  scanf("%d",&z);
  clrscr();
  cleardevice();
  display(xp,yp);
  getche();
  do
  {
    printf("***** MENU *****");
    printf("\n1.TRANSLATION.");
    printf("\t2.SCALING.");
    printf("\n3.ROTATION.");
    printf("\t4.EXIT.");
    printf("\nEnter Your Choice : ");
    scanf("%d",&ch);
    clrscr();
    cleardevice();
    display(xp,yp);
    switch(ch)
    {
     case 1 : translate();
       break;

     case 2 : scaling();
       break;

     case 3 : rotation();
       break;

     case 4 : exit(0);
    }
   }
   while(ch!=4);
   getch();
   closegraph();
  }

void translate()
{
   int p[4][4];
   int tx,ty,tz,i,j;
   for(i=0;i<4;i++)
     for(j=0;j<4;j++)
       p[i][j]=(i==j);
   printf("\nEnter The Translating Factor tx : ");
   scanf("%d",&tx);
   printf("\nEnter The Translating Factor ty : ");
   scanf("%d",&ty);
   printf("\nEnter The Translating Factor tz : ");
   scanf("%d",&tz);
   clrscr();
   cleardevice();
   display();
   p[0][3]=tx;
   p[1][3]=ty;
   p[2][3]=tz;
   matrixmul(p);
}

void scaling()
{
   int p[4][4];
   int sx,sy,sz,i,j;
   for(i=0;i<4;i++)
     for(j=0;j<4;j++)
       p[i][j]=(i==j);
   printf("\nEnter The Scaling Factor sx : ");
   scanf("%d",&sx);
   printf("\nEnter The Scaling Factor sy : ");
   scanf("%d",&sy);
   printf("\nEnter The Scaling Factor sz : ");
   scanf("%d",&sz);
   if(sx==0)
     sx=1;
   if(sy==0)
     sy=1;
   if(sz==0)
     sz=1;
   clrscr();
   cleardevice();
   p[0][0]=sx;
   p[1][1]=sy;
   p[2][2]=sz;
   p[3][3]=1;
   matrixmul(p);
}

void rotation()
{
   float res[4][1],p[4][4],t[4][1];
   int ang,i,j,k,l,rch;
   float rad;
   for(i=0;i<4;i++)
     for(j=0;j<4;j++)
       p[i][j]=(i==j);
   printf("\nEnter The Rotating Angle : ");
   scanf("%d",&ang);
   rad=ang*0.0174;
   printf("\nRotation Around Various Axis  ");
   printf("\n1.X-axis");
   printf("\n2.Y-axis");
   printf("\n3.Z-axis");
   printf("\nEnter Your Choice : ");
   scanf("%d",&rch);
   switch(rch)
   {
     case 1 : p[1][1]=cos(rad);
       p[1][2]=(-1)*sin(rad);
       p[2][1]=sin(rad);
       p[2][2]=cos(rad);
       break;

     case 2 :
       p[0][0]=cos(rad);
       p[2][0]=(-1)*sin(rad);
       p[0][2]=sin(rad);
       p[2][2]=cos(rad);
       break;

     case 3 :
       p[0][0]=cos(rad);
       p[0][1]=(-1)*sin(rad);
       p[1][0]=sin(rad);
       p[1][1]=cos(rad);
       break;

     default :
        printf("\nInvalid Choice !");
   }
   clrscr();
   cleardevice();
   for(i=0;i<2;i++)
   {
     t[0][0]=xp[i];
     t[1][0]=yp[i];
     t[2][0]=z;
     t[3][0]=1;
     for(j=0;j<4;j++)
     {
      for(k=0;k<1;k++)
      {
 res[j][k]=0;
 for(l=0;l<4;l++)
 {
   res[j][k]=res[j][k]+(p[j][l]*t[l][k]);
 }
      }
     }
     xp[i]=res[0][0];
     yp[i]=res[1][0];
     z=res[2][0];
   }
   display(xp,yp);
}

void display(int xp[2],int yp[2])
{
  int x3,y3,x4,y4;
  line(getmaxx()/2,0,getmaxx()/2,getmaxy());
  line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
  outtextxy(getmaxx()/2+5,getmaxy()/2+5,"(0,0)");
  outtextxy(getmaxx()-50,getmaxy()/2+10,"X-Axis");
  outtextxy(getmaxx()/2+10,20,"Y-Axis");
  outtextxy(10,getmaxy()/2+10,"X'-Axis");
  outtextxy(getmaxx()/2+10,getmaxy()-20,"Y'-Axis");
  rectangle(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+xp[1],getmaxy()/2-yp[1]);
  if(z>=xp[0])
  {
   x3=z+xp[0];
   y3=z+yp[0];
   x4=z+xp[1];
   y4=z+yp[1];
   rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
   line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
   line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
   line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
   line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
  }
  else
  {
   x3=xp[0]-z;
   y3=yp[0]-z;
   x4=xp[1]-z;
   y4=yp[1]-z;
   rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
   line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
   line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
   line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
   line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
  }
}

void matrixmul(int a[4][4])
{
   float res[4][1],b[4][1];
   int i,j,k,l;
   for(i=0;i<2;i++)
   {
     b[0][0]=xp[i];
     b[1][0]=yp[i];
     b[2][0]=z;
     b[3][0]=1;
     for(j=0;j<4;j++)
     {
      for(k=0;k<1;k++)
      {
 res[j][k]=0;
 for(l=0;l<4;l++)
 {
   res[j][k]=res[j][k]+(a[j][l]*b[l][k]);
 }
      }
     }
     xp[i]=res[0][0];
     yp[i]=res[1][0];
   }
   z=res[2][0];
   display(xp,yp);
}
*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***