zoukankan      html  css  js  c++  java
  • K

    凸包 + 旋转卡壳 模本

    c++ code:

    #include <bits/stdc++.h>
    
    using namespace std;
    const int N=3e5+7;
    
    struct point{
        double x, y;
        point(){}
        point(double x, double y) : x(x), y(y) {}
        friend point operator + (const point &a, const point &b){
            return point(a.x + b.x, a.y + b.y);
        }
        friend point operator - (const point &a, const point &b){
            return point(a.x - b.x, a.y - b.y);
        }
        friend point operator * (const point &a, const double &b){
            return point(a.x * b, a.y * b);
        }
        friend point operator / (const point &a, const double &b){
            return point(a.x / b, a.y / b);
        }
        friend bool operator == (const point &a, const point &b){
            return a.x == b.x && a.y == b.y;
        }
    };
    double r;
    double dis(point a,point b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
    }
    double cross(point a,point b,point c) //计算叉乘
    {
        return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y);
    }
    double  det(point a, point b)
    {
        return a.x * b.y - a.y * b.x;
    }
    double dot(point a, point b)
    {
        return a.x * b.x + a.y * b.y;
    }
    struct polygon_convex
    {
        vector<point> p;
        polygon_convex(int size = 0){
            p.resize(size);
        }
    };
    bool comp_less(const point &a, const point &b)
    {
        return a.x - b.x < 0 || a.x - b.x == 0 && a.y - b.y < 0;
    }
    polygon_convex convex_hull(vector<point> a)
    {
        polygon_convex res(2 * a.size() + 5);
        sort(a.begin(), a.end(), comp_less);
        a.erase(unique(a.begin(), a.end()), a.end());
        int m = 0;
        for(int i = 0; i < a.size(); i ++){
            while(m > 1 && det(res.p[m - 1] - res.p[m - 2], a[i] - res.p[m - 2]) <= 0) -- m;
            res.p[m ++] = a[i];
        }
        int k = m;
        for(int i = int(a.size()) - 2; i >= 0; i --){
            while(m > k && det(res.p[m - 1] - res.p[m - 2], a[i] - res.p[m - 2]) <= 0) -- m;
            res.p[m ++] = a[i];
        }
        res.p.resize(m);
        if(a.size() > 1) res.p.resize(m - 1);
        return res;
    }
    polygon_convex a;
    point p[N];
    double rotating_caliper(point *p,int n)
    {
         int j=1;
         r=2*r;
         p[n]=p[0];
         for (int i=0;i<n;i++)
         {
             while (cross(p[i],p[i+1],p[j+1])-cross(p[i],p[i+1],p[j])<=0)
                j=(j+1)%n;
            r=min(r,fabs(cross(p[i],p[i+1],p[j]))/dis(p[i],p[i+1]));
        }
        return r;
    }
    int main()
    {
        int n;
        scanf("%d%lf",&n,&r);
        point node;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&node.x,&node.y);
            a.p.push_back(node);
        }
        a=convex_hull(a.p);
        n=a.p.size();
        for(int i=0;i<n;i++)
            p[i]=a.p[i];
        if(n<=2)
            printf("0.000000000000
    ");
        else
            printf("%.12lf
    ",rotating_caliper(p,n));
        return 0;
    }
  • 相关阅读:
    第04组 beta冲刺(1/4)
    2019 SDN上机第5次作业
    SDN课程阅读作业(2)
    第04组 Alpha事后诸葛亮
    C Primer 复习题
    C Primer 编程练习
    C语言I博客作业04
    C语言I博客作业03
    C语言I博客作业02
    Appium + java截图方法
  • 原文地址:https://www.cnblogs.com/lemon-jade/p/8977977.html
Copyright © 2011-2022 走看看