2d Transformations 1
2d Transformations 1
2d Transformations 1
NAME:
REGD NO:
------------------------------------------------------------------------------------------------------ */
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include <stdlib.h>
#include <conio.h>
int n,x[20],y[20],i;
void translate();
void rotate();
void scaling();
void pivotscale();
void pivotrotate();
void main()
{
int gd=DETECT,gm,ch;
initgraph(&gd,&gm," ");
printf("enter no/.. of points :");
scanf("%d",&n);
printf("enter %d points\n",n);
for(i=0;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
circle(x[i],y[i],1);}
x[n]=x[0];
y[n]=y[0];
do
{
printf("\n\n\t MENU \n\t 1.translation");
printf("\n\t 2.scaling");
printf("\n\t 3.scaling with pivot");
printf("\n\t 4.rotation");
printf("\n\t 5.rotation with pivot");
2D TRANFORMATION 1
GUDLAVALLERU ENGINEERING COLLEGE CAD/CAM LAB
printf("\n\t 6.exit");
printf("\n\n\t\t enter ur choice");
scanf("%d",&ch);
cleardevice();
if(ch!=6)
for(i=0;i<n;i++)
line(x[i],y[i],x[i+1],y[i+1]);
switch(ch)
{
case 1:
translate();
break;
case 2:
scaling();
break;
case 3:
pivotscale();
break;
case 4:
rotate();
break;
case 5:
pivotrotate();
break;
case 6:
return;
}
getch();
}while(ch!=6);
}
void translate()
{
int tx,ty;
printf("enter translation vector:tx,ty");
scanf("%d%d",&tx,&ty);
cleardevice();
for(i=0;i<n;i++)
line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty);
}
void rotate()
{
int a,x1[10],y1[10];
float rad;
printf("enter angle");
scanf("%d",&a);
rad=(3.14*a)/180;
2D TRANFORMATION 1
GUDLAVALLERU ENGINEERING COLLEGE CAD/CAM LAB
for(i=0;i<n;i++)
{
x1[i]=((x[i]*cos(rad))-(y[i]*sin(rad)));
y1[i]=((x[i]*sin(rad))+(y[i]*cos(rad)));
}
x1[n]=x1[0];
y1[n]=y1[0];
for(i=0;i<n;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
}
void scaling()
{
int sx,sy;
printf("\n enter sx,sy ::");
scanf("%d%d",&sx,&sy);
for(i=0;i<n;i++)
line(x[i]*sx,y[i]*sy,x[i+1]*sx,y[i+1]*sy);
}
void pivotscale()
{
int sx,sy,xp,yp,x1[10],y1[10];
printf("enter sx,sy ::");
scanf("%d%d",&sx,&sy);
printf("enter pivot point ::");
scanf("%d%d",&xp,&yp);
for(i=0;i<n;i++)
{
x1[i]=xp+(x[i]-xp)*sx;
y1[i]=yp+(y[i]-yp)*sy;
}
x1[n]=x1[0];
y1[n]=y1[0];
for(i=0;i<n;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
}
void pivotrotate()
{
int a,x1[10],y1[10],xp,yp;
char str[18];
float rad;
printf("enter angle");
scanf("%d",&a);
printf("enter pivot point ::");
scanf("%d%d",&xp,&yp);
rad=(3.14*a)/180.0;
cleardevice();
2D TRANFORMATION 1
GUDLAVALLERU ENGINEERING COLLEGE CAD/CAM LAB
circle(xp,yp,1);
sprintf(str,"(%d,%d)",xp,yp);
outtextxy(xp+10,yp,str);
for(i=0;i<n;i++)
{
x1[i]=xp+(x[i]-xp)*cos(rad)-(y[i]-yp)*sin(rad);
y1[i]=yp+(x[i]-xp)*sin(rad)+(y[i]-yp)*cos(rad);
}
x1[n]=x1[0];
y1[n]=y1[0];
for(i=0;i<n;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
}
2D TRANFORMATION 1