如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。
给定一组点,连接这些点,使其不交叉。


例子:
输入:points\[\] = {(0, 3), (1, 1), (2, 2), (4, 4),
(0, 0), (1, 2), (3, 1}, {3, 3}};
输出:按以下顺序连接点不会
造成任何交叉
{(0, 0), (3, 1), (1, 1), (2, 2), (3, 3),
(4, 4), (1, 2), (0, 3)}
我们强烈建议您最小化浏览器窗口,并先自行尝试一下。
其原理是使用排序功能。
**·**通过比较所有点的 y 坐标找到最底部的点。如果两个点的 y 值相同,则选择 x 坐标值较小的点。将最底部的点放在第一个位置。

· 考虑剩余的 n-1 个点,并按极角逆时针方向围绕 points0 进行排序。如果两个点的极角相同,则将极角最近的点放在前面。
**·**遍历已排序的数组(按角度升序排序)可得到简单的闭合路径。

如何计算角度?
一种方法是使用三角函数。
注意:我们并不关心角度的实际值,我们只想按角度排序。
思路:利用方向来比较角度,而无需实际计算它们!
方向参考文章:
JavaScript 3个有序点的方向:https://blog.csdn.net/hefeng_aspnet/article/details/165296941
C# 3个有序点的方向:https://blog.csdn.net/hefeng_aspnet/article/details/165295883
Python 3个有序点的方向:https://blog.csdn.net/hefeng_aspnet/article/details/165295274
Java 3个有序点的方向:https://blog.csdn.net/hefeng_aspnet/article/details/165295086
C++ 3个有序点的方向:https://blog.csdn.net/hefeng_aspnet/article/details/165293858
以下是上述想法的实现:
// A C++ program to find simple closed path for n points
// for explanation of orientation()
#include <bits/stdc++.h>
using namespace std;
struct Point
{
int x, y;
};
// A global point needed for sorting points with reference
// to the first point. Used in compare function of qsort()
Point p0;
// A utility function to swap two points
int swap(Point &p1, Point &p2)
{
Point temp = p1;
p1 = p2;
p2 = temp;
}
// A utility function to return square of distance between
// p1 and p2
int dist(Point p1, Point p2)
{
return (p1.x - p2.x)*(p1.x - p2.x) +
(p1.y - p2.y)*(p1.y - p2.y);
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are collinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int orientation(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // collinear
return (val > 0)? 1: 2; // clockwise or counterclock wise
}
// A function used by library function qsort() to sort
// an array of points with respect to the first point
int compare(const void *vp1, const void *vp2)
{
Point *p1 = (Point *)vp1;
Point *p2 = (Point *)vp2;
// Find orientation
int o = orientation(p0, *p1, *p2);
if (o == 0)
return (dist(p0, *p2) >= dist(p0, *p1))? -1 : 1;
return (o == 2)? -1: 1;
}
// Prints simple closed path for a set of n points.
void printClosedPath(Point points\[\], int n)
{
// Find the bottommost point
int ymin = points0.y, min = 0;
for (int i = 1; i < n; i++)
{
int y = pointsi.y;
// Pick the bottom-most. In case of tie, choose the
// left most point
if ((y < ymin) || (ymin == y &&
pointsi.x < pointsmin.x))
ymin = pointsi.y, min = i;
}
// Place the bottom-most point at first position
swap(points0, pointsmin);
// Sort n-1 points with respect to the first point.
// A point p1 comes before p2 in sorted output if p2
// has larger polar angle (in counterclockwise
// direction) than p1
p0 = points0;
qsort(&points1, n-1, sizeof(Point), compare);
// Now stack has the output points, print contents
// of stack
for (int i=0; i<n; i++)
cout << "(" << pointsi.x << ", "
<< pointsi.y <<"), ";
}
// Driver program to test above functions
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(points0);
printClosedPath(points, n);
return 0;
}
输出:
(0,0),(3,1),(1,1),(2,2),(3,3),
(4,4),(1,2),(0,3),
如果使用 O(nLogn) 的排序算法对点进行排序,则上述解决方案的时间复杂度为 O(nLogn)。
辅助空间复杂度为O(1),因为没有占用额外的空间。
如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。