3D Scaling Program Using C Programming

🎯 Introduction

Welcome to the 3D Scaling Program using C programming. This program demonstrates a basic implementation of 3D scaling, a fundamental concept in computer graphics and computer-aided design. Scaling is the process of enlarging or reducing the size of an object in the coordinate space. In this program, we'll explore how to scale a 3D object represented as a 3D rectangular cuboid (box) using user-provided scaling factors.

🎯 Concept

In computer graphics, 3D scaling involves altering the dimensions of an object in three dimensions: length (x-axis), width (y-axis), and height (z-axis). Scaling can be uniform or non-uniform, depending on whether the scaling factors along all axes are the same or different. For a 3D cuboid, we can use the scaling factors along the x and y axes to increase or decrease its size.

🎯 3D Scaling Example Program 

#include <stdio.h>

#include <math.h>

#include <graphics.h>


int x1, x2, y1, y2, mx, my, depth;


void draw();

void scale();


int main()

{

    int gd = DETECT, gm, c;


    initgraph(&gd, &gm, "d:\\tc\\bgi");


    printf("\n\t\t3D Transformation Scaling\n\n");

    printf("Enter 1st top value (x1, y1): ");

    scanf("%d%d", &x1, &y1);


    printf("Enter right bottom value (x2, y2): ");

    scanf("%d%d", &x2, &y2);


    depth = (x2 - x1) / 4;

    mx = (x1 + x2) / 2;

    my = (y1 + y2) / 2;


    draw();


    getch();

    closegraph();

    return 0;

}


void draw()

{

    // Draw the original 3D cuboid

    bar3d(x1, y1, x2, y2, depth, 1);

}


void scale()

{

    int x, y, a1, a2, b1, b2, dep;


    printf("\n\nEnter scaling factors along x and y axes: ");

    scanf("%d%d", &x, &y);


    a1 = mx + (x1 - mx) * x;

    a2 = mx + (x2 - mx) * x;

    b1 = my + (y1 - my) * y;

    b2 = my + (y2 - my) * y;


    dep = (a2 - a1) / 4;


    // Set the graphics color to highlight the original cuboid in a different color

    setcolor(RED);

    draw();


    // Draw the scaled 3D cuboid

    setcolor(WHITE);

    bar3d(a1, b1, a2, b2, dep, 1);

}


Note: As I mentioned earlier, the graphics library used in this program is outdated and specific to the Turbo C compiler. Modern graphics libraries like OpenGL or SDL can be used for better portability and compatibility.

Remember that if you want to run this program, you need to use an older compiler like Turbo C and set up the graphics libraries accordingly. Alternatively, consider updating the program to use a more modern graphics library for better compatibility with modern systems.

🎯 3D Scaling Example Program Detailed Explanation

Header Files and Global Variables:

The program starts by including the necessary header files for graphics and mathematical functions. It also declares global variables x1, x2, y1, y2, mx, my, and depth. These variables will be used to store the coordinates and depth of the original 3D cuboid and the center point.

Initialization and Input:

The main function initializes the graphics mode and prints a welcome message. It then prompts the user to enter the top-left (x1, y1) and bottom-right (x2, y2) coordinates of the 3D cuboid. The depth of the cuboid is calculated as one-fourth of the difference between x2 and x1. The midpoint of the cuboid, (mx, my), is also calculated.

Drawing the Original 3D Cuboid:

The draw function is responsible for drawing the original 3D cuboid using the bar3d function provided by the graphics library. This function will draw a filled 3D rectangular box based on the given coordinates and depth.

Scaling Function:

The scale function is used to perform the scaling operation on the original cuboid. It prompts the user to enter scaling factors x and y along the x and y axes, respectively.

Calculating New Coordinates:

The function calculates the new top-left (a1, b1) and bottom-right (a2, b2) coordinates of the scaled cuboid based on the scaling factors entered by the user. The new depth dep is set to one-fourth of the difference between a2 and a1.

Drawing the Scaled 3D Cuboid:

The scaled cuboid is drawn using the bar3d function with the new coordinates and depth. Before drawing the scaled cuboid, the color of the graphics context is set to highlight the original cuboid in a different color using the setcolor function.

🎯 Summary

This C program demonstrates 3D scaling of a rectangular cuboid in a graphics environment. It takes user inputs for the original cuboid's coordinates, calculates the midpoint and depth, and then allows the user to scale the cuboid along the x and y axes. The program then displays both the original and scaled cuboids using the graphics library functions.

🎯 Key Points