📜  凸包的Quickhull算法

📅  最后修改于: 2021-04-26 06:16:01             🧑  作者: Mango

给定一组点,凸包是包含所有给定点的最小凸多边形。

凸包

输入是由点的x和y坐标指定的点的数组。输出是按x坐标的升序排列的这组点的凸包。

例子 :

Input : points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4},
                    {0, 0}, {1, 2}, {3, 1}, {3, 3}};
Output :  The points in convex hull are:
          (0, 0) (0, 3) (3, 1) (4, 4)

Input : points[] = {{0, 3}, {1, 1}
Output : Not Possible
There must be at least three points to form a hull.

Input  : points[] = {(0, 0), (0, 4), (-4, 0), (5, 0), 
                     (0, -6), (1, 0)};
Output : (-4, 0), (5, 0), (0, -6), (0, 4)

我们讨论了凸包问题的以下算法。
凸包|设置1(贾维斯算法或包装)
凸包|套装2(Graham Scan)

QuickHull算法是类似于QuickSort的分而治之算法。令a [0…n-1]为点的输入数组。以下是找到这些点的凸包的步骤。

  1. 查找具有最小x坐标的点,例如,min_x,类似地,具有最大x坐标的点,max_x。
  2. 划一条线将这两个点连接起来,说L。这条线会将整个集合分为两部分。将两个部分一个接一个地进行,然后继续进行。
  3. 对于零件,找到距直线L的最大距离的点P。P形成一个三角形,其点为min_x,max_x。显然,位于该三角形内部的点永远不会是凸包的一部分。
  4. 上面的步骤将问题分为两个子问题(递归解决)。现在,连接点P和min_x的线以及连接点P和max_x的线是新线,并且位于三角形外部的点是点集。重复点号3直到没有一点与线。将此点的端点添加到凸包。

下面是上述想法的C++实现。该实现使用set来存储点,以便可以按排序的顺序打印点。一个点表示为一对。

// C++ program to implement Quick Hull algorithm
// to find convex hull.
#include
using namespace std;
  
// iPair is integer pairs
#define iPair pair
  
// Stores the result (points of convex hull)
set hull;
  
// Returns the side of point p with respect to line
// joining points p1 and p2.
int findSide(iPair p1, iPair p2, iPair p)
{
    int val = (p.second - p1.second) * (p2.first - p1.first) -
              (p2.second - p1.second) * (p.first - p1.first);
  
    if (val > 0)
        return 1;
    if (val < 0)
        return -1;
    return 0;
}
  
// returns a value proportional to the distance
// between the point p and the line joining the
// points p1 and p2
int lineDist(iPair p1, iPair p2, iPair p)
{
    return abs ((p.second - p1.second) * (p2.first - p1.first) -
               (p2.second - p1.second) * (p.first - p1.first));
}
  
// End points of line L are p1 and p2.  side can have value
// 1 or -1 specifying each of the parts made by the line L
void quickHull(iPair a[], int n, iPair p1, iPair p2, int side)
{
    int ind = -1;
    int max_dist = 0;
  
    // finding the point with maximum distance
    // from L and also on the specified side of L.
    for (int i=0; i max_dist)
        {
            ind = i;
            max_dist = temp;
        }
    }
  
    // If no point is found, add the end points
    // of L to the convex hull.
    if (ind == -1)
    {
        hull.insert(p1);
        hull.insert(p2);
        return;
    }
  
    // Recur for the two parts divided by a[ind]
    quickHull(a, n, a[ind], p1, -findSide(a[ind], p1, p2));
    quickHull(a, n, a[ind], p2, -findSide(a[ind], p2, p1));
}
  
void printHull(iPair a[], int n)
{
    // a[i].second -> y-coordinate of the ith point
    if (n < 3)
    {
        cout << "Convex hull not possible\n";
        return;
    }
  
    // Finding the point with minimum and
    // maximum x-coordinate
    int min_x = 0, max_x = 0;
    for (int i=1; i a[max_x].first)
            max_x = i;
    }
  
    // Recursively find convex hull points on
    // one side of line joining a[min_x] and
    // a[max_x]
    quickHull(a, n, a[min_x], a[max_x], 1);
  
    // Recursively find convex hull points on
    // other side of line joining a[min_x] and
    // a[max_x]
    quickHull(a, n, a[min_x], a[max_x], -1);
  
    cout << "The points in Convex Hull are:\n";
    while (!hull.empty())
    {
        cout << "(" <<( *hull.begin()).first << ", "
             << (*hull.begin()).second << ") ";
        hull.erase(hull.begin());
    }
}
  
// Driver code
int main()
{
    iPair a[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4},
               {0, 0}, {1, 2}, {3, 1}, {3, 3}};
    int n = sizeof(a)/sizeof(a[0]);
    printHull(a, n);
    return 0;
}

输入 :

The points in Convex Hull are:
(0, 0) (0, 3) (3, 1) (4, 4) 

时间复杂度:分析类似于快速排序。平均而言,我们得到的时间复杂度为O(n Log n),但在最坏的情况下,它可能变为O(n 2 )