Hi All,
Can any one help me with 2D Transformations Polygon in C++.
Below is the program which I am referring to, but it's not working.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
struct wct
{
int x;
int y;
};
typedef double matrix[3][3];
matrix thm;
void setid(matrix m)
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m[i][j]=(i=j);
}
void premul(matrix a)
{
matrix tmp;
int r,c;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
tmp[r][c]=thm[r][0]*a[0][c]+thm[r][1]*a[1][c]+thm[r][2]*a[2][c];
for(r=0;r<3;r++)
for(c=0;c<3;c++)
thm[r][c]=tmp[r][c];
}
void transform(int n,wct *pt)
{
int i;
double tmp;
for(i=0;i<n;i++)>
{
tmp=thm[0][0]*pt[i].x+thm[0][1]*pt[i].y+thm[0][2];
pt[i].y=thm[1][0]*pt[i].x+thm[1][1]*pt[i].y+thm[1][2];
pt[i].x=tmp;
}
}
void translation(int tx,int ty)
{
matrix m;
setid(m);
m[0][2]=tx;
m[1][2]=ty;
premul(m);
}
void rotation(double a,wct pt)
{
matrix m;
a=(22*a)/1260;
setid(m);
m[0][0]=cos(a);
m[0][1]=sin(a);
m[0][2]=pt.x*(1-cos(a))-pt.x*sin(a);
m[1][0]=sin(a);
m[1][1]=cos(a);
m[1][2]=pt.y*(1-cos(a))-pt.x*sin(a);
premul(m);
}
void scaling(int sx,int sy,wct pt)
{
matrix m;
setid(m);
m[0][0]=sx;
m[0][1]=pt.x*(1-sx);
m[1][1]=sy;
m[1][2]=pt.y*(1-sy);
premul(m);
}
void usrpoly(wct *pt,int n)
{
int i,j=0;
int *a;
for(i=0;i<n;i++)>
{
a[j]=pt[i].x;
a[j+1]=pt[i].y;
j+=2;
}
j-=2;
a[j]=a[0];
a[j+1]=a[1];
drawpoly(n,a);
}
void main()
{
int gd,gm,i,j;
void translate(wct *,int);
void rotate(wct *,int);
void scale(wct *,int);
void translation(int,int);
wct pt[]={200,200,400,200,400,300,200,300,200,200};
int tx,ty;
int c;
do
{
clrscr();
printf("\n\t--------------------------- ");
printf("\n\t 2d transformations(Polygon)");
printf("\n\t--------------------------- ");
printf("\n\t 1.Translation ");
printf("\n\t 2.Rotation ");
printf("\n\t 3.Scaling ");
printf("\n\t 4.Exit ");
printf("\n\t Enter your choice:");
scanf("%d",&c);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
switch(c)
{
case 1: translate(pt,6);getch();break;
case 2: rotate(pt,6);getch();break;
case 3: scale(pt,6);getch();break;
case 4: printf("\n\t\t Exit from the menu"); getch();break;
default: printf("\n\t\t invalid choice........!");break;
}
getch();
closegraph();
}while(c!=4);
}
void translate(wct *pt,int n)
{
int x,y;
setid(thm);
cleardevice();
usrpoly(pt,n);
printf("\t\t Enter point to translate (x,y):");
scanf("%d%d",&x,&y);
translation(x,y);
transform(n,pt);
usrpoly(pt,n);
}
void rotate(wct *pt,int n)
{
double ang;
wct rpt;
setid(thm);
usrpoly(pt,n);
printf("\t\t Enter the angle to rotate");
scanf("%f",&ang);
printf("Enter the reference point(x,y)");
scanf("%d%d",&rpt.x,&rpt.y);
rotation(ang,rpt);
transform(n,pt);
usrpoly(pt,n);
}
void scale(wct *pt,int n)
{
double ang;
int sx,sy;
wct rpt;
setid(thm);
usrpoly(pt,n);
printf("\n\t Enter scaling factor (sx,sy)");
scanf("%d%d",&sx,&sy);
printf("Enter the reference point (x,y)");
scanf("%d%d",&rpt.x,&rpt.y);
scaling(sx,sy,rpt);
transform(n,pt);
usrpoly(pt,n);
}
Thanks,
Kumar.