poj 2164 (卷包裹算法)

       取了个数组名_end[]  ,Re了一天, 都要哭了。。。。。_end[]  不能用。  

 view cod

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<ctime>
#include<stdlib.h>
using namespace std;
const int mod=99999997;
const int mmax=200010;
const double eps=1e-5;
const double pi=acos(-1.0);
const int inf=0x3fffffff;


struct Point
{
    double x,y;
    Point (double x=0.0,double y=0.0):x(x),y(y) {}
    void read()
    {
        scanf("%lf %lf",&x,&y);
    }
    void pri()
    {
        printf("%.4lf %.4lf\n",x,y);
    }
};
typedef Point Vector;
Vector operator + (Vector A,Vector B)
{
    return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,double p)
{
    return Vector(A.x*p,A.y*p);
}
Vector operator / (Vector A,double p)
{
    return Vector(A.x/p,A.y/p);
}
int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    return x<0?-1:1;
}
bool operator < (const Point &a,const Point &b)
{
    return sgn(a.x-b.x)<0 || ( sgn(a.x-b.x)==0&& sgn(a.y<b.y)<0 );
}
bool operator == (const Point &a,const Point &b)
{
    return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;
}
double Dot(Vector A,Vector B)
{
    return A.x*B.x+A.y*B.y;
}
double Length(Vector A)
{
    return sqrt(Dot(A,A));
}
double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
double Angle(Vector A,Vector B)
{
    return atan2(B.y,B.x)-atan2(A.y,A.x)-2.0*pi;
}
Point GetLineIntersection(Point P,Point Q,Vector v,Vector w)
{
    Vector u=P-Q;
    double t=Cross(w,u) / Cross(v,w);
    return P+v*t;
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
    double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    return sgn(c1)*sgn(c2)<0 && sgn(c3)*sgn(c4)<0;
}
bool OnSegment(Point p,Point a1,Point a2)
{
    return sgn(Cross(a1-p,a2-p))==0 && sgn(Dot(a1-p,a2-p))<0;
}
Point p[110];
Point Poly[6000];
Point __end[220];
int main()
{
    int n;
    //freopen("in.txt","r",stdin);
    while(cin>>n&&n)
    {
        for(int i=0;i<n;i++)
            p[i].read();
        int start=0;
        for(int i=1;i<n;i++)
            if(p[i]<p[start])
                start=i;
        int nowid=0;
        Poly[0]=p[start];
        Vector V(-100.0,0);
        while(++nowid)
        {
            int cc=0;
            for(int i=0;i<n;i++)
            {
                if(p[i]==Poly[nowid-1])
                {
                    __end[cc++]=p[(i+1)%n];
                    __end[cc++]=p[(i-1+n)%n];
                }
                if(OnSegment(Poly[nowid-1],p[i],p[(i+1)%n]))
                {
                    __end[cc++]=p[i];
                    __end[cc++]=p[(i+1)%n];
                }

            }
            Point _next=__end[0];
            for(int i=1;i<cc;i++)
            {
                double ang1=Angle(V,__end[i]-Poly[nowid-1]);
                while(sgn(ang1)<=0.0)
                    ang1+=2*pi;
                double ang2=Angle(V,_next-Poly[nowid-1]);
                while(sgn(ang2)<=0.0)
                    ang2+=2*pi;
                if(ang1<ang2)
                    _next=__end[i];
            }
            for(int i=0;i<n;i++)
            {
                if(SegmentProperIntersection(Poly[nowid-1],_next,p[i],p[(i+1)%n]))
                {
                    _next=GetLineIntersection(Poly[nowid-1],p[i],
                                                _next-Poly[nowid-1],p[(i+1)%n]-p[i]);
                }
            }
            Poly[nowid]=_next;
            V=Poly[nowid-1]-Poly[nowid];
            if(Poly[nowid]==Poly[0])
                break;
        }
        cout<<nowid<<endl;
        for(int i=0;i<nowid;i++)
            Poly[i].pri();
    }
    return 0;
}

e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值