POJ 1228 Grandpa's Estate (凸包)

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<stack>
#include<climits>
using namespace std;

double MAX(double a,double b)
{
    if(a<b)return b;
    else return a;
}
double MIN(double a,double b)
{
    if(a<b)return a;
    else return b;
}
//点结构体
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};

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

bool operator < (const Point& a,const Point& b){
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}

const double eps=1e-10;
//精度判断 z 是否大于小于 0
int dcmp(double x){
    if(fabs(x)<eps)return 0;
    else return x<0?-1:1;
}

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 Length(Vector A){return sqrt(Dot(A,A));}
//叉积:A x B
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
//叉积:ab x ac
double Cross2(Point a,Point b,Point c)
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
//两直线交点
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;
}
//点P到直线AB的距离
double DistanceToLine(Point P,Point A,Point B){
    Vector v1=B-A,v2=P-A;
    return fabs(Cross(v1,v2)/Length(v1));
}

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

//Point p[1010],point[1010];
const double PI=3.141592654;
Point origin;
//两点距离
double dis2(Point a,Point b)
{
    return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
//叉积:ac x ab
double crossP(Point a,Point b,Point c)
{
    return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
//极角排序
int cmp(Point a,Point b)
{
    int re=crossP(origin,a,b);
    if(re==0)
        return dis2(a,origin)<dis2(b,origin);
    return re>0;
}
//Graham-Scan
//int graham(PT pt[],PT stk[],int n);
//pt[]是所有点;stk[]返回凸包上的点,逆时针;n是所有点的总数;
//返回凸包上点的个数。
//凸包算法:适用极角排序( pt[]:原来点的集合;stk[]:凸包顶点集合;)
int graham(Point pt[],Point stk[],int n)
{
    int t,tt=0;
    for(t=1;t<n;t++)
    {
        if(pt[t].y<pt[tt].y || (pt[t].y==pt[tt].y && pt[t].x<pt[tt].x))
            tt=t;
    }//find the lowest & leftest point
    swap(pt[0],pt[tt]);
    origin=pt[0];
    sort(pt+1,pt+n,cmp);
    for(t=0;t<=1;t++)
        stk[t]=pt[t];
    int ps=1;
    for(t=2;t<n;t++)
    {
        while(crossP(stk[ps-1],stk[ps],pt[t])<=0)
            ps--;
        stk[++ps]=pt[t];
    }
    return ps+1;
}
//凸包算法:适用水平排序( p[]:原来点的集合;ch[]:凸包顶点集合;)
int ConvexHull(Point* p,int n,Point* ch)
{
    sort(p,p+n);
    int m=0;
    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;
}
//求多边形的有向面积,结果绝对值后就是多边形的面积
//三角形任意两向量的叉积的绝对值==三角形的面积的2倍
//两个向量v和w的叉积==v和w组成的三角形的有向面积的2倍
double CouvexPolygonArea(Point* p,int n)
{
    double area=0;
    for(int i=0;i<n-1;i++)
        area+=Cross(p[i]-p[0],p[i+1]-p[0]);
    return area/2;
}
//判断三点共线
int three_pointline(Point a,Point b,Point c)
{
    Vector ab=b-a;
    Vector ac=c-a;
    if(dcmp((ab.x*ac.y)-(ab.y*ac.x))==0)return 1;
    else return 0;

}

int main()
{
    int t;
    //freopen("F.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        Point p[1010],point[1010];
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        int num=ConvexHull(p,n,point);
        /*for(int i=0;i<num;i++)
        {
            printf("(%.0lf, %.0lf)\n",point[i].x,point[i].y);
        };*/
        if(num==2)
        {
            printf("NO\n");
            continue;
        }
        int flag=0;
        for(int i=0;i<num;i++)
        {
                flag=0;
                for(int k=0;k<n;k++)
                {
                   if((p[k].x==point[i%num].x&&p[k].y==point[i%num].y)||(p[k].x==point[(i+1)%num].x&&p[k].y==point[(i+1)%num].y))continue;
                   if(three_pointline(point[i%num],p[k],point[(i+1)%num]))
                   {
                       flag=1;
                       /*printf("(%.0lf,%.0lf)\n",point[i%num].x,point[i%num].y);
                       printf("(%.0lf,%.0lf)\n",p[k].x,p[k].y);
                       printf("(%.0lf,%.0lf)\n\n",point[(i+1)%num].x,point[(i+1)%num].y);*/
                       break;
                   }
                }
                if(flag==0)break;
        }
        if(flag==1)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值