Monday, May 2, 2011

2D TRANSFORAMTIONS (TRANSLATION,SHEARING,ROTATION,SCALING) ---- MODIFIED CHECK IT .........

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>

void translating_line();
void translating_rectangle();
void scaling();


void rotation_line();
void rotation_rectangle();
void shearing();
int main()
{
   int choice;
   int gd=DETECT ,gm;
   initgraph(&gd,&gm," ");
   initwindow(1500,700);
   while(1)
   {
    printf("\n\n\t\t\t Enter Into Main Menu");
   printf("\n\n\t\t 1. For Translating A Line\n\t\t 2.For Translating A rectangle\n\t\t 3.For Scaling in x and y \n\t\t 4.For Rotating A Line\n\t\t 5.For Rotating a RECTangle\n\t\t6. Shearing\n\t\t  Enter Your Choice:");
   scanf("%d",&choice);
   switch(choice)
   {
      case 1:
           translating_line();
        break;
      case 2:
           translating_rectangle();
      break;
      case 3:
           scaling();
      break;
   
     
      case 4:
           rotation_line();
      break;
      case 5:
           rotation_rectangle();
      break;
      case 6:
           shearing();
      break;
      default:
              printf("\n\n ***** Invalid choice *****");
      break;
      }
    }
      getch();
      closegraph();
      return 0;
 
 
}
void translating_line()
{
  int x1,y1,x2,y2,tx,ty,xn,yn,xn1,yn1,choice1;
  printf("\n\n Enter x1:");
  scanf("%d",&x1);
  printf("\n\n Enter y1:");
  scanf("%d",&y1);
  printf("\n\n Enter x2:");
  scanf("%d",&x2);
  printf("\n\n Enter y2:");
  scanf("%d",&y2);
  outtextxy(1000,200,"ORIGINAL LINE");
  line(x1,y1,x2,y2);
 // getch();
 // cleardevice();

  printf("\n\n  Do u want to translate \n 1.for yes \n anything for no");
  scanf("%d",&choice1);
  if(choice1==1)
  {
    printf("\n\n --------------- Enter translation coordinates ---------");
    printf("\n\n Enter tx:");
    scanf("%d",&tx);
    printf("\n\n Enter ty:");
    scanf("%d",&ty);
    xn=x1+tx;
    yn=y1+ty;
    xn1=x2+tx;
    yn1=y2+ty;
   // getch();
   cleardevice();
  
     outtextxy(1000,200,"AFTER TRANSLATION LINE");
     line(xn,yn,xn1,yn1);
  }
  else
  {
    printf("\n\n ****** Your choice sir ji *****");
  }
 }
 void translating_rectangle()
 {
    int x1,y1,x2,y2,tx,ty,xn,yn,xn1,yn1,choice1;
  printf("\n\n Enter x1:");
  scanf("%d",&x1);
  printf("\n\n Enter y1:");
  scanf("%d",&y1);
  printf("\n\n Enter x2:");
  scanf("%d",&x2);
  printf("\n\n Enter y2:");
  scanf("%d",&y2);
  outtextxy(1000,200,"ORIGINAL RECTANGLE");
  rectangle(x1,y1,x2,y2);
 // getch();
 // cleardevice();
  printf("\n\n Do u want to translate \n 1.for yes \n anything for no");
  scanf("%d",&choice1);
  if(choice1==1)
  {
    printf("\n\n --------------- Enter translation coordinates ---------");
    printf("\n\n Enter tx:");
    scanf("%d",&tx);
    printf("\n\n Enter ty:");
    scanf("%d",&ty);
    xn=x1+tx;
    yn=y1+ty;
    xn1=x2+tx;
    yn1=y2+ty;
   // getch();
   cleardevice();
  
     outtextxy(1000,200,"AFTER TRANSLATION LINE");
     rectangle(xn,yn,xn1,yn1);
  }
  else
  {
    printf("\n\n ****** Your choice sir ji *****");
  }
  }
 void scaling()
 {

    int sx,sy,choice;
    int xmin,xmax,ymin,ymax,xmid,ymid;
    printf("\n\n\t\t Enter rectangle or square coordinates:");
    printf("\n\n Enter xmin:");
    scanf("%d",&xmin);
       printf("\n\n Enter ymin:");
    scanf("%d",&ymin);
       printf("\n\n Enter xmax:");
    scanf("%d",&xmax);
       printf("\n\n Enter ymax:");
    scanf("%d",&ymax);
    line(xmin,ymin,xmax,ymin);
    line(xmin,ymin,xmin,ymax);
    line(xmin,ymax,xmax,ymax);
    line(xmax,ymin,xmax,ymax);
    xmid=xmin+xmax/2;
    ymid=ymin+ymax/2;
  
    getche();
    xmin=xmin-xmid;
    xmax=xmax-xmid;
    ymin=ymin-ymid;
    ymax=ymax-ymid;
  
    line(xmin,ymin,xmax,ymin);
    line(xmin,ymin,xmin,ymax);
    line(xmin,ymax,xmax,ymax);
    line(xmax,ymin,xmax,ymax);
  
    printf("\n\n 1 for scaling in x and y");
    scanf("%d",&choice);
    if(choice==1)
    {
                printf("\n Enter sx:");
                 scanf("%d",&sx);
                  printf("\n Enter sy:");
                 scanf("%d",&sy);
     xmin=xmin*sx;
     xmax=xmax*sx;
    ymin=ymin*sy;
    ymax=ymax*sy;
     }
     outtextxy(300,200,"AFTER SCALING");
     xmin=xmin+xmid;
    xmax=xmax+xmid;
    ymin=ymin+ymid;
    ymax=ymax+ymid;
    cleardevice();
    line(xmin,ymin,xmax,ymin);
    line(xmin,ymin,xmin,ymax);
    line(xmin,ymax,xmax,ymax);
    line(xmax,ymin,xmax,ymax);
  
  }

   
void rotation_rectangle()
{
   float my;
   int x1,y1,x2,y2,x3,y3,x4,y4;
 
   int tempx1,tempx2,tempx3,tempx4,tempy1,tempy2,tempy3,tempy4,tempx,tempy;
//   int temp,temp1;
   printf("\n\n ----Enter Rectanlge Coordinates----");
   printf("\n\n Enter x1:");
   scanf("%d",&x1);
   printf("\n\n Enter y1:");
   scanf("%d",&y1);
   printf("\n\n Enter x2:");
   scanf("%d",&x2);
   printf("\n\n Enter y2:");
   scanf("%d",&y2);
   outtextxy(800,200,"RECTANGLE");
   rectangle(x1,y1,x2,y2);
   printf("\n\n Enter the Rotation Angle:");
   scanf("%f",&my);

   tempx=x1;  // i have made fixed point as x1 and y1  you can make fixed acc to your choice
   tempy=y1;
   //x3=x2;
   //y3=y2;
   //x4=x1;
   //y4=y2;
 
   my= my*(3.14/180);
   //printf("\n\n%d",my);
 
 
   tempx1=tempx+((x1-tempx)*cos(my)) - ((y1-tempy)*sin(my));
   tempy1=tempy+((x1-tempx)*sin(my)) + ((y1-tempy)*cos(my));
   tempx2=tempx+((x2-tempx)*cos(my)) - ((y1-tempy)*sin(my));
   tempy2=tempy+((x2-tempx)*sin(my)) + ((y1-tempy)*cos(my));
   tempx3=tempx+((x2-tempx)*cos(my)) - ((y2-tempy)*sin(my));
   tempy3=tempy+((x2-tempx)*sin(my)) + ((y2-tempy)*cos(my));
   tempx4=tempx+((x1-tempx)*cos(my)) - ((y2-tempy)*sin(my));
   tempy4=tempy+((x1-tempx)*sin(my)) +((y2-tempy)*cos(my));
 

   line(tempx1,tempy1,tempx2,tempy2);
  // getch();
   line(tempx2,tempy2,tempx3,tempy3);
   //getch();
   line(tempx3,tempy3,tempx4,tempy4);//getch();
  line(tempx4,tempy4,tempx1,tempy1);
 }
 void rotation_line()
 {
   float my;
   int x1,y1,x2,y2,x3,y3,x4,y4;
 
   int tempx1,tempx2,tempx3,tempx4,tempy1,tempy2,tempy3,tempy4,tempx,tempy;
//   int temp,temp1;
   printf("\n\n ----Enter line Coordinates----");
   printf("\n\n Enter x1:");
   scanf("%d",&x1);
   printf("\n\n Enter y1:");
   scanf("%d",&y1);
   printf("\n\n Enter x2:");
   scanf("%d",&x2);
   printf("\n\n Enter y2:");
   scanf("%d",&y2);
   outtextxy(800,200,"LINE");
   line(x1,y1,x2,y2);
   printf("\n\n Enter the Rotation Angle:");
   scanf("%f",&my);

   // rotating about the point x1 and y1
   tempx=x1;
   tempy=y1;
 
 
   my= my*(3.14/180);
   printf("\n\n%f",my);// my=angle
   tempx1=tempx+((x1-tempx)*cos(my)) - ((y1-tempy)*sin(my));
   tempy1=tempy+((x1-tempx)*sin(my)) + ((y1-tempy)*cos(my));
   tempx2=tempx+((x2-tempx)*cos(my)) - ((y1-tempy)*sin(my));
   tempy2=tempy+((x2-tempx)*sin(my)) + ((y1-tempy)*cos(my));
   tempx3=tempx+((x2-tempx)*cos(my)) - ((y2-tempy)*sin(my));
   tempy3=tempy+((x2-tempx)*sin(my)) + ((y2-tempy)*cos(my));
   //tempx4=tempy+((x1-tempx)*cos(my)) - ((y2-tempy)*sin(my));
   //tempy4=tempy+((x1-tempx)*sin(my)) +((y2-tempy)*cos(my));
 

   line(tempx1,tempy1,tempx2,tempy2);
  // getch();
   line(tempx2,tempy2,tempx3,tempy3);
   //getch();
   //line(tempx3,tempy3,tempx4,tempy4);//getch();
  //line(tempx4,tempy4,tempx1,tempy1);
}



void shearing()
{

    int xa,ya,xb,yb,shx,choice,shy;
 
   settextstyle(8,HORIZ_DIR,8);
   cleardevice();
   outtextxy(300,300," EVALUATION");
getche();
cleardevice();
printf("\n\n Enter xa:");
scanf("%d",&xa);
printf("\n\n Enter ya:");
scanf("%d",&ya);
printf("\n\n Enter xb:");
scanf("%d",&xb);
printf("\n\n Enter yb:");
scanf("%d",&yb);
rectangle(xa,ya,xb,yb);
printf("\n\t\t 1.For shearing in X direction\n\t\t 2.For Shearing In Y Direction\n\t\t Enter Your Choice:");
scanf("%d",&choice);
cleardevice();
if(choice==1)
{
  printf("\n\n Enter shearing in x:");
  scanf("%d",&shx);
  outtextxy(600,200,"After Shearing");
  line(xa,ya,xb,ya);
  line(xa,ya,xa+shx,yb);
  line(xb,ya,xb+shx,yb);
  line(xa+shx,yb,xb+shx,yb);
}
else if (choice==2)
{
 printf("\n\n Enter shearing in y:");
  scanf("%d",&shy);
  outtextxy(800,200,"After Shearing");
  line(xa,ya,xb,ya+shy);
  line(xa,ya,xa,yb);
  line(xb,ya+shy,xb,yb+shy);
  line(xa,yb,xb,yb+shy);
}
else
{
  printf(" **** invalid choice ****");
}
 }

No comments:

Post a Comment