2D Rotation in Computer Graphics:
2D rotation is a fundamental operation in computer graphics that allows us to rotate an object around a fixed point in a two-dimensional plane. To perform 2D rotation, we need to specify the angle of rotation and the point around which the object will be rotated.
The mathematical formula for 2D rotation is :
x' = x * cos(θ) - y * sin(θ)
y' = x * sin(θ) + y * cos(θ)
Here, (x, y) represents the original coordinates of a point on the object, (x', y') represents the new coordinates after rotation, and θ (theta) is the angle of rotation in radians.
To apply 2D rotation in computer graphics, we follow these steps:
1. Choose a fixed point around which the object will be rotated. This point is known as the pivot point or the center of rotation.
2. Specify the angle of rotation in degrees or radians. Positive angles represent counterclockwise rotation, while negative angles represent clockwise rotation.
3. Apply the rotation formula to each point of the object, using the pivot point as the reference.
4. Calculate the new coordinates of each point after rotation using the formulas mentioned above.
5. Redraw the object using the new coordinates.
Program to rotate a line
Let's take an example of rotating a line segment in 2D space using C++. We'll define the line segment by its endpoints and specify the angle of rotation.
C++
#include <iostream>
#include <cmath>
using namespace std;
struct Point {
int x, y;
};
void rotateLine(Point& p1, Point& p2, double angle) {
double radians = angle * M_PI / 180.0;
double cosTheta = cos(radians);
double sinTheta = sin(radians);
// Rotate point p1
int x1 = p1.x * cosTheta - p1.y * sinTheta;
int y1 = p1.x * sinTheta + p1.y * cosTheta;
// Rotate point p2
int x2 = p2.x * cosTheta - p2.y * sinTheta;
int y2 = p2.x * sinTheta + p2.y * cosTheta;
// Update the coordinates of the endpoints
p1.x = x1;
p1.y = y1;
p2.x = x2;
p2.y = y2;
}
int main() {
Point p1 = {0, 0};
Point p2 = {5, 5};
double angle = 45.0;
cout << "Before rotation:" << endl;
cout << "P1: (" << p1.x << ", " << p1.y << ")" << endl;
cout << "P2: (" << p2.x << ", " << p2.y << ")" << endl;
rotateLine(p1, p2, angle);
cout << "After rotation:" << endl;
cout << "P1: (" << p1.x << ", " << p1.y << ")" << endl;
cout << "P2: (" << p2.x << ", " << p2.y << ")" << endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output
Before rotation:
P1: (0, 0)
P2: (5, 5)
After rotation:
P1: (0, 0)
P2: (0, 7)
In this code:
1. We define a `Point` struct to represent a point in 2D space with x and y coordinates.
2. The `rotateLine` function takes two `Point` references (`p1` and `p2`) representing the endpoints of the line segment and the rotation angle in degrees.
3. Inside the `rotateLine` function, we convert the angle from degrees to radians.
4. We apply the rotation formula to each endpoint of the line segment using the rotation angle.
5. The new coordinates of the endpoints are calculated and updated.
6. In the `main` function, we create two points representing the endpoints of the line segment and specify the rotation angle.
7. We print the coordinates of the endpoints before and after the rotation.
When you run this code, it will give you the coordinates of the line segment before and after the rotation, that shows how the line segment is rotated by the specified angle.
Program to rotate a Triangle:
Now, let’s discuss the concept of rotation to a triangle in 2D space. We'll explain the triangle by its three vertices and apply rotation to each vertex.
C++
#include <iostream>
#include <cmath>
using namespace std;
struct Point {
int x, y;
};
void rotatePoint(Point& p, double angle) {
double radians = angle * M_PI / 180.0;
double cosTheta = cos(radians);
double sinTheta = sin(radians);
int x = p.x * cosTheta - p.y * sinTheta;
int y = p.x * sinTheta + p.y * cosTheta;
p.x = x;
p.y = y;
}
void rotateTriangle(Point& p1, Point& p2, Point& p3, double angle) {
rotatePoint(p1, angle);
rotatePoint(p2, angle);
rotatePoint(p3, angle);
}
int main() {
Point p1 = {0, 0};
Point p2 = {4, 0};
Point p3 = {2, 3};
double angle = 60.0;
cout << "Before rotation:" << endl;
cout << "P1: (" << p1.x << ", " << p1.y << ")" << endl;
cout << "P2: (" << p2.x << ", " << p2.y << ")" << endl;
cout << "P3: (" << p3.x << ", " << p3.y << ")" << endl;
rotateTriangle(p1, p2, p3, angle);
cout << "After rotation:" << endl;
cout << "P1: (" << p1.x << ", " << p1.y << ")" << endl;
cout << "P2: (" << p2.x << ", " << p2.y << ")" << endl;
cout << "P3: (" << p3.x << ", " << p3.y << ")" << endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output
Before rotation:
P1: (0, 0)
P2: (4, 0)
P3: (2, 3)
After rotation:
P1: (0, 0)
P2: (2, 3)
P3: (-1, 3)
In this code:
1. We use the same `Point` struct to represent a point in 2D space.
2. The `rotatePoint` function takes a `Point` reference (`p`) and the rotation angle in degrees. It applies the rotation formula to the point and updates its coordinates.
3. The `rotateTriangle` function takes three `Point` references (`p1`, `p2`, `p3`) representing the vertices of the triangle and the rotation angle in degrees. It calls the `rotatePoint` function for each vertex to rotate them individually.
4. In the `main` function, we define three points representing the vertices of the triangle and specify the rotation angle.
5. We print the coordinates of the vertices before and after the rotation.
When you run this code, it will give you the coordinates of the triangle's vertices before and after the rotation, that shows how the triangle is rotated by the specified angle.
Frequently Asked Questions
Can rotation be applied to any shape in computer graphics?
Yes, rotation can be applied to any shape or object in computer graphics by rotating each point or vertex of the shape using the rotation formulas.
How do you specify the angle of rotation?
The angle of rotation is usually specified in degrees. However, in the code, we convert the angle from degrees to radians using the formula: radians = degrees * (π / 180).
Can you perform rotation around any point in 2D space?
Yes, you can perform rotation around any point in 2D space by translating the object so that the desired point of rotation becomes the origin, applying the rotation, and then translating the object back to its original position.
Conclusion
In this article, we have learned about rotation in computer graphics, focusing on 2D rotation. We discussed the concept of rotating objects around a fixed point with the help of mathematical formulas. We also looked at code examples that showed how to rotate a line segment and a triangle in 2D space.
You can also check out our other blogs on Code360.