From 9fcd2b2e9e948feb9b1e5e4338ea20c3275adedf Mon Sep 17 00:00:00 2001 From: incredibleLeitman Date: Thu, 26 Nov 2020 22:19:14 +0100 Subject: [PATCH] adding math utility header --- Point.h | 4 ++-- Utility.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 12 +++++++++--- 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 Utility.h diff --git a/Point.h b/Point.h index 2f7ce72..cc72cbf 100644 --- a/Point.h +++ b/Point.h @@ -8,12 +8,12 @@ private: public: Point(float x, float y) : m_x(x), m_y(y) {} - float x() + float x () const { return m_x; } - float y() + float y () const { return m_y; } diff --git a/Utility.h b/Utility.h new file mode 100644 index 0000000..bf07aab --- /dev/null +++ b/Utility.h @@ -0,0 +1,53 @@ +#pragma once +#ifndef UTILITY_H + +static float sign (Point &p1, Point &p2, Point &p3) +{ + return (p1.x() - p3.x()) * (p2.y() - p3.y()) - (p2.x() - p3.x()) * (p1.y() - p3.y()); +} + +static bool IsPointInTriangle(Point &pt, Point &p1, Point &p2, Point &p3) +{ + float d1 = sign(pt, p1, p2); + float d2 = sign(pt, p2, p3); + float d3 = sign(pt, p3, p1); + + bool has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0); + bool has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0); + + return !(has_neg && has_pos); +} + +static bool SortForMinXMaxY (const Point& a, const Point& b) +{ + if (a.x() != b.x()) + { + return (a.x() < b.x()); + } + return (a.y() > b.y()); +} + +static bool SortForMinYMaxX(const Point& a, const Point& b) +{ + if (a.y() != b.y()) + { + return (a.x() < b.x()); + } + return (a.y() > b.y()); +} + +static void sortPoints (std::vector& pts) +{ + std::sort(pts.begin(), pts.end(), SortForMinXMaxY); +} + +// TODO: what happens if all/more points are on hor/vert line? -> sort for x, than y should handle this +static std::pair getMinMaxX(std::vector& pts) +{ + // TODO: check if already sorted? assume array is sorted? call sort utility function??? + //sortPoints(pts); + std::sort(pts.begin(), pts.end(), SortForMinXMaxY); + + return std::make_pair(pts[0], pts[pts.size() - 1]); +} +#endif // UTILITY_H \ No newline at end of file diff --git a/main.cpp b/main.cpp index a0efeff..f5f416d 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,7 @@ #include "Display.h" #include "Point.h" // TODO: check if there is a usable SFML or c++ class #include "Timing.h" +#include "Utility.h" // TODOs: // - use SFML vec2 instead of Point class @@ -43,8 +44,8 @@ int main (int argc, char **argv) srand(static_cast (0)); // fixed seed for testing for (int i = 0; i < valCount; ++i) { - float x = static_cast (rand()) / (static_cast (RAND_MAX / WIDTH)); - float y = static_cast (rand()) / (static_cast (RAND_MAX / HEIGHT)); + float x = OFFSET + static_cast (rand()) / (static_cast (RAND_MAX / (WIDTH - OFFSET))); + float y = OFFSET + static_cast (rand()) / (static_cast (RAND_MAX / (HEIGHT - OFFSET))); points.push_back(Point(x, y)); } } @@ -76,7 +77,8 @@ int main (int argc, char **argv) } } - // TODO: need sort here? + // TODO: sort here, once and for all? xD + sortPoints(points); for (Point& pt : points) { std::cout << "pt: " << pt.x() << ", " << pt.y() << std::endl; @@ -85,6 +87,10 @@ int main (int argc, char **argv) if (vis) { // TODO: use data as ctor argument? pointer? + + // TEST to check SFML coordinate system + points.push_back(Point(0, 0)); + Display display; display.setData(points); display.show();