Sunday, November 27, 2011

implementation of concept of translation in computer graphics

Program in c for 2-D Translation of line and Triangle

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
int object[3][3],translate[3][3],output[3][3],n;
void input(int n)
{for(int i=0;i<n;i++)
     {cout<<"\nenter x and y for cordinate #"<<i+1<<endl;
      cin>>object[i][0]>>object[i][1];
     }
}    
void matmulti(int a[10][3],int b[3][3],int c[10][3])
{for(int i=0;i<3;i++)
  for(int j=0;j<3;j++)
   for(int k=0;k<3;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(int a[10][3],int n)
{ for(int i=0;i<n-1;i++)
      line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
       line(a[0][0],a[0][1],a[n-1][0],a[n-1][1]);
}     
     

void main()
{int i,j,ch,gd=DETECT,gm;
translate[0][0]=1;
translate[1][1]=1;
translate[2][2]=1;
object[0][2]=1;
object[1][2]=1;
object[2][2]=1;


 cout<<"\n 1.translate a line";
 cout<<"\n 2.translate a triangle";
 cout<<"\n enter your choice : ";
 cin>>ch;
 if(ch==1)
   n=2;
 if(ch==2)
   n=3;
 input(n);
 initgraph(&gd,&gm,"");
 display(object,n);
 getch();
 closegraph();
 cout<<"\nenter the translation factors tx and ty\n";
 cin>>translate[2][0]>>translate[2][1];
 matmulti(object,translate,output);
  initgraph(&gd,&gm,"");
 display(object,n);
 setcolor(12);
 display(output,n);
 getch();

}