Change distance_to to distance_squared_to

more efficiency! (and we only need it for comparisons anyways)
This commit is contained in:
karl 2020-11-29 01:38:07 +01:00
parent c0bba8b264
commit 6654519d84
2 changed files with 8 additions and 8 deletions

8
Line.h
View File

@ -1,7 +1,5 @@
#pragma once #pragma once
#include <cmath> // For abs, sqrt
#include "Point.h" #include "Point.h"
class Line 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 a = from().y() - to().y();
float b = to().x() - from().x(); float b = to().x() - from().x();
float c = from().x() * to().y() - to().x() * from().y(); float c = from().x() * to().y() - to().x() * from().y();
return abs(a * other.x() + b * other.y() + c) / sqrt(a * a + b * b); float d = abs(a * other.x() + b * other.y() + c);
return (d * d) / (a * a + b * b);
} }
}; };

View File

@ -67,7 +67,7 @@ private:
for (const Point &point : input) 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) if (this_distance > furthest_distance)
{ {
furthest_distance = this_distance; furthest_distance = this_distance;
@ -85,17 +85,17 @@ private:
// Points inside the hull are added because of random lines. // Points inside the hull are added because of random lines.
for (const Point &point : input) 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 // TODO: Both are required, otherwise only one side of the rectangle is
// taken -- why? // 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); output.emplace_back(point);
} }
} }
input.remove_if([furthest_distance, line](Point 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 // Hotfix end