# Rotation in Computer Graphics

**Computer Graphics | Rotation**: In this tutorial, we are going to learn about the Rotation which is a type of Transformation in computer graphics, type of Transformation in brief, etc.

Submitted by Monika Sharma, on April 30, 2020

## Computer Graphics | Rotation

**Rotation** is a type of transformation that is very often used in computer graphics and image processing. Rotation is a process of rotating an object concerning an angle in a two-dimensional plane.

It is a process of changing the angle of the object which can be clockwise or anticlockwise, while we have to specify the angle of rotation and rotation point. A rotation point is also called a pivot point.

There are two types of rotations according to the direction of the movement of the object. These are:

- Anti-clockwise rotation
- Clockwise rotation

The positive value of the rotation angle rotates an object in an anti-clockwise direction while the negative value of the rotation angle rotates an object in a clockwise direction. When we rotate any object, then every point of that object is rotated by the same angle. For example, a straight line is rotated by the endpoints with the same angle and the line is re-drawn between the new endpoints. Also, the polygon is rotated by shifting every vertex with the help of the same rotational angle. Same for circle also, it can be obtained by center position by the specified angle.

Let's now consider a point object O which has to be rotated from one angle to another.

- Initial co-ordinates of the object O = (X
_{old}, Y_{old}) - Initial angle of the object O with respect to origin = Φ
- Rotation angle = θ
- New co-ordinates of the object O after rotation = (X
_{new}, Y_{new})

In order to rotate an object, we need to rotate each vertex of the figure individually. More clearly let us assume a point **P**, on rotating a point **P(x, y)** by an angle **A** about the origin we get a point **P' (x', y')**. The calculation of values of **x'** and **y'** are as follows,

We know that,

x = r cosB, y = r sinB x' = r cos ( A + B) = r ( cosA cosB – sinA sinB ) = r cosB cosA – r sinB sinA = x cosA – y sinA y' = r sin ( A + B ) = r ( sinA cosB + cosA sinB ) = r cosB sinA + r sinB cosA = x sinA + y cosA

It's representation in matrix form will be as follows,

Now let's understand this by using an example,

**Problem Statement:** Given a triangle with corner coordinates (0, 0), (1, 0) and (1, 1). Rotate the triangle by 90 degrees anticlockwise direction and find out the new coordinates.

**Solution:**

We always rotate a polygon by rotating each of its vertexes with the same rotation angle.

**Given,**

- Old corner coordinates of the triangle = A (0, 0), B (1, 0), C (1, 1)
- Rotation angle = θ = 90º

__For the Co-ordinate A (0, 0)__

Let the new coordinates of corner A after rotation = (X_{new}, Y_{new}).

When we apply the rotation equation, we get-

X_{new} = X_{old} x cosθ – Y_{old} x sinθ

= 0 x cos90º – 0 x sin90º

= 0 + 0

= 0

Y_{new} = X_{old} x sinθ + Y_{old} x cosθ

= 0 x sin90º + 0 x cos90º

= 0 + 0

= 0

Thus, the new co-ordinates of corner A we get after rotation is = (0, 0).

__For the Coordinate B (1, 0)__

Let the new coordinates of corner B after rotation = (X_{new}, Y_{new}).

X_{new} = X_{old} x cosθ – Y_{old} x sinθ

= 1 x cos90º – 0 x sin90º

= 0 – 0

= 0

Y_{new} = X_{old} x sinθ + Y_{old} x cosθ

= 1 x sin90º + 0 x cos90º

= 1 + 0

= 1

Thus, the new co-ordinates of corner B that we get after rotation are = (0, 1).

__For the Coordinate C (1, 1)__

Let the new coordinates of corner C after rotation = (X_{new}, Y_{new}).

X_{new} = X_{old} x cosθ – Y_{old} x sinθ

= 1 x cos90º – 1 x sin90º

= 0 – 1

= -1

Y_{new} = X_{old} x sinθ + Y_{old} x cosθ

= 1 x sin90º + 1 x cos90º

= 1 + 0

= 1

Thus, the new co-ordinates of corner C we get after rotation is = (-1, 1).

Thus, the new co-ordinates of the triangle after rotation are = A (0, 0), B (0, 1), C (-1, 1).

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.