A translation is displacement from original place. This displacement happens to be along a straight line; where two distances involves one is along x-axis that is tx and second is along y-axis that is ty. The same is shown in the figure also we can express it with following equation as well

x = x + tx  
y = y + ty


Here (tx, ty) is translation vector or shift vector. We can express above equations as a single matrix equation by using column vectors to represent coordinate positions and the translation vector:  

P = P + T
#include<stdio.h>

#include<ctype.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>



/* declaration of translation factors as a global varaibles */

int tx=200;

int ty=0;



void initialize_graph()

{

            /* select a driver and mode that supports */

            /* for example: int gdriver=EGA; nt gmode=EGAHI; */

            int gdriver=DETECT;

            int gmode;



            initgraph(&gdriver,&gmode,"\\tc2");

/* End of initialize_graph */

}



void draw_circle(int xC,int yC,int radius)

{

            /* This function draws a circle using built-in function for circle in c */



            cleardevice();

            circle(xC,yC,radius);



/* end of draw_circl */

}



void draw_rectangle(int rL,int rR,int rT,int rB)

{

            /* This function draws a rectangle using built-in function for rectangle in c */

            cleardevice();

            rectangle(rL,rT,rR,rB);



/* end of draw_rectangle */

}



void select_object(void)

{

            /* Declaration of rectangle parameters */

            int rLeft,rTop,rButtom,rRight;



            /* Declaration of circle parameters */

            int xCenter,yCenter,radius;



            char object_code;

            printf("\n Enter your choice: \n c for Circle \n r for Rectangle\n");



            /* read the selection from user */

            object_code=getchar();



            Start:

            cleardevice();

            switch(object_code)

            {

                        case 'c':

                                    printf("xCenter= "); scanf("%d",&xCenter);

                                    printf("yCenter= "); scanf("%d",&yCenter);

                                    printf("Radius= "); scanf("%d",&radius);



                                    /* call draw_circle function */

                                    draw_circle(xCenter, yCenter, radius);

                                    break;



                        case 'r':

                                    printf("rLeft= "); scanf("%d",&rLeft);

                                    printf("rTop= "); scanf("%d",&rTop);

                                    printf("rRight= "); scanf("%d",&rRight);

                                    printf("rButtom= "); scanf("%d",&rButtom);



                                    /* call draw_rectangle function */

                                    draw_rectangle(rLeft,rTop,rRight,rButtom);

                                    break;



                        default:

                                    printf("ERROR: invalid letter");

                                    goto Start;

            } /* end of switch */

} /* end of select_object */



void translation(void)

{

            /* This function performs a translation transformation on the selected object */

            int i,j;

            int colorcode;



            for(j=0;j<=getmaxy();j++)

            {

                        for(i=getmaxx();i!=0;i--)

                        {

                                    colorcode = getpixel(i,j);

                                    putpixel(i+tx,j+ty,colorcode);

                        }

            }

} /* end of translation */



void close_graph(void)

{

            /* clean up */

            closegraph();

}



/* Start of main program */

void main(void)

{

            initialize_graph();

            select_object();

            translation();

            getch();

            close_graph();
}

0 comments