码迷,mamicode.com
首页 > 其他好文 > 详细

poj1584——判断凸包,判断点是否在多边形内

时间:2015-04-03 14:55:34      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

poj1584——判断凸包,判断点是否在多边形内

A Round Peg in a Ground Hole
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5441   Accepted: 1729

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

题意:判断一个多边形是否凸包且内含给定圆,包含给定圆可内切(这里WA了一次)
思路:先通过叉积判断凸包,再判断圆心是否在多边形内,再判断是否内含或内切
技术分享
/**
 * Start at 12:33
 * End at 13:17
 * Problem: poj1584
 * Author: __560
 *
 */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<math.h>

using namespace std;

const int maxn=1000100;
const int INF=(1<<28);
const double eps=0.00000001;

int n;
struct Point
{
    double x,y;
    friend double operator*(const Point A,const Point B)
    {
        return A.x*B.y-A.y*B.x;
    }
    friend Point operator-(const Point A,const Point B)
    {
        return {A.x-B.x,A.y-B.y};
    }
};
Point vert[maxn],peg;
double R;

bool convex_bag(Point*vert,int n) ///判断是否凸包
{
    for(int i=0;i<n;i++){
        if(((vert[(i+2)%n]-vert[(i+1)%n])*(vert[(i+1)%n]-vert[i]))*((vert[(i+3)%n]-vert[(i+2)%n])*(vert[(i+2)%n]-vert[(i+1)%n]))<-eps)
            return false;
    }
    return true;
}

bool isin(Point*vert,int n,Point peg) ///判断点是否在多边形内
{
    for(int i=0;i<n;i++){
        if(((vert[i]-peg)*(vert[(i+1)%n]-peg))*((vert[(i+1)%n]-peg)*(vert[(i+2)%n]-peg))<eps) return false;
    }
    return true;
}

double dist(Point A,Point B)
{
    return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));
}

bool contain_circle(Point*vert,int n,Point peg,double R) ///判断多边形是否内含或内切已知圆
{
    for(int i=0;i<n;i++){
        double Area=fabs((vert[(i+1)%n]-peg)*(vert[i]-peg));
        double d=Area/dist(vert[i],vert[(i+1)%n]);
        if(d<R-eps) return false;
    }
    return true;
}

int main()
{
    //freopen("out.txt","w",stdout);
    while(cin>>n,n>=3){
        cin>>R>>peg.x>>peg.y;
        for(int i=0;i<n;i++) scanf("%lf%lf",&(vert[i].x),&(vert[i].y));
        if(!convex_bag(vert,n)){
            printf("HOLE IS ILL-FORMED\n");
            continue;
        }
        if(!isin(vert,n,peg)){
            printf("PEG WILL NOT FIT\n");
            continue;
        }
        if(contain_circle(vert,n,peg,R)) printf("PEG WILL FIT\n");
        else printf("PEG WILL NOT FIT\n");
    }
    return 0;
}
View Code

 

poj1584——判断凸包,判断点是否在多边形内

标签:

原文地址:http://www.cnblogs.com/--560/p/4389817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!