2D Transformations: Translations, Scaling, Rotation and Shearing

On July 31, 2012, in Algorithms, Drawing, by , Views : 43995


Transformaions are a fundamental part of computer graphics.

A point is represented by (x,y). And for each transformation i will write a function that will take in the edges, the origin(if needed) and the transform factors and will return the new transformed edges.

Here, i am demonstrating the 4 main types of transformations that one can perform in 2 dimensions:
1. Translations
2. Scaling
3. Rotation
4. Shearing

Translations

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;
	}
}

Click here to see the example usage.

Scaling

void scale( int figure[], int edges, int dx, int dy, int cx, int cy )
{
	for(int i=0; i < edges; i++)
	{
		figure[2*i] = (figure[2*i] - cx) * dx + cx;
		figure[2*i+1] = (figure[2*i+1] - cy) * dy + cy;
	}
}

Click here to see the example usage.

Rotation

void rotate( int figure[], int edges, double angle, int cx, int cy )
{
	double x, y;
	angle = -1 * (angle*3.14/180);
	double cos_a = cos(angle);
	double sin_a = sin(angle);
 
	for(int i=0; i < edges; i++)
	{
		x = figure[2*i] - cx;
		y = figure[2*i+1] - cy;
		figure[2*i] = floor( (x * cos_a) - (y * sin_a) + cx + 0.5 );
		figure[2*i+1] = floor( (x * sin_a)+(y * cos_a) + cy + 0.5 );
	}
}

Click here to see the example usage.

Shearing

Check back later.



One Response to 2D Transformations: Translations, Scaling, Rotation and Shearing

  1. vijay makwana says:

    nice tutoral…..

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:
car moving over a hump
Car moving over a Hump

ellipse
Ellipse using Mid-Point Algorithm

Animated Road
A Simple Animation of a Road

MinMaxClose