Translation of a Line

Translation of line comes under the category of geometric transformations.In geometric transformations,the object is transformed relative to a stationary coordinate system or background.In 2D translation,the object is displaced along x and y direction and magnitude of displacement is specified by translation parameters tx and ty.

In order to perform a sequence of transformations(translation,scaling,rotation or reflection) as a sequential process,we need to represent all the transformation equations in matrix multiplication.For this, we use Homogeneous Coordinate system which uses 3 coordinates to represent every point.

For a given point P(x,y), P can be represented in Homogeneous Coordinate system as (x,y,1).To perform all the transformations using matrix multiplication,we need to convert 2*2 transformation matrix to 3*3 transformation matrix.A sequence of transformations can be performed by first multiplying all the transformation matrices and then multiplying 3*1 point matrix with the result of multiplication.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*Header file for built-in graphics functions such as plot() and initgraph()*/
#define pi 3.1412

/*Function which mutiplies two matrices a and b stores the result in matrix c*/
void multiply(int a[3][3],int b[3][1],int c[3][1])
    int i,j,k;

int main()
    int x1, y1,x2,y2, tx, ty,i,j;
    int P1[3][1],P2[3][1],T[3][3],ans1[3][1],ans2[3][1];
    int gm,gd=DETECT;
    initgraph(&gd,&gm,"c:\\tc\\bgi");/*loads the graphic driver and puts system into graphics mode*/

    syntax for initgraph() is:
    void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
    *graphdriver is an integer that specifies the graphics driver to be used.DETECT requests auto-detect for the driver.
    *graphmode is an integer that specifies the initial graphics mode.If *graphdriver equals DETECT,*graphmode is set by initgraph to the highest resolution available for the detected driver.
    printf ("\n\t\t TRANSLATIONn\n Enter the starting point of line ");
    scanf ("%d %d", &x1, &y1);
    printf("\nEnter end point of line");
    scanf("%d %d",&x2,&y2);
    printf("\n Enter the translation distances: ");
    scanf("%d %d", &tx, &ty);

    /*Homogeneous coordinate matrix representation for starting point*/
    P1[0][0] = x1;
    P1[1][0] = y1;
    P1[2][0] = 1;

    /*Homogeneous coordinate matrix representation for end point*/
    P2[0][0] = x2;
    P2[1][0] = y2;
    P2[2][0] = 1;

    /* 3*3 transformation matrix for performing 2D translation*/
    for( i=0;i<3;i++)
        for( j=0;j<3;j++)
            if(i==0 && j==2)
            else if(i==1 && j==2)
            else if((i==2 && j==2)||(i==1 && j==1)||(i==0 && j==0))

    /*Multiply point matrix with transformation matrix for both the points such that the ans1 and ans2 store homogeneuos coordinate representation for translated end points*/ 

    printf("\n The final end points are :(%d,%d) and (%d,%d)\n",ans1[0][0],ans1[1][0],ans2[0][0],ans2[1][0]);
    /*closegraph function closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the original mode*/
    return 0;


You can run this code in Turbo C++. Dev C++ and Microsoft visual studio do not support graphics libraries.
You can download Turbo C++ using this link Turbo C++

This content has been helpful to you?

Thanks for contributing!

Yes No