poj 1584 A Round Peg in a Ground Hole(計算幾何)

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

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


題意:給定一個n邊形,若不是凸多邊形則輸出HOLE IS ILL-FORMED,若是而且圓在凸多邊形裏面則輸出PEG WILL FIT,若是而且圓不在凸多邊形裏面則

輸出PEG WILL NOT FIT

題解:先判斷多邊形是否凸多邊形,根據第一條偏轉的線段判斷後面的偏轉方向是否相同。然後判斷圓心是否在多邊形內,判斷一下圓心對多邊形的三角形面積和是否等於原多邊形面積就可以了。然後就是判圓心到凸多邊形的距離,若圓心與點的連線和該點的邊組成鈍角,則最短距離爲到頂點的距離,否則是到直線的距離,到直線的距離可以用三角形面積巧妙計算,具體如代碼

#include<stdio.h>
#include<math.h>
#include<algorithm>
#define eps 1e-8
struct point{
    double x,y;
}p[1000008],c;
using namespace std;
double cross(point p1,point p2,point p3)
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
double dot(point p1,point p2,point p3)
{
    return (p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y);
}
double dis(point p1,point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int judge(int n,int i=0)
{
    double temp=cross(p[i],p[i+1],p[i+2]);
    while(fabs(temp)<eps&&i<n-2) i++,temp=cross(p[i],p[i+1],p[i+2]);
    for(i++;i<=n-2;i++)
    {
        if(cross(p[i],p[i+1],p[i+2])*temp<-eps)
            return 0;
    }
    return 1;
}
int judge2(int n,double temp=0,double temp2=0)
{
    for(int i=1;i<n;i++) temp+=fabs(cross(p[0],p[i],p[i+1]));
    for(int i=0;i<n;i++) temp2+=fabs(cross(c,p[i],p[i+1]));
    if(fabs(temp-temp2)<eps) return 1;
    return 0;
}
int main()
{
    int n,i,flag;
    double r,d;

    while(scanf("%d",&n),n>=3)
    {
        scanf("%lf%lf%lf",&r,&c.x,&c.y);
        for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        p[n]=p[0];
        if(!judge(n)) printf("HOLE IS ILL-FORMED\n");
        else
        {
            if(judge2(n)) flag=1;
            else flag=0;
            for(int i=0;i<n&&flag;i++)
            {
                if(dot(p[i],c,p[i+1])<-eps||dot(p[i+1],c,p[i])<-eps)
                    d=min(dis(c,p[i]),dis(c,p[i+1]));
                else d=fabs(cross(c,p[i],p[i+1]))/dis(p[i],p[i+1]);
                if(d<r) flag=0;
            }
            if(flag) printf("PEG WILL FIT\n");
            else printf("PEG WILL NOT FIT\n");
        }
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章