2D Transformations: Rotation

On August 5, 2012, in Algorithms, Drawing, Shapes, by , Views : 20882


A two-dimensional rotation is applied to an object by repositioning it along a circular path in the x-y plane. When we generate a rotation we get a rotation angle (θ) and the position about which the object is rotated (xr , yr) this is known as rotation point or pivot point. The transformation can also be described as a rotation about rotation axis that is perpendicular to x-y plane and passes through the pivot point. Positive values for the rotation angle define counter-clockwise rotations about the pivot point and the negative values rotate objects in the clockwise direction.To know more on what rotation is and its matrix representation see 2D Transformations#Rotation.

Get the Inputs

Get the Inputs

The Original Figure entered.

The Original Figure entered.

Rotated Figure

Rotated Figure



Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 );
	}
}

Example:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
 
 
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] = ceil( (x * cos_a) - (y * sin_a) + cx );
		figure[2*i+1] = ceil( (x * sin_a)+(y * cos_a) + cy );
	}
}
 
void main()
{
	int figure[20], edges;  // A Figure with Max 10 edges.
	double angle;
	int cx=0, cy=0;
	int gd = DETECT, gm;
	initgraph( &gd, &gm, "" );
	int max_y = getmaxy();
	clrscr();
	cleardevice();
 
	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 angle of rotation in degrees: ");
	scanf( "%lf", &angle);
 
	printf( "Enter the center of rotation: \n");
	printf( "cx: ");
	scanf( "%d", &cx);
	printf( "cy: ");
	scanf( "%d", &cy);
	cy = max_y - cy;
 
	cleardevice();
	setbkcolor(WHITE);
 
	setcolor(GREEN);
	setlinestyle(SOLID_LINE, 0, 3);
	drawpoly( edges, figure );
	getch();
 
	for(int i=0; i < edges; i++)
		figure[2*i+1] = max_y - figure[2*i+1];
	rotate(figure,edges,angle,cx,cy);
	for(int i=0; i < edges; i++)
		figure[2*i+1] = max_y - figure[2*i+1];
 
	setcolor(RED);
	drawpoly( edges, figure );
	getch();
}



Hope, it helps. :)



2 Responses to 2D Transformations: Rotation

  1. shambel bogale says:

    I LIKE THIS GRAPHICS PROGRAMES

  2. ATULYA PATRA says:

    COOL………………

Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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
  • My World of Codes

Archives

All entries, chronologically...

Read more:
Rotated Figure
2D Transformations: Translations, Scaling, Rotation and Shearing

car moving over a hump
Car moving over a Hump

fish
Fish Movement

MinMaxClose