Skip to content

Commit

Permalink
Merge pull request #1464 from Arushi2S/Arushi2S-patch-3
Browse files Browse the repository at this point in the history
Geometric Transformation Algorithms
  • Loading branch information
pankaj-bind authored Oct 30, 2024
2 parents a71ed0c + c223dc0 commit c330792
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# README for 3D Rotation (User-Specified Axis)

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.

## Process

### User Input:
1. The user is prompted to enter the coordinates of the point in 3D: (x, y, z).
2. The user chooses the axis of rotation: X, Y, or Z.
3. The user specifies the rotation angle in degrees.

### Rotation Calculation:
The program rotates the input point about the chosen axis using trigonometric transformations:

#### X-axis Rotation:
- newY = y * cos(angle) - z * sin(angle)
- newZ = y * sin(angle) + z * cos(angle)

#### Y-axis Rotation:
- newX = x * cos(angle) + z * sin(angle)
- newZ = -x * sin(angle) + z * cos(angle)

#### Z-axis Rotation:
- newX = x * cos(angle) - y * sin(angle)
- newY = x * sin(angle) + y * cos(angle)

### Output:
The program prints the new coordinates of the point after rotation.

## Example Run

### Input:
Enter the coordinates of the point (x y z): 1 2 3
Enter the axis of rotation (X, Y, or Z): Z
Enter the rotation angle (in degrees): 90


### Output:
After rotation about Z-axis: (1, 2, 3) -> (-2, 1, 3)

# Complexity Analysis
### Time Complexity:𝑂(1)
O(1) since the program performs a constant number of operations regardless of input size.

### Space Complexity:𝑂(1)
O(1) as no extra memory is allocated beyond the basic variables for coordinates and calculations.

# Assumptions
The program assumes the angle is entered in degrees and converts it to radians internally.
It only rotates about one axis at a time.
The axis input is case-insensitive (X, Y, or Z)." format this file
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <math.h>
#include <ctype.h>

void rotate(int *x, int *y, int *z, float angle, char axis) {
float rad = angle * (M_PI / 180); // Convert angle to radians
int newX = *x, newY = *y, newZ = *z;

if (axis == 'X') { // Rotate about the X-axis
newY = round(*y * cos(rad) - *z * sin(rad));
newZ = round(*y * sin(rad) + *z * cos(rad));
} else if (axis == 'Y') { // Rotate about the Y-axis
newX = round(*x * cos(rad) + *z * sin(rad));
newZ = round(-*x * sin(rad) + *z * cos(rad));
} else if (axis == 'Z') { // Rotate about the Z-axis
newX = round(*x * cos(rad) - *y * sin(rad));
newY = round(*x * sin(rad) + *y * cos(rad));
}

printf("\nAfter rotation about %c-axis:\n", axis);
printf("(%d, %d, %d) -> (%d, %d, %d)\n", *x, *y, *z, newX, newY, newZ);
}

int main() {
int x, y, z;
float angle;
char axis;

printf("Enter the coordinates of the point (x y z): ");
scanf("%d %d %d", &x, &y, &z);

printf("Enter the axis of rotation (X, Y, or Z): ");
scanf(" %c", &axis); // Space before %c to avoid newline issue
axis = toupper(axis); // Ensure consistency with uppercase

printf("Enter the rotation angle (in degrees): ");
scanf("%f", &angle);

rotate(&x, &y, &z, angle, axis);
return 0;
}
42 changes: 42 additions & 0 deletions Geometric Transformation Algorithm/Shear Transformation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# README for 2D Shear Transformation

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.

## Process

### User Input:
1. The user inputs the number of points.
2. They enter the coordinates for each point as **(x, y)**.
3. They specify the shear factors: **shearX** for the X-axis and **shearY** for the Y-axis.

### Shear Calculation:
The coordinates are transformed using the following equations:
newX = x[i] + shearX * y[i]
newY = y[i] + shearY * x[i]
Each point is updated independently based on the given shear factors.

### Output:
The program prints the new coordinates after applying the shear transformation.

## Example Run

**Input:**
Enter the number of points: 2
Enter the coordinates of the points (x y): 1 2 3 4
Enter the shear factors (shearX shearY): 1.0 0.5

**Output:**
Sheared Coordinates: (1, 2) -> (3, 2) (3, 4) -> (5, 5)

## Complexity Analysis

### Time Complexity:
- **O(n)**, where **n** is the number of points. Each point is processed once.

### Space Complexity:
- **O(1)**, as the program only uses input arrays and basic variables.

## Assumptions
- The shear factors are real numbers (floating-point values).
- The program does not validate whether the shear factors result in visually meaningful transformations.
- Input coordinates are assumed to be integers.
29 changes: 29 additions & 0 deletions Geometric Transformation Algorithm/Shear Transformation/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>

void shear2D(int x[], int y[], int n, float shearX, float shearY) {
printf("\nSheared Coordinates:\n");
for (int i = 0; i < n; i++) {
int newX = x[i] + shearX * y[i];
int newY = y[i] + shearY * x[i];
printf("(%d, %d) -> (%d, %d)\n", x[i], y[i], newX, newY);
}
}

int main() {
int n;
printf("Enter the number of points: ");
scanf("%d", &n);

int x[n], y[n];
printf("Enter the coordinates of the points (x y):\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &x[i], &y[i]);
}

float shearX, shearY;
printf("Enter the shear factors (shearX shearY): ");
scanf("%f %f", &shearX, &shearY);

shear2D(x, y, n, shearX, shearY);
return 0;
}

0 comments on commit c330792

Please sign in to comment.