2D Transformations: Translations

On July 31, 2012, in Algorithms, Drawing, Shapes, by , Views : 25772


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.

Translation: Get the Inputs

Get the Inputs

Translation original image

Original image entered

Final translated Image

Final translated Image

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. 🙂



Tagged with:  

5 Responses to 2D Transformations: Translations

  1. Hema Kumar says:

    coding was so interest to run

  2. Rinku says:

    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.

  3. Rinku says:

    thanks for the code

Leave a Reply

Your email address will not be published. Required fields are marked *


*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

  • Dipin Krishna

Archives

All entries, chronologically...

Read more:
line
DDA Line Drawing Algorithm in C

chess
Chess Board using DDA line drawing algorithm

CIRCLE
Draw a Circle using Bresenham’s Circle Algorithm

MinMaxClose