Pages

Wednesday, March 23, 2011

DRAWING TRIANGLE & ITS TRANSLATION

In this program we will first draw a triangle according to the coordinates given by user and then applying the translation matrix or translation factor we will  draw that triangle with translated coordinates 


#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi ");
errorcode=graphresult();
if(errorcode!=grOk)
{
cout<<"graphics error"<<grapherrormsg(errorcode)<<endl;
cout<<"press any key to start";
getch();
exit(1);
}
int i,j,k,x1,x2,x3,y1,y2,y3,x,y;
int c[3][3],a[3][3];
cout<<"ENTER COORDINATES OF TRIANGLE";
cout<<"\n 1st coordinate";
cout<<"\n x - coordinate ";
cin>>a[0][0];
cout<<"\n y - coordinate";
cin>>a[1][0];
a[2][0]=1;
cout<<"\n 2nd coordinate";
cout<<"\n x - coordinate";
cin>>a[0][1];
cout<<"\n y - coordinate";
cin>>a[1][1];
a[2][1]=1;
cout<<"\n 3rd coordinate";
cout<<"\n x - coordinate";
cin>>a[0][2];
cout<<"\n y - coordinate";
cin>>a[1][2];
a[2][2]=1;
cout<<"\n\n\n enter the translation coordinate";
cout<<" enter x - coordinate";
cin>>x;
cout<<"enter y - coordinate";
cin>>y;
int t[3][3]={{1,0,x},{0,1,y},{0,0,1}};
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        c[i][j]=0;
        for(k=0;k<3;k++)
        {
            c[i][j]+=t[i][k]*a[k][j];
        }
    }
}
clrscr();
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
cout<<" BEFORE TRANSFORMATION";
cout<<"\n press any key to get translated triangle";
getch();
clrscr();
line(c[0][0],c[1][0],c[0][1],c[1][1]);
line(c[0][1],c[1][1],c[0][2],c[1][2]);
line(c[0][0],c[1][0],c[0][2],c[1][2]);
cout<<" AFTER TRANSLATION";
getch();
closegraph();
}

No comments:

Post a Comment