Hey guys,
I created this program for translation of an n sided polygon using C++.The following program was made to run on turbo C++ without any hurdles.This program uses BGI graphics(I know that's ancient).I have rechecked the logic but can't find
any errors.
Actually the errors are not compile time or runtime but I can't get the desired output of translated polygon.
Can somebody tell me what's wrong here?
CONCEPT:
I have used multiplication of 1 3*3 matrix,i.e, translation matrix(a in this program) and others are n matrices of 1*3 order(array of pointer to integers) and the resulting coordinates have been stored in (str_1[])[i]
I created this program for translation of an n sided polygon using C++.The following program was made to run on turbo C++ without any hurdles.This program uses BGI graphics(I know that's ancient).I have rechecked the logic but can't find
any errors.
Actually the errors are not compile time or runtime but I can't get the desired output of translated polygon.
Can somebody tell me what's wrong here?
CONCEPT:
I have used multiplication of 1 3*3 matrix,i.e, translation matrix(a in this program) and others are n matrices of 1*3 order(array of pointer to integers) and the resulting coordinates have been stored in (str_1[])[i]
[/b]
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class translation
{
int tx,ty,n,*ptr[],*ptr_1[];
public:
void mem_alloc(void);
void mem_dealloc(void);
void logic(void);
void display(void);
void initialize(void);
translation(int n1,int t_x,int t_y)
{
n=n1;
tx=t_x;
ty=t_y;
}
};
void translation::mem_alloc(void)
{
for(int i=0;i<n;i++)
{
ptr[i]=new int[3];
ptr_1[i]=new int[3];
}
}
void translation::initialize(void)
{
cout<<"Enter the initial coordinates (x,y) for"<<" "<<n<<" "<<"vertices\n";
for(int i=0;i<n;i++)
{
int j=0;
cout<<"Enter x"<<i<<":";
cin>>(ptr[i])[j];
(ptr_1[i])[j]=0;
j++;
cout<<"Enter y"<<i<<":";
cin>>(ptr[i])[j];
(ptr_1[i])[j]=0;
j++;
(ptr[i])[j]=1;
(ptr_1[i])[j]=0;
}
}
void translation::logic(void)
{
int a[3][3]={{1,0,tx},{0,1,ty},{0,0,1}};
for(int k=0;k<n;k++)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
(ptr_1[k])[i]+=a[i][j]*(ptr[k])[j];
}
}
}
void translation::display(void)
{
int j=0;
for(int i=0;i<n-1;i++)
{
line((ptr[i])[j],(ptr[i])[j+1],(ptr[i+1])[j],(ptr[i+1])[j+1]);
line((ptr_1[i])[j],(ptr_1[i])[j+1],(ptr_1[i+1])[j],(ptr_1[i+1])[j+1]);
}
}
void translation::mem_dealloc(void)
{
for(int i=0;i<n;i++)
{
delete ptr[i];
delete ptr_1[i];
}
}
int main()
{
int n_1,t_x,t_y;
clrscr();
cout<<"Enter the no of sides\n";
cin>>n_1;
cout<<"Enter the <tx>\n";
cin>>t_x;
cout<<"Enter the <ty>\n";
cin>>t_y;
translation t(n_1,t_x,t_y);
t.mem_alloc();
clrscr();
t.initialize();
t.logic();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\bgi");
cleardevice();
t.display();
t.mem_dealloc();
getch();
closegraph();
return 0;
}
[b]