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<string.h>

#include<stdlib.h>

#include<ctype.h>

#include<graphics.h>

#include<conio.h>

#include<math.h>



/* declaration of translation factors as global varaibles */



            int tx=200;

            int ty=100;



void initialize_graph(void)

{

            /* select a driver and mode that supports */

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

            int gdriver = DETECT;

            int gmode;



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

}



void draw_circle(void)

{

      /* This function draws a circle with center point (100,100) and radius 50 */

            cleardevice();

            circle(100,100,50);

}



void draw_rectangle(void)

{

            /* This function draws a circle with left=100 top=150 right=200 buttom=200*/

            cleardevice();

            rectangle(100,150,200,200);

}



void select_object()

{

            char object_code;



            Start:

            cleardevice();



            printf("\n Enter your choice: \n");

            printf(" c for circle *** r for rectangle \n");



            object_code = getchar();

            switch (object_code)

            {

                        case 'r':

                                    draw_rectangle();

                                    break;

                        case 'c':

                                    draw_circle();

                                    break;

                        default:

                                    printf("ERROR: invalide letter\n\n");

                                    goto Start;

            }

} /* End of select_object */



void translate(void)

{

            /* This function perform the translation onto the selected object */

            int i,j;

            int colorcode=2;



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

            {

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

                        {

                                    colorcode=getpixel(i,j);

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

                        }

            }

}



void close_graph(void)

{

            /* clean up */

            closegraph();

} /* End of colse_graph */



/* Start of main program */

void main(void)

{

            initialize_graph();

            select_object();

            translate();

            getch();

            close_graph();

/* End of main */

}

 

0 comments