poj 1584 A Round Peg in a Ground Hole

来源:互联网 发布:分镜头软件 编辑:程序博客网 时间:2024/06/07 03:20
A Round Peg in a Ground Hole
Time Limit: 1000MS Memory Limit: 10000KTotal 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 (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

Source

Mid-Atlantic 2003

提示

题意:

这题目的意思就是给出以顺时针或者逆时针点坐标的方式给出一个多边形,之后以半径,圆心x坐标,圆心y坐标的形式给出圆,如果多边形不是凸包输出“HOLE IS ILL-FORMED”,如果多边形是凸包但圆不在多边形内输出“PEG WILL NOT FIT”,如果多边形是凸包且圆在多边形内输出“”(包括半径为0的圆在多边形的边上)。

n小于3时结束程序。

思路:

模板运用题,知道模板就好做了。

1.凸包判断按题目给出的顺序枚举3个点,构建线段叉乘,如果符号相同表示该多边形为凸包,否则不是。

2.依次枚举多边形边上的两个点与圆心构建线段叉乘,如果符号相同表示该圆心在多边形内,否则不在。

3.在枚举多边形边上的两个点与圆心同时还可以求出这三点构成三角形的高与半径比较圆是否在多边形内。

示例程序

Source CodeProblem: 1584Code Length: 1679BMemory: 392KTime: 0MSLanguage: GCCResult: 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;}

0 0
原创粉丝点击