Finished ig

This commit is contained in:
Tyler Beckman 2024-11-18 00:16:40 -07:00
parent 149dce9495
commit 99f1a4863a
Signed by: Ty
GPG key ID: 2813440C772555A4
20 changed files with 187 additions and 167 deletions

View file

@ -1,4 +1,5 @@
#include "EquilateralTriangle.h" #include "EquilateralTriangle.h"
#include "GeometryUtils.h" #include "GeometryUtils.h"
bool EquilateralTriangle::validate() { bool EquilateralTriangle::validate() {
@ -6,7 +7,8 @@ bool EquilateralTriangle::validate() {
double sideTwo = calculate_distance(mVertices[1], mVertices[2]); double sideTwo = calculate_distance(mVertices[1], mVertices[2]);
double sideThree = calculate_distance(mVertices[2], mVertices[0]); double sideThree = calculate_distance(mVertices[2], mVertices[0]);
// Equilateral triangles must (a) Be a triangle and (b) Have three equal sides // Equilateral triangles must (a) Be a triangle and (b) Have three equal
return lengths_make_triangle(sideOne, sideTwo, sideThree) // sides
&& (double_eq(sideOne, sideTwo) && double_eq(sideTwo, sideThree)); return lengths_make_triangle(sideOne, sideTwo, sideThree) &&
(double_eq(sideOne, sideTwo) && double_eq(sideTwo, sideThree));
} }

View file

@ -1,8 +1,8 @@
#include <cmath>
#include "GeometryUtils.h" #include "GeometryUtils.h"
const double EPSILON = std::numeric_limits<double>::epsilon(); #include <cmath>
const double EPSILON = 1e-7;
bool double_eq(double first, double second) { bool double_eq(double first, double second) {
if (std::abs(first - second) <= EPSILON) { if (std::abs(first - second) <= EPSILON) {
@ -12,21 +12,20 @@ bool double_eq(double first, double second) {
} }
} }
double calculate_distance(Coordinate& firstPoint, Coordinate& secondPoint) { double calculate_distance(Coordinate &firstPoint, Coordinate &secondPoint) {
return std::sqrt( return std::sqrt(std::pow(secondPoint.x - firstPoint.x, 2) +
std::pow(secondPoint.x - firstPoint.x, 2) std::pow(secondPoint.y - firstPoint.y, 2));
+ std::pow(secondPoint.y - firstPoint.y, 2)
);
} }
bool lengths_make_triangle(double sideOne, double sideTwo, double sideThree) { bool lengths_make_triangle(double sideOne, double sideTwo, double sideThree) {
// Not a triangle if one of the side lengths is 0 // Not a triangle if one of the side lengths is 0
if (sideOne <= EPSILON || sideTwo <= EPSILON || sideThree <= EPSILON) { if (sideOne <= 0 || sideTwo <= 0 || sideThree <= 0) {
return false; return false;
} }
// Not a triangle if the sum of any two side lengths >= the third length // Not a triangle if the sum of any two side lengths >= the third length
if (sideOne + sideTwo <= sideThree || sideTwo + sideThree <= sideOne || sideThree + sideOne <= sideTwo) { if (sideOne + sideTwo <= sideThree || sideTwo + sideThree <= sideOne ||
sideThree + sideOne <= sideTwo) {
return false; return false;
} }

View file

@ -1,10 +1,12 @@
#ifndef GEOMETRY_UTILS_H #ifndef GEOMETRY_UTILS_H
#define GEOMETRY_UTILS_H #define GEOMETRY_UTILS_H
#include "Coordinate.h" #include "Coordinate.h"
#include <limits> #include <limits>
/** /**
* @brief Compares two doubles to see if they are equal, within the system epsilon range * @brief Compares two doubles to see if they are equal, within the system
* epsilon range
* *
* @param first The first value to compare * @param first The first value to compare
* @param second The second value to compare * @param second The second value to compare
@ -20,10 +22,11 @@ bool double_eq(double first, double second);
* @param secondPoint The second coordinate point to compare the first to * @param secondPoint The second coordinate point to compare the first to
* @return double The pythagorean distance between the two points * @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 triangle * @brief Returns true if all of the side lengths in the specified array make a
* triangle
* *
* @param sideOne The first side to check for "triangle-ness" * @param sideOne The first side to check for "triangle-ness"
* @param sideTwo The second side to check for "triangle-ness" * @param sideTwo The second side to check for "triangle-ness"

View file

@ -1,4 +1,5 @@
#include "IsoscelesTriangle.h" #include "IsoscelesTriangle.h"
#include "GeometryUtils.h" #include "GeometryUtils.h"
bool IsoscelesTriangle::validate() { bool IsoscelesTriangle::validate() {
@ -6,7 +7,9 @@ bool IsoscelesTriangle::validate() {
double sideTwo = calculate_distance(mVertices[1], mVertices[2]); double sideTwo = calculate_distance(mVertices[1], mVertices[2]);
double sideThree = calculate_distance(mVertices[2], mVertices[0]); double sideThree = calculate_distance(mVertices[2], mVertices[0]);
// Isosceles triangles must (a) Be a triangle and (b) Have two sides that equal each other // Isosceles triangles must (a) Be a triangle and (b) Have two sides that
return lengths_make_triangle(sideOne, sideTwo, sideThree) // equal each other
&& (double_eq(sideOne, sideTwo) || double_eq(sideTwo, sideThree) || double_eq(sideThree, sideOne)); return lengths_make_triangle(sideOne, sideTwo, sideThree) &&
(double_eq(sideOne, sideTwo) || double_eq(sideTwo, sideThree) ||
double_eq(sideThree, sideOne));
} }

View file

@ -3,7 +3,7 @@ SRC_FILES = main.cpp Coordinate.cpp Triangle.cpp Polygon.cpp ScaleneTriangle.cpp
# Tyler's custom makefile extensions for CSCI200 (anyone can use these if they want) # Tyler's custom makefile extensions for CSCI200 (anyone can use these if they want)
.DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all" .DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all"
.PHONY: pack clean-run c run fmt .PHONY: pack clean-run c run fmt r
## Adds only the necessary files for build into a .tar.gz file, named appropriately ## Adds only the necessary files for build into a .tar.gz file, named appropriately
ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp) ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp)
@ -25,6 +25,7 @@ c: clean
## Simply builds and then executes the program ## Simply builds and then executes the program
run: all run: all
./$(TARGET) ./$(TARGET)
r: run
## Formats all cpp, h, and hpp files with clang-format, using my personal clang-format config ## Formats all cpp, h, and hpp files with clang-format, using my personal clang-format config
fmt: fmt:
@ -43,7 +44,7 @@ setup: setupsrc depend
# NO EDITS NEEDED BELOW THIS LINE # NO EDITS NEEDED BELOW THIS LINE
CXX = g++ CXX ?= g++
CXXFLAGS = -O2 CXXFLAGS = -O2
CXXFLAGS_DEBUG = -g CXXFLAGS_DEBUG = -g
CXXFLAGS_WARN = -Wall -Wextra -Wunreachable-code -Wshadow -Wpedantic CXXFLAGS_WARN = -Wall -Wextra -Wunreachable-code -Wshadow -Wpedantic
@ -106,7 +107,7 @@ main.o: main.cpp Coordinate.h EquilateralTriangle.h Triangle.h Polygon.h \
IsoscelesTriangle.h Rhombus.h Quadrilateral.h ScaleneTriangle.h IsoscelesTriangle.h Rhombus.h Quadrilateral.h ScaleneTriangle.h
Coordinate.o: Coordinate.cpp Coordinate.h Coordinate.o: Coordinate.cpp Coordinate.h
Triangle.o: Triangle.cpp Triangle.h Polygon.h Coordinate.h Triangle.o: Triangle.cpp Triangle.h Polygon.h Coordinate.h
Polygon.o: Polygon.cpp Coordinate.h Polygon.h Polygon.o: Polygon.cpp Polygon.h Coordinate.h
ScaleneTriangle.o: ScaleneTriangle.cpp ScaleneTriangle.h Triangle.h \ ScaleneTriangle.o: ScaleneTriangle.cpp ScaleneTriangle.h Triangle.h \
Polygon.h Coordinate.h GeometryUtils.h Polygon.h Coordinate.h GeometryUtils.h
GeometryUtils.o: GeometryUtils.cpp GeometryUtils.h Coordinate.h GeometryUtils.o: GeometryUtils.cpp GeometryUtils.h Coordinate.h

View file

@ -1,8 +1,9 @@
#include <SFML/Graphics/ConvexShape.hpp> #include "Polygon.h"
#include <SFML/Graphics/RenderTarget.hpp>
#include "Coordinate.h" #include "Coordinate.h"
#include "Polygon.h"
#include <SFML/Graphics/ConvexShape.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
APolygon::APolygon() { APolygon::APolygon() {
_color = sf::Color::White; _color = sf::Color::White;
@ -10,15 +11,11 @@ APolygon::APolygon() {
mVertices = nullptr; mVertices = nullptr;
} }
APolygon::~APolygon() { APolygon::~APolygon() { delete[] mVertices; }
delete[] mVertices;
}
void APolygon::setColor(const sf::Color COLOR) { void APolygon::setColor(const sf::Color COLOR) { _color = COLOR; }
_color = COLOR;
}
void APolygon::draw(sf::RenderTarget& window) { void APolygon::draw(sf::RenderTarget &window) {
sf::ConvexShape shape(mNumVertices); sf::ConvexShape shape(mNumVertices);
for (int i = 0; i < mNumVertices; i++) { for (int i = 0; i < mNumVertices; i++) {
shape.setPoint(i, sf::Vector2f(mVertices[i].x, mVertices[i].y)); shape.setPoint(i, sf::Vector2f(mVertices[i].x, mVertices[i].y));

View file

@ -1,9 +1,9 @@
#ifndef POLYGON_H #ifndef POLYGON_H
#define POLYGON_H #define POLYGON_H
#include <SFML/Graphics.hpp>
#include "Coordinate.h" #include "Coordinate.h"
#include <SFML/Graphics.hpp>
class APolygon { class APolygon {
public: public:
/** /**
@ -26,7 +26,7 @@ class APolygon {
* *
* @param window The render target to draw the polygon on * @param window The render target to draw the polygon on
*/ */
void draw(sf::RenderTarget& window); void draw(sf::RenderTarget &window);
/** /**
* @brief Sets the coordinate point at a specific vertex of this polygon * @brief Sets the coordinate point at a specific vertex of this polygon
* *
@ -37,13 +37,16 @@ class APolygon {
/** /**
* @brief Returns if the created polygon is valid or not * @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 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 * @return false The vertices are invalid for the current polygon type
*/ */
virtual bool validate() = 0; virtual bool validate() = 0;
protected: protected:
short mNumVertices; short mNumVertices;
Coordinate* mVertices; Coordinate *mVertices;
private: private:
sf::Color _color; sf::Color _color;
}; };

View file

@ -1,4 +1,5 @@
#include "Quadrilateral.h" #include "Quadrilateral.h"
#include "Coordinate.h" #include "Coordinate.h"
AQuadrilateral::AQuadrilateral() { AQuadrilateral::AQuadrilateral() {

View file

@ -1,4 +1,5 @@
#include "Rhombus.h" #include "Rhombus.h"
#include "GeometryUtils.h" #include "GeometryUtils.h"
#include "IsoscelesTriangle.h" #include "IsoscelesTriangle.h"
@ -18,9 +19,10 @@ bool Rhombus::validate() {
secondTriangle.setCoordinate(1, mVertices[2]); secondTriangle.setCoordinate(1, mVertices[2]);
secondTriangle.setCoordinate(2, mVertices[3]); secondTriangle.setCoordinate(2, mVertices[3]);
// A valid rhombus must (a) Have vertices (0, 1, 2) make a valid isosceles triangle, // A valid rhombus must (a) Have vertices (0, 1, 2) make a valid isosceles
// (b) Have vertices (0, 2, 3) make a valid isosceles triangle, and (c) Have all // triangle, (b) Have vertices (0, 2, 3) make a valid isosceles triangle,
// sides of equal length // and (c) Have all sides of equal length
return firstTriangle.validate() && secondTriangle.validate() return firstTriangle.validate() && secondTriangle.validate() &&
&& (double_eq(sideOne, sideTwo) && double_eq(sideTwo, sideThree) && double_eq(sideThree, sideFour)); (double_eq(sideOne, sideTwo) && double_eq(sideTwo, sideThree) &&
double_eq(sideThree, sideFour));
} }

View file

@ -1,4 +1,5 @@
#include "ScaleneTriangle.h" #include "ScaleneTriangle.h"
#include "GeometryUtils.h" #include "GeometryUtils.h"
bool ScaleneTriangle::validate() { bool ScaleneTriangle::validate() {
@ -6,7 +7,9 @@ bool ScaleneTriangle::validate() {
double sideTwo = calculate_distance(mVertices[1], mVertices[2]); double sideTwo = calculate_distance(mVertices[1], mVertices[2]);
double sideThree = calculate_distance(mVertices[2], mVertices[0]); double sideThree = calculate_distance(mVertices[2], mVertices[0]);
// Scalene triangles must (a) Be a triangle and (b) Have no sides that equal each other // Scalene triangles must (a) Be a triangle and (b) Have no sides that equal
return lengths_make_triangle(sideOne, sideTwo, sideThree) // each other
&& (!double_eq(sideOne, sideTwo) && !double_eq(sideTwo, sideThree) && !double_eq(sideThree, sideOne)); return lengths_make_triangle(sideOne, sideTwo, sideThree) &&
(!double_eq(sideOne, sideTwo) && !double_eq(sideTwo, sideThree) &&
!double_eq(sideThree, sideOne));
} }

View file

@ -14,6 +14,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <memory>
#include <cstdio> #include <cstdio>
@ -32,10 +33,10 @@ int main(void) {
return 1; return 1;
} }
std::vector<APolygon *> polygonList; std::vector<std::unique_ptr<APolygon>> polygonList;
APolygon *currentPolygon; std::unique_ptr<APolygon> currentPolygon;
char type; char type;
double x1, y1, x2, y2, x3, y3, x4, y4; double x1, y1, x2, y2, x3, y3, x4 = -1, y4 = -1;
int r, g, b; int r, g, b;
while (true) { while (true) {
@ -51,16 +52,16 @@ int main(void) {
switch (type) { switch (type) {
case 'S': case 'S':
currentPolygon = new ScaleneTriangle; currentPolygon = std::make_unique<ScaleneTriangle>();
break; break;
case 'I': case 'I':
currentPolygon = new IsoscelesTriangle; currentPolygon = std::make_unique<IsoscelesTriangle>();
break; break;
case 'E': case 'E':
currentPolygon = new EquilateralTriangle; currentPolygon = std::make_unique<EquilateralTriangle>();
break; break;
case 'R': case 'R':
currentPolygon = new Rhombus; currentPolygon = std::make_unique<Rhombus>();
break; break;
default: default:
std::cout << "polygon is invalid - \"" << type << " " << x1 std::cout << "polygon is invalid - \"" << type << " " << x1
@ -80,16 +81,21 @@ int main(void) {
if (!currentPolygon->validate()) { if (!currentPolygon->validate()) {
std::cout << "polygon is invalid - \"" << type << " " << x1 << " " std::cout << "polygon is invalid - \"" << type << " " << x1 << " "
<< y1 << " " << x2 << " " << y2 << " " << x3 << " " << y3 << y1 << " " << x2 << " " << y2 << " " << x3 << " " << y3;
<< " " << x4 << " " << y4 << " " << r << " " << g << " " if (x4 != -1) {
<< b << "\"" << std::endl; std::cout << " " << x4 << " " << y4;
}
std::cout << " " << r << " " << g << " " << b << "\"" << std::endl;
} else { } else {
polygonList.push_back(currentPolygon); polygonList.push_back(currentPolygon);
} }
}; };
// Start SFML Rendering logic // Start SFML Rendering logic
sf::RenderWindow window( sf::VideoMode(640, 640), ":3" ); sf::RenderWindow window(
sf::VideoMode(640, 640), ":3",
sf::Style::Titlebar | sf::Style::Close // Disable window resize
);
window.setVerticalSyncEnabled(true); window.setVerticalSyncEnabled(true);
sf::Event event; sf::Event event;