poj 1113 Wall 凸包

题目地址:poj1113

如果求出凸包,就是凸包的周长加上一整个圆的周长,证明是简单的~

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
const  double eps=1e-10;
const double PI=acos(-1.0);

using namespace std;


struct Point{
    double x;
    double y;
    Point(double x=0,double y=0):x(x),y(y){}
    void operator<<(Point &A) {cout<<A.x<<' '<<A.y<<endl;}
};

int dcmp(double x)  {return (x>eps)-(x<-eps); }
int sgn(double x)  {return (x>eps)-(x<-eps); }
typedef  Point  Vector;

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); }

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);}



ostream &operator<<(ostream & out,Point & P) { out<<P.x<<' '<<P.y<<endl; return out;}
//
bool  operator< (const Point &A,const Point &B) { return dcmp(A.x-B.x)<0||(dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)<0); }

bool  operator== ( const Point &A,const Point &B) { return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;}


double  Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}

double  Cross(Vector A,Vector B)  {return A.x*B.y-B.x*A.y; }

double  Length(Vector A)  { return sqrt(Dot(A, A));}


double  Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}

double  Area2(Point A,Point B,Point C ) {return Cross(B-A, C-A);}

Vector Rotate(Vector A,double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}
Vector Normal(Vector A) {double L=Length(A);return Vector(-A.y/L,A.x/L);}

Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
    Vector u=P-Q;
    double t=Cross(w, u)/Cross(v,w);
    return P+v*t;
    
}

double DistanceToLine(Point P,Point A,Point B)
{
    Vector v1=P-A; Vector v2=B-A;
    return fabs(Cross(v1,v2))/Length(v2);
    
}

double DistanceToSegment(Point P,Point A,Point B)
{
    if(A==B)  return Length(P-A);
    
    Vector v1=B-A;
    Vector v2=P-A;
    Vector v3=P-B;
    
    if(dcmp(Dot(v1,v2))==-1)    return  Length(v2);
    else if(Dot(v1,v3)>0)    return Length(v3);
    
    else return DistanceToLine(P, A, B);
    
}

Point GetLineProjection(Point P,Point A,Point B)
{
    Vector v=B-A;
    Vector v1=P-A;
    double t=Dot(v,v1)/Dot(v,v);
    
    return  A+v*t;
}

bool  SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    double c1=Cross(b1-a1, a2-a1);
    double c2=Cross(b2-a1, a2-a1);
    double c3=Cross(a1-b1, b2-b1);
    double c4=Cross(a2-b1, b2-b1);
    
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0 ;
    
}

bool  OnSegment(Point P,Point A,Point B)
{
    return dcmp(Cross(P-A, P-B))==0&&dcmp(Dot(P-A,P-B))<=0;
}

double PolygonArea(Point *p,int n)
{
    double area=0;
    
    for(int i=1;i<n-1;i++)
    {
        area+=Cross(p[i]-p[0], p[i+1]-p[0]);
        
    }
    return area/2;
    
}

Point  read_point()
{
    Point P;
    scanf("%lf%lf",&P.x,&P.y);
    return  P;
}

// ---------------与圆有关的--------

struct Circle
{
    Point c;
    double r;
    
    Circle(Point c=Point(0,0),double r=0):c(c),r(r) {}
    
    Point point(double a)
    {
        return Point(c.x+r*cos(a),c.y+r*sin(a));
    }
    
    
};

struct  Line
{
    Point p;
    Vector v;
    Line(Point p=Point(0,0),Vector v=Vector(0,1)):p(p),v(v) {}
    
    Point point(double t)
    {
        return Point(p+v*t);
    }
    
};

int getLineCircleIntersection(Line L,Circle C,double &t1,double &t2,vector<Point> &sol)
{
    double a=L.v.x;
    double b=L.p.x-C.c.x;
    double c=L.v.y;
    double d=L.p.y-C.c.y;
    
    double e=a*a+c*c;
    double f=2*(a*b+c*d);
    double g=b*b+d*d-C.r*C.r;
    
    double delta=f*f-4*e*g;
    
    if(dcmp(delta)<0) return 0;
    
    if(dcmp(delta)==0)
    {
        t1=t2=-f/(2*e);
        sol.push_back(L.point(t1));
        return 1;
    }
    
    else
    {
        t1=(-f-sqrt(delta))/(2*e);
        t2=(-f+sqrt(delta))/(2*e);
        
        sol.push_back(L.point(t1));
        sol.push_back(L.point(t2));
        
        return 2;
    }
    
}

// 向量极角公式

double angle(Vector v)  {return atan2(v.y,v.x);}

int getCircleCircleIntersection(Circle C1,Circle C2,vector<Point> &sol)
{
    double d=Length(C1.c-C2.c);
    
    if(dcmp(d)==0)
    {
        if(dcmp(C1.r-C2.r)==0)  return -1;  // 重合
        else return 0;    //  内含  0 个公共点
    }
    
    if(dcmp(C1.r+C2.r-d)<0)  return 0;  // 外离
    if(dcmp(fabs(C1.r-C2.r)-d)>0)  return 0;  // 内含
    
    double a=angle(C2.c-C1.c);
    double da=acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*C1.r*d));
    
    Point p1=C1.point(a-da);
    Point p2=C1.point(a+da);
    
    sol.push_back(p1);
    
    if(p1==p2)  return 1; // 相切
    else
    {
        sol.push_back(p2);
        return 2;
    }
}


//  求点到圆的切线

int getTangents(Point p,Circle C,Vector *v)
{
    Vector u=C.c-p;
    
    double dist=Length(u);
    
    if(dcmp(dist-C.r)<0)  return 0;
    
    else if(dcmp(dist-C.r)==0)
    {
        v[0]=Rotate(u,PI/2);
        return 1;
    }
    
    else
    {
        
        double ang=asin(C.r/dist);
        v[0]=Rotate(u,-ang);
        v[1]=Rotate(u,+ang);
        return 2;
    }
    
}

//  求两圆公切线


int getTangents(Circle A,Circle B,Point *a,Point *b)
{
    int cnt=0;
    
    if(A.r<B.r)
    {
        swap(A,B); swap(a, b);  //  有时需标记
    }
    
    double d=Length(A.c-B.c);
    
    double rdiff=A.r-B.r;
    double rsum=A.r+B.r;
    
    if(dcmp(d-rdiff)<0)  return 0;   // 内含
    
    double base=angle(B.c-A.c);
    
    if(dcmp(d)==0&&dcmp(rdiff)==0)   return -1 ;  // 重合 无穷多条切线
    
    if(dcmp(d-rdiff)==0)             // 内切   外公切线
    {
        a[cnt]=A.point(base);
        b[cnt]=B.point(base);
        cnt++;
        return 1;
    }
    
    // 有外公切线的情形
    
    double ang=acos(rdiff/d);
    a[cnt]=A.point(base+ang);
    b[cnt]=B.point(base+ang);
    cnt++;
    a[cnt]=A.point(base-ang);
    b[cnt]=B.point(base-ang);
    cnt++;
    
    if(dcmp(d-rsum)==0)     // 外切 有内公切线
    {
        a[cnt]=A.point(base);
        b[cnt]=B.point(base+PI);
        cnt++;
    }
    
    else  if(dcmp(d-rsum)>0)   // 外离   又有两条外公切线
    {
        double  ang_in=acos(rsum/d);
        a[cnt]=A.point(base+ang_in);
        b[cnt]=B.point(base+ang_in+PI);
        cnt++;
        a[cnt]=A.point(base-ang_in);
        b[cnt]=B.point(base-ang_in+PI);
        cnt++;
    }
    
    return cnt;
}


//  几何算法模板

int  isPointInPolygon(Point p,Point * poly,int n)
{
    int wn=0;
    for(int i=0;i<n;i++)
    {
        if(OnSegment(p, poly[i], poly[(i+1)%n]))  return -1;
        int k=dcmp(Cross(poly[(i+1)%n]-poly[i], p-poly[i]));
        int d1=dcmp(poly[i].y-p.y);
        int d2=dcmp(poly[(i+1)%n].y-p.y);
        
        if(k>0&&d1<=0&&d2>0) wn++;
        if(k<0&&d2<=0&&d1>0) wn--;
        
    }
    
    if(wn!=0)  return 1;
    else   return 0;
    
}

//  Andrew 算法求凸包

//int ConvexHull(Point *p,int n,Point *ch)
//{
//    int m=0;
//    sort(p,p+n);
//    n=unique(p, p+n)-p;
//    
//    for(int i=0;i<n;i++)
//    {
//        while(m>1&&dcmp(Cross(p[m-1]-p[m-2],p[i]-p[m-2]))<=0)  m--;
//        ch[m++]=p[i];
//    }
//    
//    int k=m;
//    
//    for(int i=n-2;i>=0;i--)
//    {
//       while(m>k&&dcmp(Cross(p[m-1]-p[m-2],p[i]-p[m-2]))<=0)   m--;
//        ch[m++]=p[i];
//    }
//    
//    if(n>1) m--;
//    
//    return m;
//    
//     
//}

vector<Point> ConvexHull(vector<Point> p) {
   
    sort(p.begin(), p.end());
    p.erase(unique(p.begin(), p.end()), p.end());
    
    int n = p.size();
    int m = 0;
    vector<Point> ch(n+1);
    for(int i = 0; i < n; i++) {
        while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
        ch[m++] = p[i];
    }
    int k = m;
    for(int i = n-2; i >= 0; i--) {
        while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
        ch[m++] = p[i];
    }
    if(n > 1) m--;
    ch.resize(m);
    return ch;
}
//Point p[1010];
Point ch[1010];

int main()
{
    int n;
    double L;
    cin>>n>>L;
    vector<Point> p;
    Point temp;
    for(int i=0;i<n;i++)
    {
        temp=read_point();
        p.push_back(temp);
    }
    
    double ans=PI*L*2;

   
    vector<Point> ch=ConvexHull(p);
    
    for(int i=0;i<ch.size();i++)
        ans+=Length(ch[(i+1)%ch.size()]-ch[i]);
   
    printf("%.0f\n",ans);
    
    
}


对于凸包 比较喜欢这个写法 虽然是一样的

int ConvexHull(Point *p,int n,Point *ch)
{
    int m=0;
    sort(p,p+n);
    
    n=unique(p, p+n)-p;
    
    for(int i=0;i<n;i++)
    {
        while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)  m--;
        ch[m++]=p[i];
    }
    
    int k=m;
    
    for(int i=n-2;i>=0;i--)
    {
       while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)   m--;
        ch[m++]=p[i];
    }
    
    if(n>1) m--;
    
    return m;
    
     
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值