A Round Peg in a Ground Hole

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 3574

 

Accepted: 1051

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

Mid-Atlantic 2003

 

 解题报告:题意给出一个多边形及一个圆的具体位置,判断这个多边形是否能完全覆盖这个圆,首先判断多边形是否是凸包,其次判断圆心是否在多边形中,最后在判断一下圆心到多边形每条边的距离是否大于圆自身的半径;思路清晰了只是套用模板的问题了,求三角形的面积我用到了两种方法;

 

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAX = 205;
const double eps = 1e-8;
int n;
double r, rx, ry;//钉子的位置
int flag;
struct Point
{
    double x;
    double y;
}p[MAX], triangle[3];//存储点的坐标
double Cross(Point p1, Point p2, Point p3)
{
    return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.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));
}
/*double GetArea(int m, Point tr[])
{
    int i;
    double area = tr[0].y * (tr[m - 1].x - tr[1].x);
    for (i = 1; i < m; ++i)//求三角形的面积
    {
        area += tr[i].y * (tr[i - 1].x - tr[(i + 1) % m].x);
    }
    if (area < 0)
    {
        area = -area;
    }
    return area = area / 2.0;
}*/
//提交下面求三角形面积的时候用C++就编译错误,用G++交就AC!但是上面求三角形的面积时用C++交就AC!那位大牛知道原因呀!
double GetArea(int m, Point tr[])//利用的是海伦公式求的三角形面积
{
    double a[m];
    double pp = 0;
    for (int i = 0; i < m; ++i)
    {
        a[i] = Dis(tr[i], tr[(i + 1) % m]);
        pp += a[i];
    }
    pp = pp / 2.0;
    return sqrt(pp * (pp - a[0]) * (pp - a[1]) * (pp -a[2]));
}
int Judge()
{
    double a, b;
    int i;
    Point p3;
    p3.x = rx;
    p3.y = ry;
    b = Cross(p[0], p[1], p3);
    for (i = 1; i < n; ++i)
    {
        a = Cross(p[i], p[(i + 1) % n], p3);
        if (a * b < - eps)
        {
            return 1;
        }
        b = a;
    }
    return 0;
}
int main()
{
    int i, k, j;
    while (scanf("%d", &n) != EOF)
    {
        if (n < 3)
        {
            break;
        }
        scanf("%lf%lf%lf", &r, &rx, &ry);
        for (i = 0; i < n; ++i)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        double dis;
        for (i = 0; i + 2 < n; ++i)
        {
            dis = Cross(p[i + 1], p[i + 2], p[i]);
            if (abs(dis) >= eps)//先确定多边形的输入是逆时针还是顺时针
            {
                break;
            }
        }
        flag = 1;
        for (i = 0; i < n && flag; ++i)
        {
            for (j = 0, k = i + 2; j < n - 2; j ++, k ++)
            {
                double q = Cross(p[(i + 1) % n], p[k % n], p[i]);
                if (q * dis < -eps)//出现了旋转方向相反的点了
                {
                    flag = 0;
                    break;
                }
            }
        }
        if (flag == 0)//不是凸包
        {
            printf("HOLE IS ILL-FORMED\n");
            continue;
        }
        if (Judge())//判断圆心是否在凸多边形的内部
        {
            printf("PEG WILL NOT FIT\n");
            continue;
        }
        triangle[0].x = rx;
        triangle[0].y = ry;
        for (i = 0; i < n; ++i)
        {
            triangle[1] = p[i];
            triangle[2] = p[(i + 1) % n];
            //求圆心到多边形每条边的距离利用了三角形的面积 = (底边的边长 * 高) / 2;
            //即 dis = 2.0 * area / h;
            dis = 2.0 * GetArea(3, triangle) / Dis(triangle[1], triangle[2]);
            if (dis < r)//若距离大于圆形的半径时,说明多边形不能完全覆盖圆形
            {
                flag = 0;
                break;
            }
        }
        if (flag)
        {
            printf("PEG WILL FIT\n");
        }
        else
        {
            printf("PEG WILL NOT FIT\n");
        }
    }
    return 0;
}

 

 

 

posted on 2012-05-04 20:28  Stephen Li  阅读(244)  评论(0编辑  收藏  举报