Q - A Round Peg in a Ground Hole POJ - 1584 (叉积判凸多边形和叉积求面积问题)

 Q - A Round Peg in a Ground Hole POJ - 1584 

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 (x i+1, y i+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

题意:给定圆心坐标,圆半径,以及不规则多边形的顶点坐标,请判断该多边形是否为凸多边形,不是凸多边形输出HOLE IS ILL-FORMED,如果是,请判断是否圆在凸多边形内,如果在凸多边形内,输出PEG WILL FIT,不在,输出PEG WILL NOT FIT

思路:叉积求面积,如果求出的面积为正数,则求叉积第一条边和第二条边为逆时针,否则为顺时针,由于题目给出的点是有顺序的,所以主要按顺序判断连续的两条边的叉积,如果都相同,则为凸多边形;根据叉积同样也可以判断圆心是否在凸多边形内,在多边形外的圆心求出的面积的符号不相同,最后用叉积求面积得出的圆心到各边的距离可以判断圆是否在多边形内

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1000010;
const int mmax = 40200+ 7;
const double esp = 1e-8;
int check(double x)
{
    if(fabs(x)<esp)
        return 0;
    if(x<0)
        return -1;
    return 1;
}
struct point
{
    double x,y;
    point() {}
    point(double _x,double _y):x(_x),y(_y)
    {
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    double operator ^(const point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const point &b)const
    {
        return x*b.x+y*b.y;
    }
} a[1111];
int main()
{

    int n;
    double r;
    while(scanf("%d",&n)!=EOF)
    {
        if(n<3)
            break;
        point pos;
        scanf("%lf%lf%lf",&r,&pos.x,&pos.y);
        for(int i=0; i<n; i++)
            scanf("%lf%lf",&a[i].x,&a[i].y);
        int flag=0,judge=1;
        for(int i=0; i<n; i++)  //判断凸多边形
        {
            int t=check((a[i]-a[(i+1)%n])^(a[(i+1)%n]-a[(i+2)%n])); 
            if(flag==0)
            {
                flag=t;
            }
            else
            {
                if(t==0)
                    continue;
                if(t!=flag)
                {
                    printf("HOLE IS ILL-FORMED\n");
                    judge=-1;
                    break;
                }
            }
        }
        if(judge==-1)
            continue;
        flag=0;
        for(int i=0; i<n; i++)
        {
            double area=(pos-a[i])^(pos-a[(i+1)%n]); 
            int t=check(area);
            if(t==0)   //在凸多边形边上
            {
                judge=0;
                break;
            }
            if(i==0)
                flag=t;
            else if(t!=flag)     //判断圆心是否在四边形内
            {
                judge=0;
                break;
            }
            double d=sqrt((a[i]-a[(i+1)%n])*(a[i]-a[(i+1)%n]));  
            double h=fabs(area)/d;  //圆心到多边形边i_i+1的距离
            if(check(h-r)==-1)    //判断圆是否在多边形内
            {
                judge=0;
                break;
            }
        }
        if(judge)
            printf("PEG WILL FIT\n");
        else
            printf("PEG WILL NOT FIT\n");
    }
    return 0;
}
扫描二维码关注公众号,回复: 3725550 查看本文章

猜你喜欢

转载自blog.csdn.net/clz16251102113/article/details/82874496