Linear tranformation in C graphics

Program Description

Linear transformation in C graphics.
The program demonstrates how to perform linear transformation of a given polygon object (using C/C++ graphics) to position the given object in the co-ordinate plane.

Translation is done by multiplying the given object matrix with the linear tranformation matrix,to obtain the new image at the required position.
The program prompts the user for number of vertices in the polygon and takes their vertex co-ordinates in a cyclic order. It then asks the user to for x-translation factor and y-translation factor. The user is expected to enter the appropriate positions to position/move the given object at a specific location.
The final image of the translated object along with original object is drawn.

Program
/******************************************
*  Linear tranformation in C graphics
*
*    code.cheraus.com
*
*******************************************/


#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>

//function declarations
void mul(int mat[3][3],int vertex[10][3],int n);
void trans(int vertex[10][3],int n);
void init(int vertex[10][3],int n);

int main()
{
  int i,x,y;
  int vertex[10][3],n;
  
  //clear the screen
  clrscr();
  
  //taking inputs for the polygon object
  cout<<"\nEnter the no. of vertex : ";
  cin>>n;
   for(i=0;i<n;i++)
     {
	    cout<<"Enter the points (x,y): ";
	    cin>>x>>y;
	    vertex[i][0]=x;
	    vertex[i][1]=y;
	    vertex[i][2]=1;
     }
	  
	  
	 //calling the trans() function to perform translation of the given object 
     trans(vertex,n);
	 
	 getch();
	 return 0;
}

/*
Function definition for init()

Function: init(). This function initialises the screen in graphics mode and
                   loads the appropriate graphics driver.It also draws the 
				   co-ordinate axes,legends, and the image of the given object matrix.
                  
Input: 1) Object matrix (to be positioned later)
       2) Number of vertices(rows in object matrix)  

Returns: This function does not return anything

*/

void init(int vertex[10][3],int n)
{
   //initialisation of the graphics driver
   int gd=DETECT,gm,i;
   initgraph(&gd,&gm,"C:\\turboc3\\bgi");
   
   //drawing the co-ordinate axes
   setcolor(10);                 
   line(0,240,640,240);       //drawing X axis
   line(320,0,320,480);       //drawing Y axis
   
   
   //drawing graph legends
   setcolor(3);
   line(450,20,490,20);
   setcolor(15);
   line(450,50,490,50);
   setcolor(6);
   outtextxy(500,20,"Original");
   outtextxy(500,50,"Transformed");
     
   //drawing the object
   setcolor(3);

   for(i=0;i<n-1;i++)
    {
       line(320+vertex[i][0],240-vertex[i][1],320+vertex[i+1][0],240-vertex[i+1][1]);
     }
     line(320+vertex[n-1][0],240-vertex[n-1][1],320+vertex[0][0],240-vertex[0][1]);

}



/*
Function definition for mul()

Function: mul().This function multiplies the two given matrices(object matrix and 
                linear transformation matrix) and then draws the final image.
		                    
Input: 1) Linear transformation matrix
       2) Object matrix (to be positioned)
       3) Number of vertices (number of rows in object matrix)
	   
Returns: This function does not return anything

*/

void mul(int mat[3][3],int vertex[10][3],int n)
{
  int i,j,k;    // loop variables
  
  //array for storing final image matrix
 int res[10][3];
  
  
  //multiplying the object matrix with linear transformation matrix
  for(i=0;i<n;i++)
   {
     for(j=0;j<3;j++)
      { 
	     res[i][j]=0;
         for(k=0;k<3;k++)
	      {
	         res[i][j] = res[i][j] + vertex[i%5

Output
Output Image of Getting Data From User



Final Output

Comments
comments powered by Disqus