poj1584--A Round Peg in a Ground Hole(判断凸包,并且判断圆是否在凸包内)

A Round Peg in a Ground Hole

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5399   Accepted: 1712

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

题意:给出n个点,按顺时针或逆时针给出,问是不是凸包,和判断圆在不在凸包内。

如果图形是凸包,对任意连续三个点a,b,c的叉乘(b-a,c-b)的正负相同,也就是说在同一边,这样就可以判断凸包。

判断圆在不在凸包内,首先判断圆心,在不在凸包内,如果在凸包内的话叉乘(b-a,r-a)得到的正负应该与判断凸包时相同。

然后判断每条边到圆心的距离是否大于半径,由叉乘(b-a,r-a)得到以<a,b> <a,r>为边的平行四边形面积,再除以<a,b>的距离就可以了。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std ;
#define eps 1e-8
struct node{
    double x , y ;
}p[10005] , q ;
double r , r_x , r_y ;
node sub(node a,node b)
{
    a.x -= b.x ;
    a.y -= b.y ;
    return a ;
}
int mul(node a,node b)
{
    if( a.x*b.y - a.y*b.x >= 0 )
        return 1 ;
    else
        return -1 ;
}
double dis(node a,node b)
{
    if( fabs(a.x-b.x) < eps )
        return fabs(q.x-a.x) ;
    else if( fabs(a.y-b.y) < eps )
        return fabs(q.y-a.y) ;
    else
    {
        node s , e ;
        s = sub(a,b) ;
        e = sub(q,b) ;
        return fabs(s.x*e.y-s.y*e.x)/( sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) );
    }
}
int main()
{
    int n , i , j , flag ;
    while( scanf("%d", &n) && n >= 3 )
    {
        scanf("%lf %lf %lf", &r, &q.x, &q.y) ;
        for(i = 0 ; i < n ; i++)
            scanf("%lf %lf", &p[i].x, &p[i].y) ;
        p[n] = p[0] ;
        flag = mul(sub(p[1],p[0]),sub(p[2],p[1])) ;
        for(i = 1 ; i < n ; i++)
        {
            if( flag != mul(sub(p[i],p[i-1]),sub(p[i+1],p[i]) ) )
                break ;
        }
        if( i < n )
        {
            printf("HOLE IS ILL-FORMED\n") ;
            continue ;
        }
        for(i = 0 ; i < n ; i++)
        {
            if( flag != mul( sub(p[i+1],p[i]),sub(q,p[i]) ) )
                break ;
            //printf("%d  %lf\n", i, dis(p[i],p[i+1]) ) ;
            if( dis(p[i],p[i+1]) - r < 0 )
                break ;
        }
        if( i < n )
            printf("PEG WILL NOT FIT\n") ;
        else
            printf("PEG WILL FIT\n") ;
    }
    return 0;
}
时间: 2024-10-05 19:59:11

poj1584--A Round Peg in a Ground Hole(判断凸包,并且判断圆是否在凸包内)的相关文章

poj1584 A Round Peg in a Ground Hole 判断多边形凹凸,点到线的距离【基础计算几何】

大致思路:首先对于所给的洞的点,判断是否是凸多边形,图形的输入和输出可以是顺时针或者逆时针,而且允许多点共线 Debug 了好几个小时,发现如下问题 判断三点是否共线,可用斜率公式判断 POINT point_A, point_B, point_C; if(point_A.x == point_B.x || point_B.x == point_C.x){ if(point_A.x == point_B.x && point_B.x == point_C.x) continue; }els

poj1584 A round peg in a ground hole【计算几何】

含[判断凸包],[判断点在多边形内],[判断圆在多边形内]模板  凸包:即凸多边形 用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点. The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to one another using a wooden pe

POJ1584 A Round Peg in a Ground Hole 凸包判断 圆和凸包的关系

POJ1584 题意:给定n条边首尾相连对应的n个点 判断构成的图形是不是凸多边形 然后给一个圆 判断圆是否完全在凸包内(相切也算) 思路:首先运用叉积判断凸多边形 相邻三条变叉积符号相异则必有凹陷 O(n) 之后首先判断圆心是否在凸多边形内 如果凸多边形的点有序 则可以在logn时间内判断 否则先排序再判断 O(nlogn) 然后用每条边(线段)判断到圆心的距离即可 这道题也没给数据范围 O(nlogn)是可以AC的. #include<iostream> #include<stdio

A Round Peg in a Ground Hole POJ - 1584(凸边形与圆相交)

A Round Peg in a Ground Hole POJ - 1584 题目链接:https://vjudge.net/problem/POJ-1584#author=0 题意:要求钉子要钉入孔内,判断能否在指定点钉入 思路:先判断这些点围成的多边形是不是凸多边形,如果是那么判断圆是否在凸边形里,若在那么就输出“PEG WILL FIT“,不在凸边形里就输出“PEG WILL NOT FIT”,如果都不是凸边形那么输出“HOLE IS ILL-FORMED” // // Created

POJ1584-A Round Peg in a Ground Hole(凸包,判圆在凸包内)

A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5301   Accepted: 1662 Description The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to on

【POJ 1584】 A Round Peg in a Ground Hole (判凸包+判圆在凸包内)

[POJ 1584] A Round Peg in a Ground Hole (判凸包+判圆在凸包内) 这题题面是一大坑..长长的 明显是给我这种英语渣准备的... 大体意思是给出一个多边形的点 按顺时针或逆时针给出 判断是否为凸包 同时给出一个圆(圆心坐标+半径) 问这个圆在不在多边形内 首先顺逆时针不确定 我的做法是输入时先判断顺时针还是逆时针输入 然后统统变成逆时针来走 就是根据两种情况传入不同的枚举起点 终点 和加减(具体见代码 判凸包用建凸包的叉成法即可 既然逆时针走 那么如果是凸包

poj 1584 A Round Peg in a Ground Hole(计算几何)

A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5603   Accepted: 1788 Description The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to on

POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内

首先判断是不是凸多边形 然后判断圆是否在凸多边形内 kuangbin的板子,但是有些地方不明白. 判断多边形不是凸多边形后,为什么用判断点是否在凸多边形内的模板交WA了,而用判断点是否在任意多边形内的模板A了 而且判断点是否在任意多边形的注释,返回值为什么又说是凸多边形~~~ POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内) #include <iostream> #include <cstdio> #inclu

POJ 1584 A Round Peg in a Ground Hole(凸包判定&amp;&amp;圆在凸包内判定)

博客原文地址:http://blog.csdn.net/xuechelingxiao/article/details/39178777 A Round Peg in a Ground Hole 题目大意:按顺时针或逆时针给出多边形的顶点坐标.圆的半径及圆心坐标. 1.求多边形是否是个凸包,若不是输出"HOLE IS ILL-FORMED". 2.如果多边形为凸包,判定圆是否在凸包内,若凸包在园内,输出"PEG WILL FIT",若不在,输出"PEG WI