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 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

题意:已知一个多边形的n个顶点坐标,然后再给一个钉子,给定钉子的半径和圆心坐标,首先判断多边形是否为凸多边形,若为凸多边形,再判断钉子是否在到凸多边形内部。


题解:若为凸多边形,则先判断圆心是否在多边形里面,若不在则圆在外部,否则再判断多边形是否存在边到圆心的距离小于r的。
#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
const double eps=1e-10;
#define _sign(x) ((x)>eps?1:((x)<-eps?2:0))

using namespace std;
int n;
double r;

struct Point {
    double x,y;
};

Point peg;
vector<Point>vec;

double xmulti(Point p1,Point p2,Point p0) {
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

///判断是否是凸多边形
int is_convex() {
    int i,s[3]= {1,1,1};
    for(i=0; i<n&&s[1]|s[2]; i++) {
        s[_sign(xmulti(vec[(i+1)%n],vec[(i+2)%n],vec[i]))]=0;
    }
    return s[1]|s[2];
}

///判断点p是否在多边形里面
int inside(Point q) {
    int i,s[3]= {1,1,1};
    for(i=0; i<n&&s[1]|s[2]; i++) {
        s[_sign(xmulti(vec[(i+1)%n],q,vec[i]))]=0;
    }
    return s[0]&&(s[1]|s[2]);
}

double dist(Point a,Point b) {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

///直线交点
Point inter(Point u1,Point u2,Point v1,Point v2) {
    Point ret=u1;
    double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
             /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
    ret.x+=(u2.x-u1.x)*t;
    ret.y+=(u2.y-u1.y)*t;
    return ret;
}

///线段交点
Point ptoseg(Point p,Point l1,Point l2) {
    Point t=p;
    t.x+=l1.y-l2.y;
    t.y+=l2.x-l1.x;
    if(xmulti(l1,t,p)*xmulti(l2,t,p)>eps)
        return dist(p,l1)<dist(p,l2)?l1:l2;
    return inter(p,t,l1,l2);
}

int main() {
    //freopen("test.in","r",stdin);
    while(cin>>n) {
        if(n<3)break;
        vec.clear();
        scanf("%lf%lf%lf",&r,&peg.x,&peg.y);
        Point it;
        for(int i=0; i<n; i++) {
            scanf("%lf%lf",&it.x,&it.y);
            vec.push_back(it);
        }
        if(!is_convex()) {///不是凸多边形
            printf("HOLE IS ILL-FORMED\n");
            continue;
        }
        if(!inside(peg)) {///圆心不在里面
            printf("PEG WILL NOT FIT\n");
            continue;
        }
        bool flag=0;
        for(int i=0; i<n; i++) {
            it= ptoseg(peg,vec[i],vec[(i+1)%n]);
            double R=dist(it,peg);
            if(R<r){
               flag=1;
               break;
            }
        }
        if(flag)printf("PEG WILL NOT FIT\n");
        else    printf("PEG WILL FIT\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 18:34:14

poj 1584 A Round Peg in a Ground Hole(计算几何)的相关文章

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 判断多边形是否为凸多边形 + 圆心是否在凸多边形内 + 圆是否在凸多边形内部

题目来源: http://poj.org/problem?id=1584 题意: 给一个多边形, 一个圆心以及半径. 首先判断是否为凸多边形. 如果是凸多边形, 再判断,圆是否在凸多边形内部. 分析: 1) 先判断是否为凸多边形 ,题目给出的顶点是有序的, 即顺时针或是 逆时针.用叉积方向判断. 2) 判断圆在多边形内, 首先判断 圆心是否在多边形内部, 是的话,然后再 判断 圆心到多边形 所有边的 距离d >= r , 即可. 代码如下: const int Max_N = 1005; con

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

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

http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全在n边形内部. 分析 1.判断给出了多边形是不是凸多边形. 2.判断圆包含在凸多边形中:一定要保证圆心在凸多边形里面.然后判断圆心到每条线段的距离要大于等于半径.. #include <iostream> #include <stdio.h> #include <string.h

POJ 1584 A Round Peg in a Ground Hole

先判断是不是N多边形,求一下凸包,如果所有点都用上了,那么就是凸多边形 判断圆是否在多边形内, 先排除圆心在多边形外的情况 剩下的情况可以利用圆心到每条边的最短距离与半径的大小来判断 #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<list> #include<algorithm> us

POJ 1584 A Round Peg in a Ground Hole --判定点在形内形外形上

题意: 给一个圆和一个多边形,多边形点可能按顺时针给出,也可能按逆时针给出,先判断多边形是否为凸包,再判断圆是否在凸包内. 解法: 先判是否为凸包,沿着i=0~n,先得出初始方向dir,dir=1为逆时针,dir=-1为顺时针,然后如果后面有两个相邻的边叉积后得出旋转方向为nowdir,如果dir*nowdir < 0,说明方向逆转了,即出现了凹点,说明不是凸多边形. 然后判圆是否在多边形内: 先判圆心是否在多边形内,用环顾法,然后如果在之内,则依次判断圆心与每条凸包边的距离与半径的距离,如果所

POJ 1584 A Round Peg in a Ground Hole(点到直线距离,圆与多边形相交,多边形是否为凸)

题意:给出一个多边形和一个圆,问是否是凸多边形,若是则再问圆是否在凸多边形内部. 分3步: 1.判断是否是凸多边形 2.判断点是否在多边形内部 3.判断点到各边的距离是否大于等于半径 上代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using na

POJ 1584 A Round Peg in a Ground Hole(凸多边形判断 + 点是否在多边形内 + 点到线段的最短距离)

题目:传送门 题意:给你一个圆和一个多边形, 判断多边形是不是凸多边形,如果是,接着判断圆是否在凸多边形内部. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <map> #include <vector> #include <set> #inclu

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

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