POJ 1584 A Round Peg in a Ground Hole (凸包的判断+点在凸包+圆在凸包内)

大意:给定多边形的点和圆的半径长,和原点。进行凸包的判断+点在凸包+圆在凸包内的判断。

#include<map>
#include<cmath>
#include<queue>
#include<cmath>
#include<cstdio>
#include<stack>
#include<iostream>
#include<cstring>
#include<algorithm>
#define inf 999999999.9
#define eps 1e-8
#define ls l,mid,rt<<1
#define rs mid+1,rt,rt<<1|1
#define LL __int64
#define _sign(x) ((x)>eps?1:( (x)<-eps?2:0 ) )
using namespace std;

struct point{
    double x,y;
};
struct Circle{
    double r;
    point poi;
}c;

struct Line{
    double a,b,c;
};

double xmult(point p1,point p2,point p){
    return (p1.x - p.x)*(p2.y-p.y) - (p2.x - p.x)*(p1.y - p.y);
}

int checkvex(int n,point *p){//多边形的判断
    int s[3]={1,1,1};
    for(int i = 0;i < n&&s[1]|s[2];++ i)
        s[_sign(xmult(p[(i+1)%n],p[(i+2)%n],p[i]))] = 0;
    return s[1] | s[2];

}

bool insideConvex(point q,int n,point *p){//点在多边形内的判断
    int s[3] ={1,1,1};
    for(int i = 0;i < n &&s[1] | s[2];++ i )
        s[_sign(xmult(p[(i+1)%n ],q,p[i]))] = 0;
    return s[1]|s[2];
}

Line towpoLine(point p1,point p2){ //两点连城的线
    Line l;
    l.a = p1.y - p2.y;
    l.b = p2.x - p1.x;
    l.c = p1.x*p2.y - p2.x*p1.y;
    return l;
}

double potoLine(point p,Line l){//点到直线的距离
    return fabs(l.a*p.x+l.b*p.y+l.c)/sqrt(l.a*l.a+l.b*l.b);
}
point q[10010];
int main(){
    int n,i,j,k;
    while(~scanf("%d",&n),n>2){
        scanf("%lf%lf%lf",&c.r,&c.poi.x,&c.poi.y);
        for(i = 0;i < n;++ i)
            scanf("%lf%lf",&q[i].x,&q[i].y);

        if(checkvex(n,q)){//判断在多边形上
            if(insideConvex(c.poi,n,q)){//圆心在多边形内
                for(i = 0;i < n;++ i){
                    Line l = towpoLine(q[i],q[(i+1)%n]);
                    if(potoLine(c.poi,l) - c.r <0 ){//不知道为什么< eps就错
                        printf("PEG WILL NOT FIT\n");
                        break;
                    }
                }
                if(i == n)
                    printf("PEG WILL FIT\n");
            }
            else
                printf("PEG WILL NOT FIT\n");
        }
        else
            printf("HOLE IS ILL-FORMED\n");
    }
    return 0;
}

你可能感兴趣的:(poj,计算几何)