A Round Peg in a Ground Hole(POJ1584点在多边形内+是否为凸多边形)

题意:顺时针或逆时针给你一些点判断是否为凸多边形,如果不是输出HOLE IS ILL-FORMED

如果是在判断圆是否在多边形内

思路:圆在多边形内,判断原点到每个点的距离如果有一条边小于半径那就不在多边形内

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
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) ;}
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 ;}
const double eps = 1e-8;
int dcmp(double x)
{
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}
bool operator == (Point a,Point b)
{
    return dcmp(a.x-b.x) == 0&& dcmp(a.y-b.y) == 0;
}
bool operator < (Point a,Point b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

bool Onsegment(Point p,Point a,Point b)
{
    return dcmp(Cross(b-a,p-a)) == 0 && dcmp(Dot(b-p,a-p)) < 0 || (p == a) || (p == b);
}

bool OnLine(Point p,Point a,Point b)
{
    return fabs(Cross(p-a,a-b)) / Length(b-a);
}

bool Segmentsection(Point a,Point b,Point c,Point d)
{
    double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c);
    if(dcmp(d1)*dcmp(d2) < 0 && dcmp(d3)*dcmp(d4) < 0) return true;
    else if(dcmp(d1) == 0 && Onsegment(c,a,b) ) return true;
    else if(dcmp(d2) == 0 && Onsegment(d,a,b) ) return true;
    else if(dcmp(d3) == 0 && Onsegment(a,c,d) ) return true;
    else if(dcmp(d4) == 0 && Onsegment(b,c,d) ) return true;
    else return false;
}


Point Segment(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 Max(double a,double b)
{
    return a > b ? a : b;
}

double PointtoLine(Point p,Point a,Point b)
{
    return fabs(Cross(p-a,p-b)) / Length(b-a);
}
struct Line
{
    Point s,e;
    Line(Point s = 0,Point e = 0) :s(s),e(e){}
};

bool PointInpoly(Point p,Point poly[],int n)
{
    int w = 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) w++;
        if(k < 0 && d2 <= 0 && d1 > 0) w--;
    }
    if(w != 0) return 1;
    else return 0;
}
Point poly[1005];
int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        if(n < 3)
        {
            break;
        }
        double r,x,y;
        scanf("%lf%lf%lf",&r,&x,&y);
        for(int i = 0; i < n; i++)
        {
            scanf("%lf%lf",&poly[i].x,&poly[i].y);
        }
        bool flag1 = true,flag2 = true,flag = true;
        for(int i = 0; i < n; i++)
        {
            if(Cross(poly[(i+1)%n]-poly[i],poly[(i+2)%n]-poly[i]) < 0)
            {
                flag1 = false;
            }
            if(Cross(poly[(i+1)%n]-poly[i],poly[(i+2)%n]-poly[i]) > 0)
            {
                flag2 = false;
            }
        }
        if(!flag1 && !flag2)
        {
            printf("HOLE IS ILL-FORMED\n");
        }
        else
        {
            for(int i = 0; i < n; i++)
            {
                if(dcmp(PointtoLine(Point(x,y) ,poly[i],poly[(i+1)%n]) - r) < 0)
                {
                    flag = false;break;
                }
            }

            if(!flag || !PointInpoly(Point(x,y) , poly,n) )
                printf("PEG WILL NOT FIT\n");
            else
                printf("PEG WILL FIT\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值