From acfb3a4500c7eb2a093c4df5241faceb5c65e276 Mon Sep 17 00:00:00 2001 From: karl Date: Sat, 28 Nov 2020 21:11:32 +0100 Subject: [PATCH] More Quickhull implementation; add Triangle class --- Line.h | 2 +- Makefile | 4 ++-- Quickhull.h | 27 +++++++++++++++++++++++++++ Triangle.h | 22 ++++++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 Triangle.h diff --git a/Line.h b/Line.h index 1727495..b156d02 100644 --- a/Line.h +++ b/Line.h @@ -22,7 +22,7 @@ public: // Return true if the given point is to the right of this line. // False is also returned if the point is directly on the line. - bool is_point_right(Point other) + bool is_point_right(Point other) const { other -= from(); diff --git a/Makefile b/Makefile index a1ab1a7..109ae4d 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,9 @@ CXXFLAGS = -Wall -O3 # If SFML is not installed in a standard path, you need to tell the dynamic linker where to find the SFML libraries first by specifying LD_LIBRARY_PATH: # export LD_LIBRARY_PATH=/lib && ./sfml-app -quickhull: main.o Quickhull.h Point.h Line.h Timing.o Display.o +quickhull: main.o Quickhull.h Point.h Line.h Triangle.h Timing.o Display.o # link with sfml libs; -lsfml-network -lsfml-audio currently not needed - $(CXX) $(CXXFLAGS) -o quickhull main.o Quickhull.h Point.h Line.h Timing.o Display.o -lsfml-system -lsfml-window -lsfml-graphics + $(CXX) $(CXXFLAGS) -o quickhull main.o Quickhull.h Point.h Line.h Triangle.h Timing.o Display.o -lsfml-system -lsfml-window -lsfml-graphics main.o: main.cpp Timing.h $(CXX) $(CXXFLAGS) -c main.cpp diff --git a/Quickhull.h b/Quickhull.h index c458cb2..a49ade5 100644 --- a/Quickhull.h +++ b/Quickhull.h @@ -5,6 +5,7 @@ #include "Point.h" #include "Line.h" +#include "Triangle.h" class Quickhull { @@ -61,10 +62,36 @@ private: if (input.empty()) return; // Find the point which is furthest away from the line, add it to the output + Point furthest_point; // TODO + + output.emplace_back(furthest_point); // Build a triangle with these 3 points + + // The order with which we must pass the points depends on where the new furthest point is + Point a, b, c; + if (line.is_point_right(furthest_point)) + { + a = line.from(); + b = line.to(); + c = furthest_point; + } + else + { + a = line.from(); + b = furthest_point; + c = line.to(); + } + + Triangle triangle(a, b, c); + // Remove points inside this triangle + input.remove_if([triangle](Point point) + { + return !triangle.is_point_inside(point); + }); // Recursively call get_hull_with_line for each side of the triangle + // TODO } }; \ No newline at end of file diff --git a/Triangle.h b/Triangle.h new file mode 100644 index 0000000..d271c1a --- /dev/null +++ b/Triangle.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Point.h" +#include "Line.h" + +class Triangle +{ +private: + Point m_p1, m_p2, m_p3; + Line m_l1, m_l2, m_l3; + +public: + // The points must come in __clockwise__ order. + Triangle(Point p1, Point p2, Point p3) + : m_p1(p1), m_p2(p2), m_p3(p3), + m_l1(Line(p1, p2)), m_l2(Line(p2, p3)), m_l3(Line(p3, p1)) {} + + bool is_point_inside(Point other) const + { + return m_l1.is_point_right(other) && m_l2.is_point_right(other) && m_l3.is_point_right(other); + } +}; \ No newline at end of file