poj 1584 A Round Peg in a Ground Hole

来源:互联网 发布:佳能 cr2 编辑软件 编辑:程序博客网 时间:2024/05/19 10:52

题目链接:点击打开链接

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
知识点:点在凸多边形内的判定,点到直线的距离(用叉积求),凸多边形的判定,叉积的运用

其实搞明白上面的知识点这个题也就解出来了;
题目大意:有一个圆形的钉子和能插钉子的筒口,但是这个筒不是正规的,是由多个点构成的多边形,问能否将这个筒通过一些方法(如向筒内塞一些胶)使得钉子和筒能够吻合;
通过题意可以看出,先要判定是否为凸的多边形(这是题目直接要求的),然后判定钉子的圆点在不在多边形内,然后在判定圆点到多边形的距离是否大于钉子的半径,然后根据要求输出给的字符串

ps:凸多边形的判定:如果该多边形为凸多边形,则相邻边的叉积符号应该是相同的,否则不是凸多边形
     点在凸多边形内的判定:如果点在凸多边形内,也就意味着该点在该多边形每条边的同一侧(左或右),也就是每条边的向量和该点与边上端点所形成的向量的叉积是相同符号的,否则不在凸多边形内。
    点道直线的距离:利用直线的两点和给出的点这三个点所形成的两个向量的叉积是由这两个向量所构成的平行四边形的有向面积,再除去一天边的长度,就是高(点到直线的距离)
<span style="font-size:18px;">#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>const double eps=1e-8;using namespace std;struct point{    double x,y;} p[100005],o;int n;double r;int dcmp(double x){    if(fabs(x)<eps)return 0;    else return x<0?-1:1;}double cross(point a,point b,point c){    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);}bool istubianxing(){    int a,b;    a=0;    for(int i=0; i<n; i++)    {        b=dcmp(cross(p[i+1],p[i+2],p[i]));        if(!a)a=b;        else if(a&&a*b<0)return false;    }    return true;}bool inround(){    int b,a;    a=dcmp(cross(p[1],o,p[0]));    for(int i=1;i<n;i++)    {        b=dcmp(cross(p[i+1],o,p[i]));        if(a*b<0)return false;    }    return true ;}double length(point a,point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double rl(point a,point b){    return fabs(cross(a,o,b)/length(a,b));}bool rmax(){    for(int i=0;i<n;i++)    {        if(rl(p[i],p[i+1])<r)return false;    }    return true;}int main(){    while(cin>>n,n>=3)    {        cin>>r>>o.x>>o.y;        for(int i=0; i<n; i++)        {            cin>>p[i].x>>p[i].y;        }        p[n]=p[0];        p[n+1]=p[1];        if(!istubianxing())        {            cout<<"HOLE IS ILL-FORMED\n";        }        else        {            if(!inround()||!rmax())            {                cout<<"PEG WILL NOT FIT\n";            }            else cout<<"PEG WILL FIT\n";        }    }    return 0;}</span>


0 0
原创粉丝点击