【计算几何】【凸包新板子】【模拟】[CERC2016]凸轮廓线 Convex Contour

here

根据输入的字母,在一行中横向放入几何图形,最后求一个凸包周长。要提到的是,这里的几何是有圆形的,并且用之前的凸包模板会存在一个点重合的问题。

这个不会有这样的问题,所以换了新的模板,注意使用就好啦!

这两天要花时间研究一下kuangbin的计算几何模板,至少我要知道该怎么用才可以。

ac code

#include <bits/stdc++.h>
#define pb push_back
#define ld long double

using namespace std;
const ld eps = 1e-16;
const ld pi=acos(-1);
const ld inf=1e9;

inline bool equal(ld x,ld y)
{
    return abs(x-y)<=eps;
}

struct pt
{
    ld x,y;
    pt(ld a=0,ld b=0){x=a,y=b;}
    pt operator+ (const pt&A){return pt(x+A.x,y+A.y);}
    pt operator- (const pt&A){return pt(x-A.x,y-A.y);}
    pt operator* (ld d){return pt(x*d,y*d);}
    pt operator/ (ld d){return pt(x/d,y/d);}
    ld operator* (const pt&A) {return x*A.y-y*A.x;}
    void out(){cout<<"("<<x<<","<<y<<")"<<endl;}
};

struct line
{
    pt A,B;
    line(pt a=pt(),pt b=pt())
    {
        A=a,B=b;
    }
};

inline int cross(pt A,pt B)
{
    ld d=A*B;
    if(equal(d,0))
        return 0;
    return d>0?1:-1;
}

inline ld dis(pt A,pt B)
{
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

inline pt intersection(line a,line b)
{
    pt A=b.B-b.A,B=a.B-a.A,C=b.A-a.A;
    if(cross(A,B)==0)
        return pt(inf,inf);
    ld d=-(B*C)/(B*A);
    return b.A+A*d;
}

inline pt foot(pt A,line a)
{
    return intersection(line(A,A+pt(a.B.y-a.A.y,a.A.x-a.B.x)),a);
}

pt O(0,0);

bool cmp(pt A,pt B)
{
    ld x=atan2(A.y-O.y,A.x-O.x),y=atan2(B.y-O.y,B.x-O.x);
    if(equal(x,y))
        return dis(A,O)<dis(B,O);
    return x<y;
}
vector<pt>convex(vector<pt>P)
{
    int pos=0;
    int length=P.size();
    for(int i=1;i<length;++i)
        if(P[i].x<P[pos].x)
            pos=i;
    swap(P[pos],P[0]);
    O=P[0];
    sort(P.begin()+1,P.end(),cmp);
    vector<pt>ans;
    int now=0;
    for(int i=0;i<length;++i)
    {
        while(now>1&&cross(P[i]-ans[now-2],ans[now-1]-ans[now-2])!=-1)
            ans.pop_back(),--now;
        ans.push_back(P[i]);
        ++now;
    }
    return ans;
}
inline bool cmpLine(line a,line b)
{
    return atan2(a.A.y-a.B.y,a.A.x-a.B.x)<atan2(b.A.y-b.B.y,b.A.x-b.B.x);
}
inline bool onClockwise(line a,line b,line c)//b,c的交点在a顺时针方向
{
    return cross(intersection(b,c)-a.A,a.B-a.A)==1;
}
inline bool isSame(line a,line b)
{
    return cross(a.A-b.B,b.A-b.B)==0;
}
line wait[66666];
vector<line>halfPlane(vector<line>A)
{
    vector<line>ans;
    sort(A.begin(),A.end(),cmpLine);
    int l=1,r=0;
    int length=A.size();
    for(int i=0;i<length;++i)
    {
        while(l<r&&!isSame(A[i],wait[r])&&onClockwise(A[i],wait[r-1],wait[r]))
            --r;
        while(l<r&&!isSame(A[i],wait[l])&&onClockwise(A[i],wait[l],wait[l+1]))
            ++l;
        if(!isSame(A[i],wait[r])||r==0)
            wait[++r]=A[i];
        else if(!onClockwise(wait[r],wait[r-1],A[i]))
            wait[r]=A[i];
    }
    while(l<r&&onClockwise(wait[l],wait[r],wait[r-1]))
        --r;
    while(l<r&&onClockwise(wait[r],wait[l],wait[l+1]))
        ++l;
    for(int i=l;i<=r;++i)
        ans.push_back(wait[i]);
    return ans;
}
inline ld length(vector<pt>P)
{
    ld sum=0;
    int length=P.size();
    for(int i=1;i<length;++i)
        sum+=dis(P[i-1],P[i]);
    sum+=dis(P[P.size()-1],P[0]);
    return sum;
}

const ld num=10000;
void solve()
{
    int n;
    cin>>n;
    vector<pt>P;
    for(int i=1;i<=n;i++)
    {
        char ch;
        cin>>ch;
        ld x=i;
        if(ch=='S')
        {
            P.pb(pt(x,0));
            P.pb(pt(x+1,0));
            P.pb(pt(x+1,1));
            P.pb(pt(x,1));
        }
        else if(ch=='T')
        {
            P.pb(pt(x,0));
            P.pb(pt(x+1,0));
            P.pb(pt(x+0.5,sqrt(3)/2));
        }
        else
        {
            for(ld j=0;j<num;j+=1)
            {
                ld ra=2*pi/num*j;
                P.push_back(pt(x+0.5+cos(ra)/2,0.5+sin(ra)/2));
            }
        }
    }
    ld ans = length(convex(P));
    printf("%.10Lf",ans);
}

signed main()
{
    int T=1;
//    cin>>T
    for(int index=1;index<=T;index++)
    {
        solve();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

while WA er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值