查找 C++ 中给定点集的简单封闭路径
考虑我们有一组点。我们必须找到一个包含所有点的简单封闭路径。假设这些点如下,那么下一张图片是在这些点上形成的封闭路径。
要获取路径,我们必须执行以下步骤 -
找到左下方的点作为 P
根据围绕 P 的极角逆时针对其他 n - 1 点进行排序,如果两个点的极角相同,则将它们作为最短距离
遍历排序后的点列表,然后创建路径
实例
#include <bits/stdc++.h> using namespace std; class Point { public: int x, y; }; Point p0; int euclid_dist(Point p1, Point p2) { return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y); } int orientation(Point p1, Point p2, Point p3) { int val = (p2.y - p1.y) * (p3.x - p2.x) - (p2.x - p1.x) * (p3.y - p2.y); if (val == 0) return 0; // colinear return (val > 0)? 1: 2; // clockwise or counterclock wise } int compare(const void *vp1, const void *vp2) { Point *p1 = (Point *)vp1; Point *p2 = (Point *)vp2; int o = orientation(p0, *p1, *p2); if (o == 0) return (euclid_dist(p0, *p2) >= euclid_dist(p0, *p1))? -1 : 1; return (o == 2)? -1: 1; } void findClosedPath(Point points[], int n) { int y_min = points[0].y, min = 0; for (int i = 1; i < n; i++) { int y = points[i].y; if ((y < y_min) || (y_min == y && points[i].x < points[min].x)) y_min = points[i].y, min = i; } swap(points[0], points[min]); p0 = points[0]; qsort(&points[1], n-1, sizeof(Point), compare); //sort on polar angle for (int i=0; i<n; i++) cout << "(" << points[i].x << ", "<< points[i].y <<"), "; } int main() { Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4},{0, 0}, {1, 2}, {3, 1}, {3, 3}}; int n = sizeof(points)/sizeof(points[0]); findClosedPath(points, n); }
产出
(0, 0), (3, 1), (1, 1), (2, 2), (3, 3), (4, 4), (1, 2), (0, 3),
广告