【问题标题】:C++: Why does this basic 2D Quickhull algorithm return points in the incorrect order?C++:为什么这个基本的 2D Quickhull 算法以错误的顺序返回点?
【发布时间】:2020-03-10 00:00:37
【问题描述】:

以下是用于凸壳的 2D Quickhull 算法的 C++ 实现,取自 geeksforgeeks.org:https://www.geeksforgeeks.org/quickhull-algorithm-convex-hull/

#include<bits/stdc++.h> 
using namespace std; 

// iPair is integer pairs 
#define iPair pair<int, int> 

// Stores the result (points of convex hull) 
set<iPair> 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<n; i++) 
    { 
        int temp = lineDist(p1, p2, a[i]); 
        if (findSide(p1, p2, a[i]) == side && temp > 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<n; i++) 
    { 
        if (a[i].first < a[min_x].first) 
            min_x = i; 
        if (a[i].first > 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; 
} 

在未修改的情况下使用给定的一组点运行程序会按该顺序返回 (0, 0) (0, 3) (3, 1) (4, 4)。根据输出绘制结果如下所示:

请注意,通过中间的线实际上并没有击中这三个点,只是我用来绘制它们的程序在它们之间画了一条线。如果我没记错的话,输出的正确顺序应该更符合 (0, 0) (0, 3) (4, 4) (3, 1)。按这个顺序绘制它们会给你一个真正的凸包。

正如我所说,以下是 geeksforgeeks 实现的直接复制粘贴。我正在处理的项目需要具有显着更多点的凸包,这些点是从文件输入的,并且我已将上述内容调整为以该格式工作。也就是说,基本算法似乎存在一个基本问题,除非我误解了某些东西。

任何帮助将不胜感激!

【问题讨论】:

  • 不相关。小心#include&lt;bits/stdc++.h&gt; using namespace std;。它将您的代码变成标识符碰撞雷区。如果您在教程中发现这样的代码,请认真考虑寻找其他教程。

标签: c++ algorithm 2d convex


【解决方案1】:

该算法仅当线外没有点时(即线是凸包的边缘之一)才将 L 的端点添加到凸包。所以点不是按顺序添加的。此外,我们检查 quickHull 函数开始时的所有点。所以这段代码不是最优的。你可以找到更好的实现here。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    相关资源
    最近更新 更多