DEV Community

Alexandru
Alexandru

Posted on

Stop Using Meters in C++ Graphics: Building a Real-World Material Mass Calculator

When building software for engineers, architects, or machinists, you quickly learn one golden rule: nobody uses meters on the shop floor or construction site. Everything is measured in millimeters (mm).

Today, we will build a practical C++ console application that estimates the mass of various 3D structural shapes based on their material density and dimensions—optimized for real-world blueprints.


📐 The Physics & Math Behind It

The core formula is straightforward:

Mass = Density × Volume
Enter fullscreen mode Exit fullscreen mode

Since density is typically given in kg/m³ and structural dimensions are in mm, our calculated volume will initially be in cubic millimeters (mm³). To get the correct mass in kilograms, we must divide the final volume by 1,000,000,000 (10⁹) to convert mm³ to m³.

Here are the geometric shapes we will support:

Rectangular Prism: V = l × w × h

Solid Cylinder: V = (π × d² × l) / 4

Hollow Tube: V = [π × (D² - d²) × l] / 4

Hexagonal Prism: V = (3 × √3 / 2) × a² × l

#include <iostream>
#include <cmath>

using namespace std;

int main()
{ 
    char solid;
    double mat_density;
    double mass = 0.0; 

    cout << "Enter the solid type: r(rectangular prism), c(cylinder), t(tube), h(hexagonal prism): ";
    cin >> solid;

    while (solid != 'r' && solid != 'c' && solid != 't' && solid != 'h') {
        cout << "Invalid solid type! Enter r, c, t, or h: ";
        cin >> solid;
    }

    cout << "Enter the material density [kg/m3]: ";
    cin >> mat_density;

    switch(solid){
        case 'r': {
            double l, w, h;
            cout << "Enter the length of the prism [mm]: ";
            cin >> l;
            cout << "Enter the width of the prism [mm]: ";
            cin >> h;
            cout << "Enter the height of the prism [mm]: ";
            cin >> w;

            mass = mat_density * (l * w * h) / 1000000000.0; 
            break;
        }
        case 'c': {
            double d, l;
            cout << "Enter the length of the cylinder [mm]: "; 
            cin >> l;
            cout << "Enter the diameter of the base [mm]: ";
            cin >> d;

            mass = mat_density * M_PI * d * d * l / 4.0 / 1000000000.0;
            break;
        }
        case 't': {
            double D, d, l;
            cout << "Enter the length of the tube [mm]: "; 
            cin >> l;
            cout << "Enter the outer diameter [mm]: ";
            cin >> D;
            cout << "Enter the inner diameter [mm]: ";
            cin >> d;

            if (D <= d) {
                cout << "[Error] Outer diameter must be larger than inner diameter!\n";
                return 0;
            }

            mass = mat_density * l * M_PI * (D * D - d * d) / 4.0 / 1000000000.0;
            break;
        }
        case 'h': {
            double a, l;
            cout << "Enter the length of the prism [mm]: "; 
            cin >> l;
            cout << "Enter the side of the base [mm]: ";
            cin >> a;

            mass = mat_density * (3.0 * sqrt(3.0) / 2.0) * a * a * l / 1000000000.0;
            break;
        }
    }

    cout << "The mass of the solid is: " << mass << " kg" << endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

⚡ Key Takeaways for Clean Code
Hardcoded Constants over pow(): Using 1000000000.0 directly instead of pow(10, 9) avoids runtime floating-point overhead, making compile-time math cleaner.

Early Returns: Catching the impossible case where a pipe's inner diameter is larger than its outer diameter prevents negative mass artifacts.

Initialization Safeguards: Forcing mass = 0.0 at the top ensures that if something catastrophic happens, your system won't output garbage data stored in your RAM.

What shape should we add next? Steel I-beams or asymmetric channels? Let me know in the comments!

Top comments (0)