2D Rotation Program (C Language)
2D rotation is a transformation in computer graphics where an object is rotated by an angle θ about
the origin.
Rotation Formula:
x' = x cosθ - y sinθ
y' = x sinθ + y cosθ
Algorithm:
1. Start
2. Input number of points (n)
3. Input coordinates (x, y) for each point
4. Input rotation angle θ (in degrees)
5. Convert θ into radians
6. For each point:
a. Compute x' = x cosθ - y sinθ
b. Compute y' = x sinθ + y cosθ
7. Display rotated coordinates
8. Stop
Diagram (Rotation about Origin):
(x',y') (x,y)
#include <stdio.h>
#include <math.h>
int main() {
int n, i;
float x[10], y[10];
float angle, rad;
printf("Enter number of points: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("Enter x and y for point %d: ", i+1);
scanf("%f %f", &x[i], &y[i]);
}
printf("Enter rotation angle (in degrees): ");
scanf("%f", &angle);
rad = angle * (M_PI / 180);
printf("\nRotated Points:\n");
for(i = 0; i < n; i++) {
float x_new = x[i] * cos(rad) - y[i] * sin(rad);
float y_new = x[i] * sin(rad) + y[i] * cos(rad);
printf("Point %d -> (%.2f, %.2f)\n", i+1, x_new, y_new);
}
return 0;
}