算法学习: 凸包

 


 

前置知识:
计算几何基础

 


 

凸包

vector<P> convex(vector<P> l)
{
    vector<P> ans, s;
    P tmp(lim, lim);
    int pos = 0;
    for (int i = 0; i < l.size(); i++)
        if (l[i] < tmp)
            tmp = l[i], pos = i;
    for (int i = pos, cnt_ = 0; cnt_ < l.size(); cnt_++, i = (i + 1) % l.size())
    {
        while (s.size() >= 2 && sgn(cross(s[s.size() - 2], s[s.size() - 1], l[i])) <= 0)
            s.pop_back();
        s.push_back(l[i]);
        pos = i;
    }
    ans = s; s.clear();
    for (int i = pos, cnt_ = 0; cnt_ < l.size(); cnt_++, i = (i - 1) % l.size())
    {
        while (s.size() >= 2 && sgn(cross(s[s.size() - 2], s[s.size() - 1], l[i])) <= 0)
            s.pop_back();
        s.push_back(l[i]);
        pos = i;
    }
    for (int i = 1; i + 1 < s.size(); i++)
        ans.push_back(s[i]);
    return ans;
}
找凸包

 


 

posted @ 2019-07-30 14:15  rentu  阅读(237)  评论(0编辑  收藏  举报