POJ1584:A Round Peg in a Ground Hole(叉积,凸包)

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

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.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

MYCode:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define inf 100000000
using namespace std;
#define MAX 1010
struct node
{
    double x,y;
}a[MAX];
node tp[MAX];
node center;
double r;
node p;
double cw(node a,node b,node c)
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double dist(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double dist(node a,node b,node c)
{
    double d=dist(a,b);
    double res=cw(a,b,c);
    return fabs(res/d);
}
bool cmp(node a,node b)
{
    double ans=cw(p,a,b);
    if(ans!=0)return cw(p,a,b)>0;
    else return dist(p,a)>dist(p,b);//note
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n<3)
        break;
        scanf("%lf%lf%lf",&r,&center.x,&center.y);
        int i;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
        }
        bool flag=true;
        double dirt=0;
        for(i=0;i<n-1;i++)
        {
            if(cw(a[i],a[i+1],a[(i+2)%n])!=0)
            {
                if(dirt==0)
                dirt=cw(a[i],a[i+1],a[(i+2)%n]);
                else if(cw(a[i],a[i+1],a[(i+2)%n])>0&&dirt<0
                ||cw(a[i],a[i+1],a[(i+2)%n])<0&&dirt>0)
                {
                    flag=false;
                    break;
                }
            }
        }
        if(flag==false)
        {
            printf("HOLE IS ILL-FORMED\n");
            continue;
        }
        for(i=0;i<n;i++)
        {
            double d=dist(a[i],a[(i+1)%n],center);
            double res=cw(a[i],a[(i+1)%n],center);
            if(d<r||res<0&&dirt>0
            ||res>0&&dirt<0||res==0)
            {
                flag=false;
                break;
            }
        }
        if(!flag)
        {
            printf("PEG WILL NOT FIT\n");
        }
        else
        printf("PEG WILL FIT\n");
    }
}

//

首先要判断多边形是否凸包.

由于题目是按照顺时针或逆时针方向给出所有点,所以不需要对所有点进行排序

使用叉积判断凸包

允许多点共线情况的存在

陷阱是:

圆心可能不在多边形内部

要求出圆心到多边形各边的距离,所有边的距离都要大于半径

求点到线段的距离的方法:利用叉积

 

posted @ 2012-11-29 15:12  java程序员-c  阅读(193)  评论(0编辑  收藏  举报