diff --git a/Coordinate.cpp b/Coordinate.cpp index d8b1f26..4b77944 100644 --- a/Coordinate.cpp +++ b/Coordinate.cpp @@ -1,11 +1,11 @@ #include "Coordinate.h" Coordinate::Coordinate() { - x = 0; - y = 0; + x = 0; + y = 0; } Coordinate::Coordinate(const double X, const double Y) { - x = X; - y = Y; + x = X; + y = Y; } \ No newline at end of file diff --git a/Coordinate.h b/Coordinate.h index 684e29d..f7180e8 100644 --- a/Coordinate.h +++ b/Coordinate.h @@ -1,12 +1,12 @@ #ifndef COORDINATE_H #define COORDINATE_H class Coordinate { - public: - Coordinate(); - Coordinate(const double X, const double Y); +public: + Coordinate(); + Coordinate(const double X, const double Y); - double x; - double y; + double x; + double y; }; #endif // COORDINATE_H diff --git a/EquilateralTriangle.cpp b/EquilateralTriangle.cpp index 52a66a0..8480ecd 100644 --- a/EquilateralTriangle.cpp +++ b/EquilateralTriangle.cpp @@ -3,12 +3,12 @@ #include "GeometryUtils.h" bool EquilateralTriangle::validate() { - double sideOne = calculate_distance(mVertices[0], mVertices[1]); - double sideTwo = calculate_distance(mVertices[1], mVertices[2]); - double sideThree = calculate_distance(mVertices[2], mVertices[0]); + double sideOne = calculate_distance(mVertices[0], mVertices[1]); + double sideTwo = calculate_distance(mVertices[1], mVertices[2]); + double sideThree = calculate_distance(mVertices[2], mVertices[0]); - // Equilateral triangles must (a) Be a triangle and (b) Have three equal - // sides - return lengths_make_triangle(sideOne, sideTwo, sideThree) && - (double_eq(sideOne, sideTwo) && double_eq(sideTwo, sideThree)); + // Equilateral triangles must (a) Be a triangle and (b) Have three equal + // sides + return lengths_make_triangle(sideOne, sideTwo, sideThree) && + (double_eq(sideOne, sideTwo) && double_eq(sideTwo, sideThree)); } \ No newline at end of file diff --git a/EquilateralTriangle.h b/EquilateralTriangle.h index 8e5b2ea..1c4c445 100644 --- a/EquilateralTriangle.h +++ b/EquilateralTriangle.h @@ -2,8 +2,8 @@ #define EQUILATERAL_TRIANGLE_H #include "Triangle.h" class EquilateralTriangle : public ATriangle { - public: - bool validate() override; +public: + bool validate() override; }; #endif // EQUILATERAL_TRIANGLE_H diff --git a/GeometryUtils.cpp b/GeometryUtils.cpp index 86e5acb..bc0a6fc 100644 --- a/GeometryUtils.cpp +++ b/GeometryUtils.cpp @@ -5,29 +5,29 @@ const double EPSILON = 1e-7; bool double_eq(double first, double second) { - if (std::abs(first - second) <= EPSILON) { - return true; - } else { - return false; - } + if (std::abs(first - second) <= EPSILON) { + return true; + } else { + return false; + } } -double calculate_distance(Coordinate &firstPoint, Coordinate &secondPoint) { - return std::sqrt(std::pow(secondPoint.x - firstPoint.x, 2) + - std::pow(secondPoint.y - firstPoint.y, 2)); +double calculate_distance(Coordinate& firstPoint, Coordinate& secondPoint) { + return std::sqrt(std::pow(secondPoint.x - firstPoint.x, 2) + + std::pow(secondPoint.y - firstPoint.y, 2)); } bool lengths_make_triangle(double sideOne, double sideTwo, double sideThree) { - // Not a triangle if one of the side lengths is 0 - if (sideOne <= 0 || sideTwo <= 0 || sideThree <= 0) { - return false; - } + // Not a triangle if one of the side lengths is 0 + if (sideOne <= 0 || sideTwo <= 0 || sideThree <= 0) { + return false; + } - // Not a triangle if the sum of any two side lengths >= the third length - if (sideOne + sideTwo <= sideThree || sideTwo + sideThree <= sideOne || - sideThree + sideOne <= sideTwo) { - return false; - } + // Not a triangle if the sum of any two side lengths >= the third length + if (sideOne + sideTwo <= sideThree || sideTwo + sideThree <= sideOne || + sideThree + sideOne <= sideTwo) { + return false; + } - return true; + return true; } \ No newline at end of file diff --git a/GeometryUtils.h b/GeometryUtils.h index 2dd27aa..8c254ca 100644 --- a/GeometryUtils.h +++ b/GeometryUtils.h @@ -22,7 +22,7 @@ bool double_eq(double first, double second); * @param secondPoint The second coordinate point to compare the first to * @return double The pythagorean distance between the two points */ -double calculate_distance(Coordinate &firstPoint, Coordinate &secondPoint); +double calculate_distance(Coordinate& firstPoint, Coordinate& secondPoint); /** * @brief Returns true if all of the side lengths in the specified array make a diff --git a/IsoscelesTriangle.cpp b/IsoscelesTriangle.cpp index 61fd1e6..e285862 100644 --- a/IsoscelesTriangle.cpp +++ b/IsoscelesTriangle.cpp @@ -3,13 +3,13 @@ #include "GeometryUtils.h" bool IsoscelesTriangle::validate() { - double sideOne = calculate_distance(mVertices[0], mVertices[1]); - double sideTwo = calculate_distance(mVertices[1], mVertices[2]); - double sideThree = calculate_distance(mVertices[2], mVertices[0]); + double sideOne = calculate_distance(mVertices[0], mVertices[1]); + double sideTwo = calculate_distance(mVertices[1], mVertices[2]); + double sideThree = calculate_distance(mVertices[2], mVertices[0]); - // Isosceles triangles must (a) Be a triangle and (b) Have two sides that - // equal each other - return lengths_make_triangle(sideOne, sideTwo, sideThree) && - (double_eq(sideOne, sideTwo) || double_eq(sideTwo, sideThree) || - double_eq(sideThree, sideOne)); + // Isosceles triangles must (a) Be a triangle and (b) Have two sides that + // equal each other + return lengths_make_triangle(sideOne, sideTwo, sideThree) && + (double_eq(sideOne, sideTwo) || double_eq(sideTwo, sideThree) || + double_eq(sideThree, sideOne)); } \ No newline at end of file diff --git a/IsoscelesTriangle.h b/IsoscelesTriangle.h index bbbf327..e5d3895 100644 --- a/IsoscelesTriangle.h +++ b/IsoscelesTriangle.h @@ -2,8 +2,8 @@ #define ISOSCELES_TRIANGLE_H #include "Triangle.h" class IsoscelesTriangle : public ATriangle { - public: - bool validate() override; +public: + bool validate() override; }; #endif // ISOSCELES_TRIANGLE_H diff --git a/Polygon.cpp b/Polygon.cpp index ee99b48..9b9048a 100644 --- a/Polygon.cpp +++ b/Polygon.cpp @@ -6,25 +6,25 @@ #include APolygon::APolygon() { - _color = sf::Color::White; - mNumVertices = 0; - mVertices = nullptr; + _color = sf::Color::White; + mNumVertices = 0; + mVertices = nullptr; } APolygon::~APolygon() { delete[] mVertices; } void APolygon::setColor(const sf::Color COLOR) { _color = COLOR; } -void APolygon::draw(sf::RenderTarget &window) { - sf::ConvexShape shape(mNumVertices); - for (int i = 0; i < mNumVertices; i++) { - shape.setPoint(i, sf::Vector2f(mVertices[i].x, mVertices[i].y)); - } - shape.setFillColor(_color); +void APolygon::draw(sf::RenderTarget& window) { + sf::ConvexShape shape(mNumVertices); + for (int i = 0; i < mNumVertices; i++) { + shape.setPoint(i, sf::Vector2f(mVertices[i].x, mVertices[i].y)); + } + shape.setFillColor(_color); - window.draw(shape); + window.draw(shape); } void APolygon::setCoordinate(const int IDX, const Coordinate COORD) { - mVertices[IDX] = COORD; + mVertices[IDX] = COORD; } diff --git a/Polygon.h b/Polygon.h index d787846..d65fc1b 100644 --- a/Polygon.h +++ b/Polygon.h @@ -5,50 +5,50 @@ #include class APolygon { - public: - /** - * @brief Construct a new Polygon object, with a white color and 0 vertices - */ - APolygon(); - /** - * @brief Destroy the APolygon object - */ - virtual ~APolygon(); +public: + /** + * @brief Construct a new Polygon object, with a white color and 0 vertices + */ + APolygon(); + /** + * @brief Destroy the APolygon object + */ + virtual ~APolygon(); - /** - * @brief Sets the color of this polygon - * - * @param COLOR The color to change the polygon to - */ - void setColor(const sf::Color COLOR); - /** - * @brief Draws this polygon to a SFML render target - * - * @param window The render target to draw the polygon on - */ - void draw(sf::RenderTarget &window); - /** - * @brief Sets the coordinate point at a specific vertex of this polygon - * - * @param IDX The index of the vertex to change - * @param COORD The coordinate location to set the vertex to - */ - void setCoordinate(const int IDX, const Coordinate COORD); - /** - * @brief Returns if the created polygon is valid or not - * - * @return true All of the vertices of the polygon line up with the current - * polygon type - * @return false The vertices are invalid for the current polygon type - */ - virtual bool validate() = 0; + /** + * @brief Sets the color of this polygon + * + * @param COLOR The color to change the polygon to + */ + void setColor(const sf::Color COLOR); + /** + * @brief Draws this polygon to a SFML render target + * + * @param window The render target to draw the polygon on + */ + void draw(sf::RenderTarget& window); + /** + * @brief Sets the coordinate point at a specific vertex of this polygon + * + * @param IDX The index of the vertex to change + * @param COORD The coordinate location to set the vertex to + */ + void setCoordinate(const int IDX, const Coordinate COORD); + /** + * @brief Returns if the created polygon is valid or not + * + * @return true All of the vertices of the polygon line up with the current + * polygon type + * @return false The vertices are invalid for the current polygon type + */ + virtual bool validate() = 0; - protected: - short mNumVertices; - Coordinate *mVertices; +protected: + short mNumVertices; + Coordinate* mVertices; - private: - sf::Color _color; +private: + sf::Color _color; }; #endif // POLYGON_H diff --git a/Quadrilateral.cpp b/Quadrilateral.cpp index 2ad4532..2de60b1 100644 --- a/Quadrilateral.cpp +++ b/Quadrilateral.cpp @@ -3,6 +3,6 @@ #include "Coordinate.h" AQuadrilateral::AQuadrilateral() { - mNumVertices = 4; - mVertices = new Coordinate[4]; + mNumVertices = 4; + mVertices = new Coordinate[4]; } \ No newline at end of file diff --git a/Quadrilateral.h b/Quadrilateral.h index 178445e..dfd3a25 100644 --- a/Quadrilateral.h +++ b/Quadrilateral.h @@ -2,8 +2,8 @@ #define QUADRILATERAL_H #include "Polygon.h" class AQuadrilateral : public APolygon { - public: - AQuadrilateral(); +public: + AQuadrilateral(); }; #endif // QUADRILATERAL_H diff --git a/Rhombus.cpp b/Rhombus.cpp index 291c85c..dbe2794 100644 --- a/Rhombus.cpp +++ b/Rhombus.cpp @@ -4,25 +4,25 @@ #include "IsoscelesTriangle.h" bool Rhombus::validate() { - double sideOne = calculate_distance(mVertices[0], mVertices[1]); - double sideTwo = calculate_distance(mVertices[1], mVertices[2]); - double sideThree = calculate_distance(mVertices[2], mVertices[3]); - double sideFour = calculate_distance(mVertices[3], mVertices[0]); + double sideOne = calculate_distance(mVertices[0], mVertices[1]); + double sideTwo = calculate_distance(mVertices[1], mVertices[2]); + double sideThree = calculate_distance(mVertices[2], mVertices[3]); + double sideFour = calculate_distance(mVertices[3], mVertices[0]); - IsoscelesTriangle firstTriangle; - firstTriangle.setCoordinate(0, mVertices[0]); - firstTriangle.setCoordinate(1, mVertices[1]); - firstTriangle.setCoordinate(2, mVertices[2]); + IsoscelesTriangle firstTriangle; + firstTriangle.setCoordinate(0, mVertices[0]); + firstTriangle.setCoordinate(1, mVertices[1]); + firstTriangle.setCoordinate(2, mVertices[2]); - IsoscelesTriangle secondTriangle; - secondTriangle.setCoordinate(0, mVertices[0]); - secondTriangle.setCoordinate(1, mVertices[2]); - secondTriangle.setCoordinate(2, mVertices[3]); + IsoscelesTriangle secondTriangle; + secondTriangle.setCoordinate(0, mVertices[0]); + secondTriangle.setCoordinate(1, mVertices[2]); + secondTriangle.setCoordinate(2, mVertices[3]); - // A valid rhombus must (a) Have vertices (0, 1, 2) make a valid isosceles - // triangle, (b) Have vertices (0, 2, 3) make a valid isosceles triangle, - // and (c) Have all sides of equal length - return firstTriangle.validate() && secondTriangle.validate() && - (double_eq(sideOne, sideTwo) && double_eq(sideTwo, sideThree) && - double_eq(sideThree, sideFour)); + // A valid rhombus must (a) Have vertices (0, 1, 2) make a valid isosceles + // triangle, (b) Have vertices (0, 2, 3) make a valid isosceles triangle, + // and (c) Have all sides of equal length + return firstTriangle.validate() && secondTriangle.validate() && + (double_eq(sideOne, sideTwo) && double_eq(sideTwo, sideThree) && + double_eq(sideThree, sideFour)); } \ No newline at end of file diff --git a/Rhombus.h b/Rhombus.h index 81c49f6..9391c9d 100644 --- a/Rhombus.h +++ b/Rhombus.h @@ -2,8 +2,8 @@ #define RHOMBUS_H #include "Quadrilateral.h" class Rhombus : public AQuadrilateral { - public: - bool validate() override; +public: + bool validate() override; }; #endif // RHOMBUS_H diff --git a/ScaleneTriangle.cpp b/ScaleneTriangle.cpp index ec969be..97198eb 100644 --- a/ScaleneTriangle.cpp +++ b/ScaleneTriangle.cpp @@ -3,13 +3,13 @@ #include "GeometryUtils.h" bool ScaleneTriangle::validate() { - double sideOne = calculate_distance(mVertices[0], mVertices[1]); - double sideTwo = calculate_distance(mVertices[1], mVertices[2]); - double sideThree = calculate_distance(mVertices[2], mVertices[0]); + double sideOne = calculate_distance(mVertices[0], mVertices[1]); + double sideTwo = calculate_distance(mVertices[1], mVertices[2]); + double sideThree = calculate_distance(mVertices[2], mVertices[0]); - // Scalene triangles must (a) Be a triangle and (b) Have no sides that equal - // each other - return lengths_make_triangle(sideOne, sideTwo, sideThree) && - (!double_eq(sideOne, sideTwo) && !double_eq(sideTwo, sideThree) && - !double_eq(sideThree, sideOne)); + // Scalene triangles must (a) Be a triangle and (b) Have no sides that equal + // each other + return lengths_make_triangle(sideOne, sideTwo, sideThree) && + (!double_eq(sideOne, sideTwo) && !double_eq(sideTwo, sideThree) && + !double_eq(sideThree, sideOne)); } \ No newline at end of file diff --git a/ScaleneTriangle.h b/ScaleneTriangle.h index f9b6123..a3b665d 100644 --- a/ScaleneTriangle.h +++ b/ScaleneTriangle.h @@ -2,8 +2,8 @@ #define SCALENE_TRIANGLE_H #include "Triangle.h" class ScaleneTriangle : public ATriangle { - public: - bool validate() override; +public: + bool validate() override; }; #endif // SCALENE_TRIANGLE_H diff --git a/Triangle.cpp b/Triangle.cpp index b3ffd3c..6cc3c0c 100644 --- a/Triangle.cpp +++ b/Triangle.cpp @@ -1,6 +1,6 @@ #include "Triangle.h" ATriangle::ATriangle() { - mNumVertices = 3; - mVertices = new Coordinate[3]; + mNumVertices = 3; + mVertices = new Coordinate[3]; } \ No newline at end of file diff --git a/Triangle.h b/Triangle.h index 7f919cc..8c18b31 100644 --- a/Triangle.h +++ b/Triangle.h @@ -2,8 +2,8 @@ #define TRIANGLE_H #include "Polygon.h" class ATriangle : public APolygon { - public: - ATriangle(); +public: + ATriangle(); }; #endif // TRIANGLE_H diff --git a/main.cpp b/main.cpp index d575d8d..ac02b65 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,10 @@ /** * @author Tyler Beckman (tyler_beckman@mines.edu) - * @brief A program template for CSCI200 + * @brief A program to read polygon points and colors from a data file and + * display them utilizing the SFML library, validating all shapes at each point + * during processing. * @version 1 - * @date 2024-09-21 + * @date 2024-11-11 */ #include "Coordinate.h" @@ -21,98 +23,98 @@ #include int main(void) { - // Start file parsing logic - std::cout << "Please enter file path to read polygons from: "; - std::string filePath; - std::cin >> filePath; + // Start file parsing logic + std::cout << "Please enter file path to read polygons from: "; + std::string filePath; + std::cin >> filePath; - std::ifstream file(filePath); - if (file.fail()) { - std::cout << "Failed to open specified file path " << filePath - << ", does it exist?" << std::endl; - return 1; - } + std::ifstream file(filePath); + if (file.fail()) { + std::cout << "Failed to open specified file path " << filePath + << ", does it exist?" << std::endl; + return 1; + } - std::vector> polygonList; - std::unique_ptr currentPolygon; - char type; - double x1, y1, x2, y2, x3, y3, x4 = -1, y4 = -1; - int r, g, b; + std::vector> polygonList; + std::unique_ptr currentPolygon; + char type; + double x1, y1, x2, y2, x3, y3, x4 = -1, y4 = -1; + int r, g, b; - while (true) { - file >> type >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; - if (type == 'R') { - file >> x4 >> y4; - } - file >> r >> g >> b; + while (true) { + file >> type >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; + if (type == 'R') { + file >> x4 >> y4; + } + file >> r >> g >> b; - if (file.fail()) { - break; - } + if (file.fail()) { + break; + } - switch (type) { - case 'S': - currentPolygon = std::make_unique(); - break; - case 'I': - currentPolygon = std::make_unique(); - break; - case 'E': - currentPolygon = std::make_unique(); - break; - case 'R': - currentPolygon = std::make_unique(); - break; - default: - std::cout << "polygon is invalid - \"" << type << " " << x1 - << " " << y1 << " " << x2 << " " << y2 << " " << x3 - << " " << y3 << " " << x4 << " " << y4 << " " << r - << " " << g << " " << b << "\"" << std::endl; - continue; - } + switch (type) { + case 'S': + currentPolygon = std::make_unique(); + break; + case 'I': + currentPolygon = std::make_unique(); + break; + case 'E': + currentPolygon = std::make_unique(); + break; + case 'R': + currentPolygon = std::make_unique(); + break; + default: + std::cout << "polygon is invalid - \"" << type << " " << x1 << " " << y1 + << " " << x2 << " " << y2 << " " << x3 << " " << y3 << " " + << x4 << " " << y4 << " " << r << " " << g << " " << b << "\"" + << std::endl; + continue; + } - currentPolygon->setCoordinate(0, Coordinate(x1, y1)); - currentPolygon->setCoordinate(1, Coordinate(x2, y2)); - currentPolygon->setCoordinate(2, Coordinate(x3, y3)); - if (type == 'R') { - currentPolygon->setCoordinate(3, Coordinate(x4, y4)); - } - currentPolygon->setColor(sf::Color(r, g, b)); + currentPolygon->setCoordinate(0, Coordinate(x1, y1)); + currentPolygon->setCoordinate(1, Coordinate(x2, y2)); + currentPolygon->setCoordinate(2, Coordinate(x3, y3)); + if (type == 'R') { + currentPolygon->setCoordinate(3, Coordinate(x4, y4)); + } + currentPolygon->setColor(sf::Color(r, g, b)); - if (!currentPolygon->validate()) { - std::cout << "polygon is invalid - \"" << type << " " << x1 << " " - << y1 << " " << x2 << " " << y2 << " " << x3 << " " << y3; - if (x4 != -1) { - std::cout << " " << x4 << " " << y4; - } - std::cout << " " << r << " " << g << " " << b << "\"" << std::endl; - } else { - polygonList.push_back(currentPolygon); - } - }; + if (!currentPolygon->validate()) { + std::cout << "polygon is invalid - \"" << type << " " << x1 << " " << y1 + << " " << x2 << " " << y2 << " " << x3 << " " << y3; + if (x4 != -1) { + std::cout << " " << x4 << " " << y4; + } + std::cout << " " << r << " " << g << " " << b << "\"" << std::endl; + } else { + polygonList.push_back(std::move(currentPolygon)); + } + }; - // Start SFML Rendering logic - sf::RenderWindow window( - sf::VideoMode(640, 640), ":3", - sf::Style::Titlebar | sf::Style::Close // Disable window resize - ); - window.setVerticalSyncEnabled(true); + // Start SFML Rendering logic + sf::RenderWindow window( + sf::VideoMode(640, 640), "correct horse battery staple", + sf::Style::Titlebar | sf::Style::Close // Disable window resize + ); + window.setVerticalSyncEnabled(true); - sf::Event event; + sf::Event event; - while (window.isOpen()) { - window.clear(); + while (window.isOpen()) { + window.clear(); - for (size_t i = 0; i < polygonList.size(); i++) { - polygonList.at(i)->draw(window); - } + for (size_t i = 0; i < polygonList.size(); i++) { + polygonList.at(i)->draw(window); + } - window.display(); + window.display(); - while (window.pollEvent(event)) { - if (event.type == sf::Event::Closed) { - window.close(); - } - } - } + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) { + window.close(); + } + } + } }