More Quickhull implementation; add Triangle class

This commit is contained in:
karl 2020-11-28 21:11:32 +01:00
parent 3cfa15a33c
commit acfb3a4500
4 changed files with 52 additions and 3 deletions

2
Line.h
View File

@ -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();

View File

@ -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=<sfml-install-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

View File

@ -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
}
};

22
Triangle.h Normal file
View File

@ -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);
}
};