程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj1584--A Round Peg in a Ground Hole(判斷凸包,並且判斷圓是否在凸包內)

poj1584--A Round Peg in a Ground Hole(判斷凸包,並且判斷圓是否在凸包內)

編輯:C++入門知識

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)得到以 為邊的平行四邊形面積,再除以的距離就可以了。

#include 
#include 
#include 
#include 
#include 
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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved