Skip to content

Commit

Permalink
fix: box, feat: format script
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Feb 29, 2024
1 parent c8781d6 commit e44dfc0
Show file tree
Hide file tree
Showing 10 changed files with 497 additions and 544 deletions.
9 changes: 5 additions & 4 deletions generator/include/shapes/cone.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#ifndef CONE_HPP
#define CONE_HPP

#include <iostream>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#include <cmath>

#include "../utils.hpp"

bool generateCone(float radius, float height, int slices, int stacks, const char* filepath);
bool generateCone(float radius, float height, int slices, int stacks,
const char* filepath);

#endif //CONE_HPP
#endif // CONE_HPP
6 changes: 3 additions & 3 deletions generator/include/shapes/cube.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef CUBE_HPP
#define CUBE_HPP


#include <vector>
#include <string>
#include <vector>

#include "../utils.hpp"

bool generateCube(float length, int divisions, const char* filepath);

#endif //CUBE_HPP
#endif // CUBE_HPP
6 changes: 3 additions & 3 deletions generator/include/shapes/plane.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef SOLAR_SYSTEM_PLANE_HPP
#define SOLAR_SYSTEM_PLANE_HPP


#include <vector>
#include <string>
#include <vector>

#include "../utils.hpp"

bool generatePlane(float length, int divisions, const char* filepath);

#endif //SOLAR_SYSTEM_PLANE_HPP
#endif // SOLAR_SYSTEM_PLANE_HPP
5 changes: 2 additions & 3 deletions generator/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <iostream>
#include <string>

#include "../include/shapes/cone.hpp"
#include "../include/shapes/cube.hpp"
#include "../include/shapes/plane.hpp"
#include "../include/shapes/cone.hpp"


void generateFigure(int argc, char* argv[]) {
if (argc < 5) {
Expand Down Expand Up @@ -41,7 +40,7 @@ void generateFigure(int argc, char* argv[]) {
int slices = std::stoi(argv[4]);
int stacks = std::stoi(argv[5]);

generateCone(radius,height,slices,stacks, figureType);
generateCone(radius, height, slices, stacks, figureType);

} else {
std::cerr << "Invalid arguments\n";
Expand Down
83 changes: 42 additions & 41 deletions generator/src/shapes/cone.cpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
#include "utils.hpp"

#include <iostream>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#include <cmath>

std::vector<Point> coneTriangles(float radius, float height, int slices, int stacks) {
std::vector<Point> points;
float alfa = 2.0f * M_PI / slices;
float yT = height / 2.0f;
float yB = -height / 2.0f;
float lastX = radius * cos(0);
float lastZ = radius * sin(0);

for (int i = 1; i <= slices; ++i) {
float a = i * alfa;
float x = radius * cos(a);
float z = radius * sin(a);


// Side triangles
points.emplace_back(0, height, 0);
points.emplace_back(x, 0, z);
points.emplace_back(lastX, 0, lastZ);

// Bottom triangles
points.emplace_back(lastX, 0, lastZ);
points.emplace_back(x, 0, z);
points.emplace_back(0, 0, 0);

lastX = x;
lastZ = z;
}

return points;
#include "utils.hpp"

std::vector<Point> coneTriangles(float radius, float height, int slices,
int stacks) {
std::vector<Point> points;
float alfa = 2.0f * M_PI / slices;
float yT = height / 2.0f;
float yB = -height / 2.0f;
float lastX = radius * cos(0);
float lastZ = radius * sin(0);

for (int i = 1; i <= slices; ++i) {
float a = i * alfa;
float x = radius * cos(a);
float z = radius * sin(a);

// Side triangles
points.emplace_back(0, height, 0);
points.emplace_back(x, 0, z);
points.emplace_back(lastX, 0, lastZ);

// Bottom triangles
points.emplace_back(lastX, 0, lastZ);
points.emplace_back(x, 0, z);
points.emplace_back(0, 0, 0);

lastX = x;
lastZ = z;
}

return points;
}

bool generateCone(float radius, float height, int slices, int stacks, const char* filepath) {
std::vector<Point> triangles = coneTriangles(radius, height, slices, stacks);
bool generateCone(float radius, float height, int slices, int stacks,
const char* filepath) {
std::vector<Point> triangles = coneTriangles(radius, height, slices, stacks);

if (triangles.empty()) {
std::cerr << "Error: Empty vector of triangles.\n";
return false;
}
if (triangles.empty()) {
std::cerr << "Error: Empty vector of triangles.\n";
return false;
}

saveToFile(triangles, filepath);
saveToFile(triangles, filepath);

return true;
return true;
}
200 changes: 77 additions & 123 deletions generator/src/shapes/cube.cpp
Original file line number Diff line number Diff line change
@@ -1,136 +1,90 @@
#include "utils.hpp"

#include <iostream>
#include <fstream>
#include <iostream>
#include <vector>

std::vector<Point> cubeTriangles(float length, int divisions) {

float halfSize = length / 2.0f;
float step = length / divisions;

std::vector<Point> points;

// Front face
for(int i = 0; i < divisions; ++i) {
for(int j = 0; j < divisions; ++j) {
float x1 = -halfSize + i * step;
float y1 = -halfSize + j * step;
float x2 = x1 + step;
float y2 = y1 + step;

points.push_back(Point(x1, y1, halfSize));
points.push_back(Point(x2, y1, halfSize));
points.push_back(Point(x1, y2, halfSize));

points.push_back(Point(x1, y2, halfSize));
points.push_back(Point(x2, y1, halfSize));
points.push_back(Point(x2, y2, halfSize));
}
}

// Back face
for(int i = 0; i < divisions; ++i) {
for(int j = 0; j < divisions; ++j) {
float x1 = -halfSize + i * step;
float y1 = -halfSize + j * step;
float x2 = x1 + step;
float y2 = y1 + step;

points.push_back(Point(x1, y1, -halfSize));
points.push_back(Point(x1, y2, -halfSize));
points.push_back(Point(x2, y1, -halfSize));

points.push_back(Point(x1, y2, -halfSize));
points.push_back(Point(x2, y2, -halfSize));
points.push_back(Point(x2, y1, -halfSize));
}
}

// Left face
for(int i = 0; i < divisions; ++i) {
for(int j = 0; j < divisions; ++j) {
float y1 = -halfSize + i * step;
float z1 = -halfSize + j * step;
float y2 = y1 + step;
float z2 = z1 + step;

points.push_back(Point(-halfSize, y1, z1));
points.push_back(Point(-halfSize, y1, z2));
points.push_back(Point(-halfSize, y2, z1));

points.push_back(Point(-halfSize, y1, z2));
points.push_back(Point(-halfSize, y2, z2));
points.push_back(Point(-halfSize, y2, z1));
}
}

// Right face
for(int j = 0; j < divisions; ++j) {
for(int i = 0; i < divisions; ++i) {
float y1 = -halfSize + j * step;
float z1 = -halfSize + i * step;
float y2 = y1 + step;
float z2 = z1 + step;

points.push_back(Point(halfSize, y1, z1));
points.push_back(Point(halfSize, y2, z1));
points.push_back(Point(halfSize, y1, z2));

points.push_back(Point(halfSize, y1, z2));
points.push_back(Point(halfSize, y2, z1));
points.push_back(Point(halfSize, y2, z2));
}
}

// Top face
for(int i = 0; i < divisions; ++i) {
for(int j = 0; j < divisions; ++j) {
float x1 = -halfSize + i * step;
float z1 = -halfSize + j * step;
float x2 = x1 + step;
float z2 = z1 + step;

points.push_back(Point(x1, halfSize, z1));
points.push_back(Point(x1, halfSize, z2));
points.push_back(Point(x2, halfSize, z1));

points.push_back(Point(x1, halfSize, z2));
points.push_back(Point(x2, halfSize, z2));
points.push_back(Point(x2, halfSize, z1));
}
}
#include "utils.hpp"

// Bottom face
for(int i = 0; i < divisions; ++i) {
for(int j = 0; j < divisions; ++j) {
float x1 = -halfSize + i * step;
float z1 = -halfSize + j * step;
float x2 = x1 + step;
float z2 = z1 + step;

points.push_back(Point(x1, -halfSize, z1));
points.push_back(Point(x2, -halfSize, z1));
points.push_back(Point(x1, -halfSize, z2));

points.push_back(Point(x1, -halfSize, z2));
points.push_back(Point(x2, -halfSize, z1));
points.push_back(Point(x2, -halfSize, z2));
}
std::vector<Point> cubeTriangles(float length, int divisions) {
float halfSize = length / 2.0f;
float step = length / divisions;

std::vector<Point> points;

for (int i = 0; i < divisions; ++i) {
for (int j = 0; j < divisions; ++j) {
float x1 = -halfSize + i * step;
float y1 = -halfSize + j * step;
float x2 = x1 + step;
float y2 = y1 + step;

// Front Face
points.push_back(Point(x1, y1, halfSize));
points.push_back(Point(x2, y1, halfSize));
points.push_back(Point(x1, y2, halfSize));

points.push_back(Point(x1, y2, halfSize));
points.push_back(Point(x2, y1, halfSize));
points.push_back(Point(x2, y2, halfSize));

// Back Face
points.push_back(Point(x1, y1, -halfSize));
points.push_back(Point(x1, y2, -halfSize));
points.push_back(Point(x2, y1, -halfSize));

points.push_back(Point(x1, y2, -halfSize));
points.push_back(Point(x2, y2, -halfSize));
points.push_back(Point(x2, y1, -halfSize));

// Left Face
points.push_back(Point(-halfSize, x1, y1));
points.push_back(Point(-halfSize, x1, y2));
points.push_back(Point(-halfSize, x2, y1));

points.push_back(Point(-halfSize, x1, y2));
points.push_back(Point(-halfSize, x2, y2));
points.push_back(Point(-halfSize, x2, y1));

// Right Face
points.push_back(Point(halfSize, x1, y1));
points.push_back(Point(halfSize, x2, y1));
points.push_back(Point(halfSize, x1, y2));

points.push_back(Point(halfSize, x1, y2));
points.push_back(Point(halfSize, x2, y1));
points.push_back(Point(halfSize, x2, y2));

// Top Face
points.push_back(Point(x1, halfSize, y1));
points.push_back(Point(x1, halfSize, y2));
points.push_back(Point(x2, halfSize, y1));

points.push_back(Point(x1, halfSize, y2));
points.push_back(Point(x2, halfSize, y2));
points.push_back(Point(x2, halfSize, y1));

// Bottom Face
points.push_back(Point(x1, -halfSize, y1));
points.push_back(Point(x2, -halfSize, y1));
points.push_back(Point(x1, -halfSize, y2));

points.push_back(Point(x1, -halfSize, y2));
points.push_back(Point(x2, -halfSize, y1));
points.push_back(Point(x2, -halfSize, y2));
}
}

return points;
return points;
}

bool generateCube(float length, int divisions, const char* filepath) {
std::vector<Point> triangles = cubeTriangles(length, divisions);
std::vector<Point> triangles = cubeTriangles(length, divisions);

if (triangles.empty()) {
std::cerr << "Error: Empty vector of triangles.\n";
return false;
}
if (triangles.empty()) {
std::cerr << "Error: Empty vector of triangles.\n";
return false;
}

saveToFile(triangles, filepath);
saveToFile(triangles, filepath);

return true;
return true;
}
Loading

0 comments on commit e44dfc0

Please sign in to comment.