16,001,979 members
1.00/5 (3 votes)
See more:
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.

C++
```//2D Transformations (POLYGON)
#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.
Posted
Updated 16-Aug-12 2:07am
v2
Comments
Keith Barrow 16-Aug-12 8:15am
Hi,
Please update you question with either:
1. The compile time error message you are getting
2. The runtime error message you are getting
3. The information you are putting in, your expected results and your actual results.

People aren't likely to help unless you give this information as we don't know what the error is.
enhzflep 16-Aug-12 8:20am
Your setid(matrix m) function is essentially useless - it does not set the passed matrix to be a identity matrix - it passes the matrix by value - i.e it creates a copy of it, that _copy_ is set to be an identity matrix.

Look-up passing by reference, you'll either make the input var a pointer to a matrix or you'll make the var a matrix reference.

As it stands, try setting a matrix manually to contain junk data - print the contents of that matrix, pass it to setid, then print it again. See what I mean?

// this function will work in that way though.
void zeroMe(int &tgt)
{
tgt = 0;
}
Kenneth Haugland 16-Aug-12 8:41am
Did you just repost the question, becouse I cant see that much difference except this said Polygon while the other said Triangle

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 75 merano99 40 Yvan Rodrigues 20 Dave Kreskowiak 15 brownpeteg 10
 OriginalGriff 631 Richard Deeming 320 Dave Kreskowiak 286 Pete O'Hanlon 270 CPallini 120

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900