diff --git a/generator/include/shapes/cone.hpp b/generator/include/shapes/cone.hpp new file mode 100644 index 0000000..5cccd65 --- /dev/null +++ b/generator/include/shapes/cone.hpp @@ -0,0 +1,13 @@ +#ifndef CONE_HPP +#define CONE_HPP + +#include +#include +#include +#include + +#include "../utils.hpp" + +bool generateCone(float radius, float height, int slices, int stacks, const char* filepath); + +#endif //CONE_HPP \ No newline at end of file diff --git a/generator/src/main.cpp b/generator/src/main.cpp index ca54dcc..ada6c8e 100644 --- a/generator/src/main.cpp +++ b/generator/src/main.cpp @@ -3,6 +3,8 @@ #include "../include/shapes/cube.hpp" #include "../include/shapes/plane.hpp" +#include "../include/shapes/cone.hpp" + void generateFigure(int argc, char* argv[]) { if (argc < 5) { @@ -34,6 +36,13 @@ void generateFigure(int argc, char* argv[]) { } else if (figureName == "cone" && argc == 7) { // Generate Cone std::cout << "Generating Cone\n"; + float radius = std::stof(argv[2]); + float height = std::stof(argv[3]); + int slices = std::stoi(argv[4]); + int stacks = std::stoi(argv[5]); + + generateCone(radius,height,slices,stacks, figureType); + } else { std::cerr << "Invalid arguments\n"; } diff --git a/generator/src/shapes/cone.cpp b/generator/src/shapes/cone.cpp new file mode 100644 index 0000000..666b99c --- /dev/null +++ b/generator/src/shapes/cone.cpp @@ -0,0 +1,57 @@ +#include "utils.hpp" + +#include +#include +#include +#include + +std::vector coneTriangles(float radius, float height, int slices, int stacks) { + std::vector points; + + // Generate points for each stack + for (int i = 0; i < stacks; ++i) { + float y1 = height * i / stacks; + float y2 = height * (i + 1) / stacks; + + for (int j = 0; j < slices; ++j) { + float theta1 = 2.0f * M_PI * j / slices; + float theta2 = 2.0f * M_PI * (j + 1) / slices; + + float x1 = radius * std::cos(theta1); + float z1 = radius * std::sin(theta1); + + float x2 = radius * std::cos(theta2); + float z2 = radius * std::sin(theta2); + + // Side triangles + points.emplace_back(x1, y1, z1); + points.emplace_back(x2, y1, z2); + points.emplace_back(0.0f, y2, 0.0f); + + // Bottom triangles + if (i == 0) { + points.emplace_back(0.0f, 0.0f, 0.0f); + points.emplace_back(x2, y1, z2); + points.emplace_back(x1, y1, z1); + } + } + } + + // Cone tip + points.emplace_back(0.0f, height, 0.0f); + + return points; +} + +bool generateCone(float radius, float height, int slices, int stacks, const char* filepath) { + std::vector triangles = coneTriangles(radius, height, slices, stacks); + + if (triangles.empty()) { + std::cerr << "Error: Empty vector of triangles.\n"; + return false; + } + + saveToFile(triangles, filepath); + + return true; +}