In a translation transformation all the points in the object are moved in a straight line in the same direction. The size, the shape and the orientation of the image are the same as that of the original object. Same orientation means that the object and image are facing the same direction.

Function:

void translation( int figure[], int edges, int dx, int dy ) { for(int i=0; i < edges; i++) { figure[2*i] += dx; figure[2*i+1] += dy; } } |

Example usage:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include<graphics.h> #include<stdio.h> #include<conio.h> void translation( int figure[], int edges, int dx, int dy ) { for(int i=0; i < edges; i++) { figure[2*i] += dx; figure[2*i+1] += dy; } } void main() { int figure[20], edges, dx, dy; // A Figure with Max 10 edges. int gd = DETECT, gm; clrscr(); printf( "Number of edges: " ); scanf( "%d", &edges ); for(int i=0; i < edges; i++) { printf( "Enter edge (x%d,y%d) : ", i , i ); scanf( "%d %d", &figure[2*i], &figure[2*i+1] ); } figure[2*i] = figure[0]; figure[2*i+1] = figure[1]; edges += 1; printf( "Enter dx: "); scanf( "%d", &dx); printf( "Enter dy: "); scanf( "%d", &dy); initgraph( &gd, &gm, "" ); cleardevice(); drawpoly( edges, figure ); getch(); translation(figure,edges,dx,dy); setcolor(RED); drawpoly( edges, figure ); getch(); } |

Hope, it helped. 🙂

coding was so interest to run

Can u please explain me why u used array [2*i] and [2*i+1] or exactly what it means????

I m not getting it!!

The single dimension array is used to store the edges.

eg: For the edge (x1, y1), x1 is stored in array[0] and y1 in array[1].

Why array[2*i] and array[2*i+1]:

i starts with 0.

So, the 1st edge is stored in array[0] and array[1].

For the second edge, i is 1, so its stored in array[2] and array[3].

Hope it helped.

Thanks.

Thank you sir

thanks for the code