POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,判断圆是否在凸多边形内)

来源:互联网 发布:人肉软件 app 编辑:程序博客网 时间:2024/05/21 15:02

Description

The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to one another using a wooden peg that fits into pre-cut holes in each piece to be attached. The pegs have a circular cross-section and so are intended to fit inside a round hole. 
A recent factory run of computer desks were flawed when an automatic grinding machine was mis-programmed. The result is an irregularly shaped hole in one piece that, instead of the expected circular shape, is actually an irregular polygon. You need to figure out whether the desks need to be scrapped or if they can be salvaged by filling a part of the hole with a mixture of wood shavings and glue. 
There are two concerns. First, if the hole contains any protrusions (i.e., if there exist any two interior points in the hole that, if connected by a line segment, that segment would cross one or more edges of the hole), then the filled-in-hole would not be structurally sound enough to support the peg under normal stress as the furniture is used. Second, assuming the hole is appropriately shaped, it must be big enough to allow insertion of the peg. Since the hole in this piece of wood must match up with a corresponding hole in other pieces, the precise location where the peg must fit is known. 
Write a program to accept descriptions of pegs and polygonal holes and determine if the hole is ill-formed and, if not, whether the peg will fit at the desired location. Each hole is described as a polygon with vertices (x1, y1), (x2, y2), . . . , (xn, yn). The edges of the polygon are (xi, yi) to (xi+1, yi+1) for i = 1 . . . n − 1 and (xn, yn) to (x1, y1).

Input

Input consists of a series of piece descriptions. Each piece description consists of the following data: 
Line 1 < nVertices > < pegRadius > < pegX > < pegY > 
number of vertices in polygon, n (integer) 
radius of peg (real) 
X and Y position of peg (real) 
n Lines < vertexX > < vertexY > 
On a line for each vertex, listed in order, the X and Y position of vertex The end of input is indicated by a number of polygon vertices less than 3.

Output

For each piece description, print a single line containing the string: 
HOLE IS ILL-FORMED if the hole contains protrusions 
PEG WILL FIT if the hole contains no protrusions and the peg fits in the hole at the indicated position 
PEG WILL NOT FIT if the hole contains no protrusions but the peg will not fit in the hole at the indicated position

Sample Input

5 1.5 1.5 2.01.0 1.02.0 2.01.75 2.01.0 3.00.0 2.05 1.5 1.5 2.01.0 1.02.0 2.01.75 2.51.0 3.00.0 2.01

Sample Output

HOLE IS ILL-FORMEDPEG WILL NOT FIT

题意:让你判断一个多边形是否为凸多边形,如果是,那么再判断给定的圆是否在这个凸多边形内。


判断是否为凸多边形,可以用叉积来判断,如果一个多边形上存在连续的三条边,l1,l2,l3(方向得是一致的,都是顺时针或者都是逆时针),(l1 X l2) *(l2 X l3)的值为负,那么这个多边形就是凹多边形,否则就为凸多边形。(自己画一下图就能看出来了)

判断完如果是凸多边形,就要判断圆是否在凸多边形内。首先判断圆心在不在凸多边形内,可以用圆心和多边形上相邻的两条边叉积的符号来判断,如果两个叉积的符号相同,就说明圆心在这两条线段的同一边,如果符号都相同,那么圆心就在多边形内。

如果圆心在多边形内,就判断圆的半径与圆心到线段的距离关系就可以了。

点到线段的距离可以这样求:

点和线段的两个端点组成一个三角形,面积S = 1/2 * (PA X PB),S = 1/2 * |AB|*h,所以h = (PA X PB) / |AB|


#include<cmath>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const double eps = 1e-8;struct Point{    double x,y;    Point(){}    Point(double _x,double _y)    {        x = _x;y = _y;    }    Point operator -(const Point &b)const    {        return Point(x - b.x,y - b.y);    }    double operator *(const Point &b)const    {        return x*b.x + y*b.y;    }    double operator ^(const Point &b)const    {        return x*b.y - y*b.x;    }};double dis(Point a,Point b){    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}double xmult(Point p0,Point p1,Point p2){    return (p1-p0)^(p2-p0);}bool judge_convex(vector<Point> a){    for(int i=0;i<a.size();i++)    {        int i1 = (i+1)%a.size();        int i2 = (i+2)%a.size();        int i3 = (i+3)%a.size();        if(((a[i1]-a[i])^(a[i2]-a[i1])) * ((a[i2]-a[i1])^(a[i3]-a[i2])) < -eps)            return false;    }    return true;}bool judge_Circle_in_Polygon(Point p,double r,vector<Point> a){    for(int i=0;i<a.size();i++)//判断圆心在不在多边形内    {        int i1 = (i+1)%a.size();        int i2 = (i+2)%a.size();        if(xmult(p,a[i],a[i1])*xmult(p,a[i1],a[i2]) < eps)            return false;    }    for(int i=0;i<a.size();i++)//判断半径与圆心到边的距离的关系    {        int i1 = (i+1)%a.size();        double h = fabs(xmult(p,a[i],a[i1]))/dis(a[i],a[i1]);        if(h - r < -eps)            return false;    }    return true;}vector<Point> v;int main(void){    int n,i,j;    double r,x,y;    while(scanf("%d",&n)==1)    {        if(n < 3)            break;        scanf("%lf%lf%lf",&r,&x,&y);        Point p(x,y);        v.clear();        for(i=0;i<n;i++)        {            scanf("%lf%lf",&x,&y);            v.push_back(Point(x,y));        }        if(!judge_convex(v))            printf("HOLE IS ILL-FORMED\n");        else        {            if(judge_Circle_in_Polygon(p,r,v))                printf("PEG WILL FIT\n");            else                printf("PEG WILL NOT FIT\n");        }    }    return 0;}


阅读全文
0 0
原创粉丝点击