TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Computer Graphics Rotation

Computer Graphics Rotation with Computer Graphics Tutorial, Line Generation Algorithm, 2D Transformation, 3D Computer Graphics, Types of Curves, Surfaces, Computer Animation, Animation Techniques, Keyframing, Fractals etc.

<< Back to COMPUTER

Rotation:

It is a process of changing the angle of the object. Rotation can be clockwise or anticlockwise. For rotation, we have to specify the angle of rotation and rotation point. Rotation point is also called a pivot point. It is print about which object is rotated.

Types of Rotation:

  1. Anticlockwise
  2. Counterclockwise

The positive value of the pivot point (rotation angle) rotates an object in a counter-clockwise (anti-clockwise) direction.

The negative value of the pivot point (rotation angle) rotates an object in a clockwise direction.

When the object is rotated, then every point of the object is rotated by the same angle.

Straight Line: Straight Line is rotated by the endpoints with the same angle and redrawing the line between new endpoints.

Polygon: Polygon is rotated by shifting every vertex using the same rotational angle.

Curved Lines: Curved Lines are rotated by repositioning of all points and drawing of the curve at new positions.

Circle: It can be obtained by center position by the specified angle.

Ellipse: Its rotation can be obtained by rotating major and minor axis of an ellipse by the desired angle.

Rotation
Rotation

Matrix for rotation is a clockwise direction.

Rotation

Matrix for rotation is an anticlockwise direction.

Rotation

Matrix for homogeneous co-ordinate rotation (clockwise)

Rotation

Matrix for homogeneous co-ordinate rotation (anticlockwise)

Rotation

Rotation about an arbitrary point: If we want to rotate an object or point about an arbitrary point, first of all, we translate the point about which we want to rotate to the origin. Then rotate point or object about the origin, and at the end, we again translate it to the original place. We get rotation about an arbitrary point.

Example: The point (x, y) is to be rotated

The (xc yc) is a point about which counterclockwise rotation is done

Step1: Translate point (xc yc) to origin

Rotation

Step2: Rotation of (x, y) about the origin

Rotation

Step3: Translation of center of rotation back to its original position

Rotation
Rotation

Example1: Prove that 2D rotations about the origin are commutative i.e. R1 R2=R2 R1.

Solution: R1 and R2are rotation matrices

Rotation

Example2: Rotate a line CD whose endpoints are (3, 4) and (12, 15) about origin through a 45° anticlockwise direction.

Solution: The point C (3, 4)

Rotation
Rotation
Rotation

Example3: Rotate line AB whose endpoints are A (2, 5) and B (6, 12) about origin through a 30° clockwise direction.

Solution: For rotation in the clockwise direction. The matrix is

Rotation

Step1: Rotation of point A (2, 5). Take angle 30°

Rotation

Step2: Rotation of point B (6, 12)

Rotation
Rotation

Program to rotate a line:

#include
#include
#include
int main()
{
	intgd=0,gm,x1,y1,x2,y2;
	double s,c, angle;
	initgraph(&gd, &gm, "C:\\TC\\BGI");
	setcolor(RED);
	printf("Enter coordinates of line: ");
	scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
	cleardevice();
	setbkcolor(WHITE);
	line(x1,y1,x2,y2);
	getch();
	setbkcolor(BLACK);
	printf("Enter rotation angle: ");
	scanf("%lf", &angle);
	setbkcolor(WHITE);
	c = cos(angle *3.14/180);
	s = sin(angle *3.14/180);
	x1 = floor(x1 * c + y1 * s);
	y1 = floor(-x1 * s + y1 * c);
	x2 = floor(x2 * c + y2 * s);
	y2 = floor(-x2 * s + y2 * c);
	cleardevice();
	line(x1, y1 ,x2, y2);
	getch();
	closegraph();
return 0;
}

Output:

Before rotation

Rotation
Rotation
Rotation

After rotation

Rotation

Program to rotate a Triangle:

#include
#include
#include
main()
{
	intgd=0,gm,x1,y1,x2,y2,x3,y3;
	double s,c, angle;
	initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
	setcolor(RED);
	printf("Enter coordinates of triangle: ");
	scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2, &x3, &y3);
	setbkcolor(WHITE);
	cleardevice();
	line(x1,y1,x2,y2);
	line(x2,y2, x3,y3);
	line(x3, y3, x1, y1);
	getch();
	setbkcolor(BLACK);
	printf("Enter rotation angle: ");
	scanf("%lf", &angle);
	setbkcolor(WHITE);
	c = cos(angle *M_PI/180);
	s = sin(angle *M_PI/180);
	x1 = floor(x1 * c + y1 * s);
	y1 = floor(-x1 * s + y1 * c);
	x2 = floor(x2 * c + y2 * s);
	y2 = floor(-x2 * s + y2 * c);
	x3 = floor(x3 * c + y3 * s);
	y3 = floor(-x3 * s + y3 * c);
	cleardevice();
	line(x1, y1 ,x2, y2);
	line(x2,y2, x3,y3);
	line(x3, y3, x1, y1);
	getch();
	closegraph();
	return 0;
}

Output:

Before rotation

Rotation
Rotation
Rotation

After rotation

Rotation
Next TopicReflection




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf