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

A Round Peg in a Ground Hole
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3534   Accepted: 1036

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 (x i+1, y i+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.0
1.0 1.0
2.0 2.0
1.75 2.0
1.0 3.0
0.0 2.0
5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.5
1.0 3.0
0.0 2.0
1

Sample Output

HOLE IS ILL-FORMED
PEG WILL NOT FIT

Source

Mid-Atlantic 2003
题目: http://poj.org/problem?id=1584
分析:这题要求判断一个多边形是否是凸的,如果是凸的就判断一个圆是否在它里面,给出的多边形的点是按顺时针或逆时针的,所以只要顺序枚举,用叉积的方法判断是否每条边相对其前一条边的拐向都一样,(注意添加两个点保证构成一个环,即第0个点等于第n个点,第n+1个点等于第1个点),然后如果是凸多边形的,就判断点是否在多边形内,因为多边形是凸的,这一步也就简单了许多,直接枚举相邻的两条边,两条边之间的点与圆心构成一条直线L,判断这两条边是否在L两侧就行,如果都满足的话,再判断圆心到每条边的距离是否大于半径,详细见代码。。。

一些用到的公式:

    两点式:已知直线上两点(x1,y1),(x2,y2),则直线方程为 

     (y1-y2)*x+(x2-x1)*y+x1*y2-x2*y1=0 

           A=y1-y2  B=x2-x1  C=x1*y2-x2*y1

 已知点(x1,y1),直线Ax+By+C=0,则点到直线距离

     d=|A*x1+B*y1+C|/sqrt(A*A+B*B) 

    点 P(x1,y1)与点Q(x1,y1)的叉积为x1*y1-x2*y1;

    对于有公共端点的线段p0p1和p1p2,通过计算(p2 - p0) × (p1 - p0)的符号便可以确定折线段的拐向

   两线段在直线的两端只要两线段的拐向不一样就行

代码:
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
double x[333],y[333];
double px,py,r,t;
int i,j,k,n;
double crotch(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}
double check(int i,int j,int k)
{
    return crotch(x[j]-x[i],y[j]-y[i],x[k]-x[i],y[k]-y[i]);
}
bool ok()
{
    double a,b,c;
    for(int i=1;i<=n;++i)
    {
        if(crotch(x[i+1]-x[i],y[i+1]-y[i],px-x[i],py-y[i])*
           crotch(x[i-1]-x[i],y[i-1]-y[i],px-x[i],py-y[i])>0)return 0;
        a=y[i]-y[i-1],b=x[i-1]-x[i],c=x[i]*y[i-1]-x[i-1]*y[i];
        if(fabs(a*px+b*py+c)<sqrt(a*a+b*b)*r)return 0;
    }
    return 1;
}
int main()
{
    while(scanf("%d",&n),n>2)
    {
        scanf("%lf%lf%lf",&r,&px,&py);
        for(i=1;i<=n;++i)
            scanf("%lf%lf",&x[i],&y[i]);
        x[0]=x[n],y[0]=y[n];
        x[n+1]=x[1],y[n+1]=y[1];
        t=(check(0,1,2)<0)?-1:1;
        for(i=2;i<=n;++i)
            if(t*check(i-1,i,i+1)<0)break;
        if(i>n)
        {
            if(ok())puts("PEG WILL FIT");
            else puts("PEG WILL NOT FIT");
        }
        else puts("HOLE IS ILL-FORMED");
    }
    return 0;
}


你可能感兴趣的:(poj 1584 A Round Peg in a Ground Hole(判断凸多边形+圆是否在凸多边形内))