How to draw a simple cuboid generator in C++

1 Answer

0 votes
#include <iostream>
#include <string>

/**
 * Draws a 3D-style wireframe cuboid in the console.
 * @param w Width of the front face
 * @param h Height of the front face
 * @param d Depth (the slant)
 */
void drawCuboid(int w, int h, int d) {
    // Basic validation to prevent visual glitches
    if (w < 2 || h < 2 || d < 1) {
        std::cout << "Dimensions too small to render." << std::endl;
        return;
    }

    // 1. Top Edge
    std::cout << std::string(d, ' ') << "+" << std::string(w, '-') << "+" << std::endl;

    // 2. Top Face (Slanted)
    for (int i = 1; i < d; ++i) {
        std::cout << std::string(d - i, ' ') << "/" << std::string(w, ' ') << "/" 
                  << std::string(i - 1, ' ') << "|" << std::endl;
    }

    // 3. Middle Connecting Edge
    std::cout << "+" << std::string(w, '-') << "+" << std::string(d - 1, ' ') << "|" << std::endl;

    // 4. Front Face & Side Face
    for (int i = 0; i < h; ++i) {
        std::cout << "| " << std::string(w - 2, ' ') << " |"; // Using w-2 to account for the '|' borders
        
        // Logic for the receding side edge
        if (i < h - d) {
            std::cout << std::string(d - 1, ' ') << "|";
        } else if (i == h - d) {
            std::cout << std::string(d - 1, ' ') << "+";
        } else {
            int slant_remaining = h - i - 1;
            std::cout << std::string(slant_remaining, ' ') << "/";
        }
        std::cout << std::endl;
    }

    // 5. Bottom Edge
    std::cout << "+" << std::string(w, '-') << "+" << std::endl;
}

int main() {
    int w = 20, h = 8, d = 4;

    std::cout << "--- Cuboid Generator ---" << std::endl;

    drawCuboid(w, h, d);

    return 0;
}


/*
run:
 
--- Cuboid Generator ---
    +--------------------+
   /                    /|
  /                    / |
 /                    /  |
+--------------------+   |
|                    |   |
|                    |   |
|                    |   |
|                    |   |
|                    |   +
|                    |  /
|                    | /
|                    |/
+--------------------+
 
*/
 

 



answered 19 hours ago by avibootz
...