#include <algorithm>
#include <vector>
#include <iostream>
#include <math.h>
#include <Windows.h>
using namespace std;
// point class representing point in 2D plane
struct Point
{
int x;
int y;
Point() = default;
Point(int xx, int yy) : x(xx), y(yy) {}
};
// compare function using x coordinate
bool comparX(const Point &p1, const Point &p2)
{
return p1.x < p2.x;
}
// compare function using y coordinate
bool comparY(const Point &p1, const Point &p2)
{
return p1.y < p2.y;
}
// euclidean distance function
float dist(const Point &p1, const Point &p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
double bruteForce(vector<Point> &points, int start, int end)
{
double minDist = DBL_MAX;
for (int i = start; i < end; ++i)
{
for (int j = i + 1; j < end; ++j)
{
if (dist(points[i], points[j]) < minDist)
minDist = dist(points[i], points[j]);
}
}
return minDist;
}
double closestUtil(vector<Point> &points, int start, int end)
{
int n = end - start;
// base case: if there are 2 or 3 points, use brute force
if (n <= 3)
return bruteForce(points, start, end);
int mid = n / 2;
double delta1 = closestUtil(points, start, mid); // left part
double delta2 = closestUtil(points, mid + 1, end); // right part
double delta = min(delta1, delta2);
// strip is the points set that all points are closer than delta to the
// line passing through the mid set
vector<Point> strip;
for (int i = start; i < end; ++i)
if (abs(points[i].x - points[mid].x) < delta)
strip.push_back(points[i]);
sort(strip.begin(), strip.end(), comparY);
// O(nlogn) complexity
for (int i = 0; i < strip.size(); ++i)
for (int j = i + 1; j < strip.size() && strip[j].y - strip[i].y < delta; ++j)
if (dist(strip[i], strip[j]) < delta)
delta = dist(strip[i], strip[j]);
return delta;
}
double closestPair(vector<Point> &points)
{
sort(points.begin(), points.end(), comparX);
return closestUtil(points, 0, points.size());
}
int main()
{
vector<Point> p{ { 2, 3 },{ 12, 30 },{ 40, 50 },{ 5, 1 },{ 12, 10 },{ 3, 5 }, {100, 3}, {10, 10} };
cout << closestPair(p) << endl;
system("PAUSE");
return 0;
}
closest pair of points(O(n*logn*logn)) solution
最新推荐文章于 2024-01-30 00:36:40 发布