【问题标题】:A Convex Hull function凸包函数
【发布时间】:2018-07-28 04:50:31
【问题描述】:

我是 StackOverflow 的新手,这是我在这里的第一个问题。

我在Convex Hull上解决了一些问题,在Codechef上看到vjudges的答案提交时,我发现他们反复使用以下函数来找出一组点的凸包。

int n = (int)p.size();
if (n <= 1)
    return p;
sort(p.begin(), p.end());
int cnt = 0;
vector<Pint> q(n * 2);
for (int i = 0; i < n; q[cnt++] = p[i++])
    for (; cnt >= 2 && !cw(q[cnt - 2], q[cnt - 1], p[i]); --cnt)
        ;
for (int i = n - 2, t = cnt; i >= 0; q[cnt++] = p[i--])
    for (; cnt > t && !cw(q[cnt - 2], q[cnt - 1], p[i]); --cnt)
        ;
q.resize(cnt - 1 - (q[0] == q[1]));
return q;

有人可以解释一下这个函数背后的逻辑是什么,它与 Jarvis 或 Graham 的方法有什么不同吗?

【问题讨论】:

    标签: convex-hull


    【解决方案1】:

    上面的代码使用了与 Grahm 或 Jarvis 算法略有不同的算法。 它通过以相反的顺序应用相同的算法首先找到上半部分船体,然后找到下半部分船体。最后,它结合了两者。

    输入:平面上的一组点 P。 输出:一个包含 CH(P) 的顶点的列表,按顺时针顺序排列。

     1. Sort the points by x-coordinate, resulting in a sequence p1,p2.....pn
     2. Put the points p1 and p2 in a list Lupper, with p1 as the first point
     3. for i from 3 to n
     4.     do Append pi to Lupper
     5.          while length(Lupper)>2 and last three points in Lupper make left turn
     6.               do delete middle of last three points from Lupper
    

    现在将点 pn,pn-1 放入列表 Llower 中,pn 作为第一个点

     7. for i from n-2 to 1
     8.     do Append pi to Llower
     9.          while length(Llower)>2 and last three points in Llower make left turn
     10.               do delete middle of last three points from Llower
     11. Remove the first and last point from Llower to avoid duplication of the points where upper and lower hull meet.
     12. Append Llower to Lupper and call the resulting list L
     13. return L
    

    例如:- 点 = (0,0),(1,1),(2,5),(8,9),(10,0),(1,-1),(2,- 5),(8,-9)

    Lupper

    Llower

    Final Hull

    算法来源:Mark de Berg 的 Computational Geometry Algorithms and Applications

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 2012-08-04
      • 2017-10-10
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2018-12-29
      • 2016-11-19
      • 2013-10-09
      相关资源
      最近更新 更多