diff --git a/Line.h b/Line.h index e198558..8ff77cb 100644 --- a/Line.h +++ b/Line.h @@ -42,8 +42,7 @@ public: other -= from(); // Cross product greater than zero? - // Note: We need ">=" here so that we include points which are directly on the existing hull. - if (m_to_from_origin.x() * other.y() - m_to_from_origin.y() * other.x() >= 0) + if (m_to_from_origin.x() * other.y() - m_to_from_origin.y() * other.x() > 0) { return true; } diff --git a/Quickhull.h b/Quickhull.h index aada29b..f953ce9 100644 --- a/Quickhull.h +++ b/Quickhull.h @@ -8,6 +8,7 @@ #include "Display.h" #include "Triangle.h" +#include "Utility.h" // For IsPointInRectangle class Quickhull { @@ -19,21 +20,56 @@ public: // Get leftmost and rightmost point Point leftmost(INFINITY, 0.0), rightmost(-INFINITY, 0.0); - for (const Point &point : input) + if (akl) { - if (point.x() < leftmost.x()) { - leftmost = point; - } else if (point.y() > rightmost.y()) { - rightmost = point; - } - } - // Add them to the output - output.emplace_back(leftmost); - output.emplace_back(rightmost); + // Also get highest and lowest point + Point lowest(0.0, -INFINITY), highest(0.0, INFINITY); - // Remove them from the input (as well as duplicates) - input.remove(leftmost); - input.remove(rightmost); + for (const Point &point : input) + { + if (point.x() < leftmost.x()) { + leftmost = point; + } else if (point.x() > rightmost.x()) { + rightmost = point; + } + + if (point.y() < highest.y()) { + highest = point; + } else if (point.y() > lowest.y()) { + lowest = point; + } + } + + Triangle t1(highest, rightmost, lowest); + Triangle t2(lowest, leftmost, highest); + + // Remove all points in this rectangle + input.remove_if([highest, leftmost, lowest, rightmost](const Point &point) + { + return IsPointInRectangle(point, highest, leftmost, lowest, rightmost); + }); + + output.emplace_back(leftmost); + output.emplace_back(rightmost); + output.emplace_back(highest); + output.emplace_back(lowest); + } else { + for (const Point &point : input) + { + if (point.x() < leftmost.x()) { + leftmost = point; + } else if (point.x() > rightmost.x()) { + rightmost = point; + } + } + // Add them to the output + output.emplace_back(leftmost); + output.emplace_back(rightmost); + + // Remove them from the input (as well as duplicates) + input.remove(leftmost); + input.remove(rightmost); + } // Create a line from leftmost to rightmost Line line = Line(leftmost, rightmost);