[BZOJ2829]信用卡凸包(凸包)

题目描述

传送门

题解

和Wall那道题有点像,先求一个凸包然后加上一个圆周
注意输入的是“圆滑处理前”的a和b,需要自己砍掉半径

代码

#include
#include
#include
#include
#include
using namespace std;
#define N 400005

const double pi=acos(-1.0);
const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    bool operator < (const Vector &a) const
    {
        return xtypedef Vector Point;
Vector operator + (Vector a,Vector b) {return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b) {return Vector(a.x-b.x,a.y-b.y);}

int n,m,top;
double a,b,r,x,y,rad,ans;
Vector v;
Point P,Q,p[N],stack[N];

double Dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}
double Len(Vector a)
{
    return sqrt(Dot(a,a));
}
double Cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}
Vector rotate(Vector a,double rad)
{
    return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
void graham()
{
    sort(p+1,p+n+1);
    top=0;
    for (int i=1;i<=n;++i)
    {
        while (top>1&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
            --top;
        stack[++top]=p[i];
    }
    int k=top;
    for (int i=n-1;i>=1;--i)
    {
        while (top>k&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
            --top;
        stack[++top]=p[i];
    }
    if (n>1) --top;
}

int main()
{
    scanf("%d",&m);
    scanf("%lf%lf%lf",&a,&b,&r);a-=2*r,b-=2*r;
    for (int i=1;i<=m;++i)
    {
        scanf("%lf%lf%lf",&x,&y,&rad);
        P=Point(x,y);
        Q=Point(x-b/2,y+a/2);v=rotate(Q-P,rad);p[++n]=P+v;
        Q=Point(x+b/2,y+a/2);v=rotate(Q-P,rad);p[++n]=P+v;
        Q=Point(x-b/2,y-a/2);v=rotate(Q-P,rad);p[++n]=P+v;
        Q=Point(x+b/2,y-a/2);v=rotate(Q-P,rad);p[++n]=P+v;
    }
    graham();
    for (int i=1;i<=top;++i)
        ans+=Len(stack[i%top+1]-stack[(i+1)%top+1]);
    ans+=2*pi*r;
    printf("%.2lf\n",ans);
}

你可能感兴趣的:(题解,计算几何)