Skip to content

Commit c330792

Browse files
authored
Merge pull request #1464 from Arushi2S/Arushi2S-patch-3
Geometric Transformation Algorithms
2 parents a71ed0c + c223dc0 commit c330792

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# README for 3D Rotation (User-Specified Axis)
2+
3+
This project implements a 3D Rotation algorithm in C, where the user specifies the axis of rotation (X, Y, or Z) along with the rotation angle. It transforms a 3D point to new coordinates after rotation.
4+
5+
## Process
6+
7+
### User Input:
8+
1. The user is prompted to enter the coordinates of the point in 3D: (x, y, z).
9+
2. The user chooses the axis of rotation: X, Y, or Z.
10+
3. The user specifies the rotation angle in degrees.
11+
12+
### Rotation Calculation:
13+
The program rotates the input point about the chosen axis using trigonometric transformations:
14+
15+
#### X-axis Rotation:
16+
- newY = y * cos(angle) - z * sin(angle)
17+
- newZ = y * sin(angle) + z * cos(angle)
18+
19+
#### Y-axis Rotation:
20+
- newX = x * cos(angle) + z * sin(angle)
21+
- newZ = -x * sin(angle) + z * cos(angle)
22+
23+
#### Z-axis Rotation:
24+
- newX = x * cos(angle) - y * sin(angle)
25+
- newY = x * sin(angle) + y * cos(angle)
26+
27+
### Output:
28+
The program prints the new coordinates of the point after rotation.
29+
30+
## Example Run
31+
32+
### Input:
33+
Enter the coordinates of the point (x y z): 1 2 3
34+
Enter the axis of rotation (X, Y, or Z): Z
35+
Enter the rotation angle (in degrees): 90
36+
37+
38+
### Output:
39+
After rotation about Z-axis: (1, 2, 3) -> (-2, 1, 3)
40+
41+
# Complexity Analysis
42+
### Time Complexity:𝑂(1)
43+
O(1) since the program performs a constant number of operations regardless of input size.
44+
45+
### Space Complexity:𝑂(1)
46+
O(1) as no extra memory is allocated beyond the basic variables for coordinates and calculations.
47+
48+
# Assumptions
49+
The program assumes the angle is entered in degrees and converts it to radians internally.
50+
It only rotates about one axis at a time.
51+
The axis input is case-insensitive (X, Y, or Z)." format this file
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
#include <ctype.h>
4+
5+
void rotate(int *x, int *y, int *z, float angle, char axis) {
6+
float rad = angle * (M_PI / 180); // Convert angle to radians
7+
int newX = *x, newY = *y, newZ = *z;
8+
9+
if (axis == 'X') { // Rotate about the X-axis
10+
newY = round(*y * cos(rad) - *z * sin(rad));
11+
newZ = round(*y * sin(rad) + *z * cos(rad));
12+
} else if (axis == 'Y') { // Rotate about the Y-axis
13+
newX = round(*x * cos(rad) + *z * sin(rad));
14+
newZ = round(-*x * sin(rad) + *z * cos(rad));
15+
} else if (axis == 'Z') { // Rotate about the Z-axis
16+
newX = round(*x * cos(rad) - *y * sin(rad));
17+
newY = round(*x * sin(rad) + *y * cos(rad));
18+
}
19+
20+
printf("\nAfter rotation about %c-axis:\n", axis);
21+
printf("(%d, %d, %d) -> (%d, %d, %d)\n", *x, *y, *z, newX, newY, newZ);
22+
}
23+
24+
int main() {
25+
int x, y, z;
26+
float angle;
27+
char axis;
28+
29+
printf("Enter the coordinates of the point (x y z): ");
30+
scanf("%d %d %d", &x, &y, &z);
31+
32+
printf("Enter the axis of rotation (X, Y, or Z): ");
33+
scanf(" %c", &axis); // Space before %c to avoid newline issue
34+
axis = toupper(axis); // Ensure consistency with uppercase
35+
36+
printf("Enter the rotation angle (in degrees): ");
37+
scanf("%f", &angle);
38+
39+
rotate(&x, &y, &z, angle, axis);
40+
return 0;
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# README for 2D Shear Transformation
2+
3+
This project contains a 2D Shear Transformation program in C, which allows users to shear a set of points along both the X-axis and Y-axis based on user-specified shear factors.
4+
5+
## Process
6+
7+
### User Input:
8+
1. The user inputs the number of points.
9+
2. They enter the coordinates for each point as **(x, y)**.
10+
3. They specify the shear factors: **shearX** for the X-axis and **shearY** for the Y-axis.
11+
12+
### Shear Calculation:
13+
The coordinates are transformed using the following equations:
14+
newX = x[i] + shearX * y[i]
15+
newY = y[i] + shearY * x[i]
16+
Each point is updated independently based on the given shear factors.
17+
18+
### Output:
19+
The program prints the new coordinates after applying the shear transformation.
20+
21+
## Example Run
22+
23+
**Input:**
24+
Enter the number of points: 2
25+
Enter the coordinates of the points (x y): 1 2 3 4
26+
Enter the shear factors (shearX shearY): 1.0 0.5
27+
28+
**Output:**
29+
Sheared Coordinates: (1, 2) -> (3, 2) (3, 4) -> (5, 5)
30+
31+
## Complexity Analysis
32+
33+
### Time Complexity:
34+
- **O(n)**, where **n** is the number of points. Each point is processed once.
35+
36+
### Space Complexity:
37+
- **O(1)**, as the program only uses input arrays and basic variables.
38+
39+
## Assumptions
40+
- The shear factors are real numbers (floating-point values).
41+
- The program does not validate whether the shear factors result in visually meaningful transformations.
42+
- Input coordinates are assumed to be integers.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
3+
void shear2D(int x[], int y[], int n, float shearX, float shearY) {
4+
printf("\nSheared Coordinates:\n");
5+
for (int i = 0; i < n; i++) {
6+
int newX = x[i] + shearX * y[i];
7+
int newY = y[i] + shearY * x[i];
8+
printf("(%d, %d) -> (%d, %d)\n", x[i], y[i], newX, newY);
9+
}
10+
}
11+
12+
int main() {
13+
int n;
14+
printf("Enter the number of points: ");
15+
scanf("%d", &n);
16+
17+
int x[n], y[n];
18+
printf("Enter the coordinates of the points (x y):\n");
19+
for (int i = 0; i < n; i++) {
20+
scanf("%d %d", &x[i], &y[i]);
21+
}
22+
23+
float shearX, shearY;
24+
printf("Enter the shear factors (shearX shearY): ");
25+
scanf("%f %f", &shearX, &shearY);
26+
27+
shear2D(x, y, n, shearX, shearY);
28+
return 0;
29+
}

0 commit comments

Comments
 (0)