From 6654519d84c80e110ee18fe3361c15eda516cce5 Mon Sep 17 00:00:00 2001 From: karl Date: Sun, 29 Nov 2020 01:38:07 +0100 Subject: [PATCH] Change distance_to to distance_squared_to more efficiency! (and we only need it for comparisons anyways) --- Line.h | 8 ++++---- Quickhull.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Line.h b/Line.h index 775fce6..95cf75d 100644 --- a/Line.h +++ b/Line.h @@ -1,7 +1,5 @@ #pragma once -#include // For abs, sqrt - #include "Point.h" class Line @@ -51,12 +49,14 @@ public: } } - float distance_to(Point other) const + float distance_squared_to(Point other) const { float a = from().y() - to().y(); float b = to().x() - from().x(); float c = from().x() * to().y() - to().x() * from().y(); + + float d = abs(a * other.x() + b * other.y() + c); - return abs(a * other.x() + b * other.y() + c) / sqrt(a * a + b * b); + return (d * d) / (a * a + b * b); } }; \ No newline at end of file diff --git a/Quickhull.h b/Quickhull.h index ed1da94..96123c7 100644 --- a/Quickhull.h +++ b/Quickhull.h @@ -67,7 +67,7 @@ private: for (const Point &point : input) { - float this_distance = line.distance_to(point); + float this_distance = line.distance_squared_to(point); if (this_distance > furthest_distance) { furthest_distance = this_distance; @@ -85,17 +85,17 @@ private: // Points inside the hull are added because of random lines. for (const Point &point : input) { - float this_distance = line.distance_to(point); + float this_distance = line.distance_squared_to(point); // TODO: Both are required, otherwise only one side of the rectangle is // taken -- why? - if (this_distance == furthest_distance || line.distance_to(point) == 0) + if (this_distance == furthest_distance || line.distance_squared_to(point) == 0) { output.emplace_back(point); } } input.remove_if([furthest_distance, line](Point point) { - return furthest_distance == line.distance_to(point); + return furthest_distance == line.distance_squared_to(point); }); // Hotfix end