Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Reflection of an 'N' sided polygon using BGI graphics in C++

$
0
0
Hi Guys,
This is a program on reflection of an 'N' sided polygon using BGI Graphics in C++.The reflection is bout'
y=(getmaxy()+1)/2(The center line of the screen).I have done it in following steps:
In the logic() function,
1)I have translated it(any polygon) to y=0.
2)then,reflected it about y=0.
3)At last translated it back by originally translated distance.
There is no sort of syntactical,runtime or compile time error(I think so),I have checked it many times.
The problem is that I can't get the desired output .Just compile it on Turbo C++.
AND, I have done it using multiplication of a 3*3 matrix and a 3*n matrix.
Here is the program.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class reflection
{
    int n,**ptr,**ptr_1;
    public:
    void mem_alloc(void);
    void mem_dealloc(void);
    void logic(void);
    void display(void);
    void initialize(void);
    reflection(int n1)
    {
        n=n1;
    }
};
void reflection::mem_alloc(void)
{
    ptr=new int*[n];
    ptr_1=new int*[n];
    for(int i=0;i<n;i++)
    {
        *(ptr+i)=new int[3];
        *(ptr_1+i)=new int[3];

    }
}
void reflection::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 reflection::logic(void)
{
    int k;
    int a[3][3]={{1,0,0},{0,1,-(getmaxy()+1)/2},{0,0,1}};
    int b[3][3]={{1,0,0},{0,-1,0},{0,0,1}};
    int c[3][3]={{1,0,0},{0,1,+(getmaxy()+1)/2},{0,0,1}};
    for(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];
        }
    }
    for(k=0;k<n;k++)
    {
	for(int i=0;i<3;i++)
	{
	    for(int j=0;j<3;j++)
	    (ptr_1[k])[i]+=b[i][j]*(ptr[k])[j];
	}
    }
    for(k=0;k<n;k++)
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            (ptr_1[k])[i]+=c[i][j]*(ptr[k])[j];
        }
    }

}
void reflection::display(void)
{
    int j=0;
    line(0,(getmaxy()+1)/2,getmaxx()+1,(getmaxy()+1)/2);
    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]);
    }
    line((ptr[0])[0],(ptr[0])[1],(ptr[n-1])[0],(ptr[n-1])[1]);
    line((ptr_1[0])[0],(ptr_1[0])[1],(ptr_1[n-1])[0],(ptr_1[n-1])[1]);

}
void reflection::mem_dealloc(void)
{
    for(int i=0;i<n;i++)
    {
        delete ptr[i];
        delete ptr_1[i];
    }
}
int main()
{
    int n_1;
    clrscr();
    cout<<"Enter the no of sides\n";
    cin>>n_1;
    reflection t(n_1);
    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;
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>