Write a c program to demonstrate 3D Object Rotation.

Here below is the C program that demonstrates 3D Object Rotation around the X-axis, Y-axis, and Z-axis. The program uses homogeneous coordinates and matrix multiplication for the rotation transformation.

C Program for 3D Object Rotation

The program will take a 3D object (represented by a set of vertices) and rotate it around one of the three axes based on user input. The object is displayed using the graphics.h library in 2D after applying the 3D rotations.

	#include <stdio.h>
    #include <graphics.h>
    #include <math.h>
    
    #define MAX_VERTICES 10

    // Structure to represent a point in homogeneous coordinates (x, y, z, 1)
    typedef struct {
        float x, y, z, w;
    } Point;
    
    // Function to multiply a point by a transformation matrix
    Point multiplyMatrix(Point p, float matrix[4][4]) {
        Point result;
        result.x = matrix[0][0] * p.x + matrix[0][1] * p.y + matrix[0][2] * p.z + matrix[0][3] * p.w;
        result.y = matrix[1][0] * p.x + matrix[1][1] * p.y + matrix[1][2] * p.z + matrix[1][3] * p.w;
        result.z = matrix[2][0] * p.x + matrix[2][1] * p.y + matrix[2][2] * p.z + matrix[2][3] * p.w;
        result.w = matrix[3][0] * p.x + matrix[3][1] * p.y + matrix[3][2] * p.z + matrix[3][3] * p.w;
        return result;
    }
    
    // Function to rotate a point around the X-axis by an angle
    void rotateX(float angle, float matrix[4][4]) {
        float rad = angle * M_PI / 180.0;  // Convert angle to radians
        matrix[0][0] = 1; matrix[0][1] = 0; matrix[0][2] = 0; matrix[0][3] = 0;
        matrix[1][0] = 0; matrix[1][1] = cos(rad); matrix[1][2] = -sin(rad); matrix[1][3] = 0;
        matrix[2][0] = 0; matrix[2][1] = sin(rad); matrix[2][2] = cos(rad); matrix[2][3] = 0;
        matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
    }
    
    // Function to rotate a point around the Y-axis by an angle
    void rotateY(float angle, float matrix[4][4]) {
        float rad = angle * M_PI / 180.0;  // Convert angle to radians
        matrix[0][0] = cos(rad); matrix[0][1] = 0; matrix[0][2] = sin(rad); matrix[0][3] = 0;
        matrix[1][0] = 0; matrix[1][1] = 1; matrix[1][2] = 0; matrix[1][3] = 0;
        matrix[2][0] = -sin(rad); matrix[2][1] = 0; matrix[2][2] = cos(rad); matrix[2][3] = 0;
        matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
    }
    
    // Function to rotate a point around the Z-axis by an angle
    void rotateZ(float angle, float matrix[4][4]) {
        float rad = angle * M_PI / 180.0;  // Convert angle to radians
        matrix[0][0] = cos(rad); matrix[0][1] = -sin(rad); matrix[0][2] = 0; matrix[0][3] = 0;
        matrix[1][0] = sin(rad); matrix[1][1] = cos(rad); matrix[1][2] = 0; matrix[1][3] = 0;
        matrix[2][0] = 0; matrix[2][1] = 0; matrix[2][2] = 1; matrix[2][3] = 0;
        matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
    }
    
    // Function to plot the 3D object (in 2D projection)
    void plot3DObject(Point object[], int n) {
        for (int i = 0; i < n - 1; i++) {
            line(object[i].x, object[i].y, object[i + 1].x, object[i + 1].y);
        }
        line(object[n - 1].x, object[n - 1].y, object[0].x, object[0].y);
    }
    
    int main() {
        int gd = DETECT, gm;
        initgraph(&gd, &gm, "");  // Initialize graphics mode
    
        int n;
        Point object[MAX_VERTICES];
    
        // Input the number of vertices of the 3D object (polygon)
        printf("Enter the number of vertices of the 3D object: ");
        scanf("%d", &n);
    
        // Input the coordinates of the 3D object
        printf("Enter the coordinates of the object vertices (x y z):\n");
        for (int i = 0; i < n; i++) {
            printf("Vertex %d: ", i + 1);
            scanf("%f %f %f", &object[i].x, &object[i].y, &object[i].z);
            object[i].w = 1;  // Homogeneous coordinate (w = 1 for 3D)
        }
    
        // Plot the original object
        setcolor(WHITE);
        plot3DObject(object, n);
    
        // Choose rotation axis and angle
        int choice;
        printf("\nChoose the axis of rotation:\n");
        printf("1. X-axis\n");
        printf("2. Y-axis\n");
        printf("3. Z-axis\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
    
        float matrix[4][4] = { 0 };
        Point rotatedObject[MAX_VERTICES];
    
        // Apply rotation
        float angle;
        printf("Enter rotation angle (in degrees): ");
        scanf("%f", &angle);
    
        switch (choice) {
            case 1:
                rotateX(angle, matrix);
                break;
            case 2:
                rotateY(angle, matrix);
                break;
            case 3:
                rotateZ(angle, matrix);
                break;
            default:
                printf("Invalid choice\n");
                closegraph();
                return 0;
        }
    
        // Apply the rotation matrix to all vertices of the object
        for (int i = 0; i < n; i++) {
            rotatedObject[i] = multiplyMatrix(object[i], matrix);
        }
    
        // Plot the rotated object (in 2D projection)
        setcolor(RED);
        plot3DObject(rotatedObject, n);
    
        // Wait for the user to press a key before closing the graphics window
        getch();
        closegraph();
        return 0;
    }