天天看點

poj 1584 A Round Peg in a Ground Hole

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

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

提示

題意:

這題目的意思就是給出以順時針或者逆時針點坐标的方式給出一個多邊形,之後以半徑,圓心x坐标,圓心y坐标的形式給出圓,如果多邊形不是凸包輸出“HOLE IS ILL-FORMED”,如果多邊形是凸包但圓不在多邊形内輸出“PEG WILL NOT FIT”,如果多邊形是凸包且圓在多邊形内輸出“”(包括半徑為0的圓在多邊形的邊上)。

n小于3時結束程式。

思路:

模闆運用題,知道模闆就好做了。

1.凸包判斷按題目給出的順序枚舉3個點,建構線段叉乘,如果符号相同表示該多邊形為凸包,否則不是。

2.依次枚舉多邊形邊上的兩個點與圓心建構線段叉乘,如果符号相同表示該圓心在多邊形内,否則不在。

3.在枚舉多邊形邊上的兩個點與圓心同時還可以求出這三點構成三角形的高與半徑比較圓是否在多邊形内。

示例程式

Source Code

Problem: 1584		Code Length: 1679B
Memory: 392K		Time: 0MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <math.h>
struct point
{
    double x,y;
}p[200],peg;
double cross(struct point a,struct point b,struct point o)			//叉乘
{
    return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
int eps(double x)					//精度處理
{
    if(x>1e-8)
    {
        return 1;
    }
    else if(-x>1e-8)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}
int check(int n)					//凸包判斷
{
    int i;
    double t,t1;
    t=cross(p[0],p[1],p[2]);
    for(i=1;n>i;i++)
    {
        t1=cross(p[i],p[i+1],p[i+2]);
        if(eps(t*t1)<0)
        {
            return 0;
        }
    }
    return 1;
}
double dis(struct point a,struct point b,struct point c)
{
    double s,d;
    s=fabs(cross(c,a,b));					//求出面積(三角形2倍面積)
    d=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));			//三角形的底
    return s/d;								//三角形的高
}
int check1(int n,double r)						//圓是否在多邊形内
{
    int i;
    double t,t1,h;
    t=cross(p[0],p[1],peg);
    h=dis(p[0],p[1],peg);
    if(eps(h-r)<0)					//三角形的高小于半徑說明圓不在多邊形内
    {
        return 0;
    }
    for(i=1;n>=i;i++)
    {
        t1=cross(p[i],p[i+1],peg);
        h=dis(p[i],p[i+1],peg);
        if(eps(t*t1)<0||eps(h-r)<0)				//第一個條件是判斷圓心是否在多邊形内
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int n,i,i1;
    double min,r;
    scanf("%d",&n);
    while(n>=3)
    {
        scanf("%lf %lf %lf",&r,&peg.x,&peg.y);
        for(i=1;n>=i;i++)
        {
            scanf("%lf %lf",&p[i].x,&p[i].y);
        }
        p[0]=p[n];						//把多邊形封閉起來
        p[n+1]=p[1];
        if(check(n)==0)
        {
            printf("HOLE IS ILL-FORMED\n");
        }
        else if(check1(n,r)==0)
        {
            printf("PEG WILL NOT FIT\n");
        }
        else
        {
            printf("PEG WILL FIT\n");
        }
        scanf("%d",&n);
    }
    return 0;
}