2D Transformation

2D Transformation


//C Program for 2D transformation  : Translation ,Scaling , Rotation about origin and arbitrary point


#include<graphics.h>
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int gd=DETECT,gm,ch,exitp=0;
float theta,sx,sy;
int a,b[20],i,c[20],d[20],e[20],tx,ty,ax,ay;
void translate();
void scale();
void rotate();
void quad();
void arotate();

int main()
{
cout<<"enter no points of poly";
cin>>a;
cout<<" enter no of cordinates of polygon";
for (i=0;i<a*2;i++)
{
cin>>b[i];
if (i%2==0)
c[i]=b[i]+320;
else
c[i]=240-b[i];
}
c[i]=c[0];
c[i+1]=c[1];
cout<<" please enter choice 1:Translation 2:scaling 3:Rotation about origin 4:Rotation about arbitary point";
cin>>ch;

switch(ch)
{
case 1:
cout<<"enter x and y translation";
cin>>tx>>ty;
quad();
translate();
break;

case 2:
cout<<"enter x and y Scaling";
cin>>sx>>sy;
quad();
scale();
break;

case 3:
cout<<"enter theta";
cin>>theta;
quad();
rotate();
break;

case 4:
cout<<"enter theta";
cin>>theta;
cout<<"enter x and y coordinates of arbitary point";
cin>>ax>>ay;
quad();
arotate();
break;
}
getch();
closegraph();
return 0;
}

void quad()
{
initgraph(&gd,&gm,NULL);
line(0,240,640,240);
line(320,0,320,480);
drawpoly(a+1,c);
}
void translate()
{
 for (i=0;i<a*2;i++)
{
if (i%2==0)
c[i]=b[i]+320+tx;
else
c[i]=240-b[i]-ty;
}
c[2*a]=c[0];
c[2*a+1]=c[1];
drawpoly(a+1,c);
}

void scale()
{
 for (i=0;i<a*2;i++)
{
if (i%2==0)
d[i]=(b[i]*sx)+320;
else
d[i]=240-(b[i]*sy);
}
d[2*a]=d[0];
d[2*a+1]=d[1];
drawpoly(a+1,d);
 getch();
}

void rotate()
{
 setcolor(3);
 theta=theta*3.14/180;
 for(i=0;i<a*2;i++)
{
e[i]=b[i]*cos(theta)-b[i+1]*sin(theta)+320;
i++;
e[i]=240-(b[i-1]*sin(theta)+b[i]*cos(theta));
}
e[2*a]=e[0];
e[2*a+1]=e[1];
drawpoly(a+1,e);
}

void arotate()
{
 setcolor(3);
 theta=theta*3.14/180;
 for(i=0;i<a*2;i++)
{
e[i]=ax+(b[i]-ax)*cos(theta)-(b[i+1]-ay)*sin(theta)+320;
i++;
e[i]=240-(ay+((b[i-1]-ax)*sin(theta)+(b[i]-ay)*cos(theta)));
}
e[2*a]=e[0];
e[2*a+1]=e[1];
drawpoly(a+1,e);
}

Comments

Popular posts from this blog

Shaun White Skateboarding Offline Fix SKIDROW MediaFire

2015 2016 Kwara State University KWASU Post UTME Result is out