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

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

 
 
 
 
 
 
 

首先是判断给出了多边形是不是凸多边形。

然后判断圆包含在凸多边形中。

一定要保证圆心在凸多边形里面。

然后判断圆心到每条线段的距离要大于等于半径。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h> using namespace std; const double eps = 1e-;
const double PI = acos(-1.0);
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
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;
}
void input()
{
scanf("%lf%lf",&x,&y);
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
};
//*两点间距离
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
//判断凸多边形
//允许共线边
//点可以是顺时针给出也可以是逆时针给出
//点的编号1~n-1
bool isconvex(Point poly[],int n)
{
bool s[];
memset(s,false,sizeof(s));
for(int i = ;i < n;i++)
{
s[sgn( (poly[(i+)%n]-poly[i])^(poly[(i+)%n]-poly[i]) )+] = true;
if(s[] && s[])return false;
}
return true;
}
//点到线段的距离
//返回点到线段最近的点
Point NearestPointToLineSeg(Point P,Line L)
{
Point result;
double t = ((P-L.s)*(L.e-L.s))/((L.e-L.s)*(L.e-L.s));
if(t >= && t <= )
{
result.x = L.s.x + (L.e.x - L.s.x)*t;
result.y = L.s.y + (L.e.y - L.s.y)*t;
}
else
{
if(dist(P,L.s) < dist(P,L.e))
result = L.s;
else result = L.e;
}
return result;
}
//*判断点在线段上
bool OnSeg(Point P,Line L)
{
return
sgn((L.s-P)^(L.e-P)) == &&
sgn((P.x - L.s.x) * (P.x - L.e.x)) <= &&
sgn((P.y - L.s.y) * (P.y - L.e.y)) <= ;
}
//*判断点在凸多边形内
//点形成一个凸包,而且按逆时针排序(如果是顺时针把里面的<0改为>0)
//点的编号:0~n-1
//返回值:
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inConvexPoly(Point a,Point p[],int n)
{
for(int i = ;i < n;i++)
{
if(sgn((p[i]-a)^(p[(i+)%n]-a)) < )return -;
else if(OnSeg(a,Line(p[i],p[(i+)%n])))return ;
}
return ;
}
//*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= ;
}
//*判断点在任意多边形内
//射线法,poly[]的顶点数要大于等于3,点的编号0~n-1
//返回值
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inPoly(Point p,Point poly[],int n)
{
int cnt;
Line ray,side;
cnt = ;
ray.s = p;
ray.e.y = p.y;
ray.e.x = -100000000000.0;//-INF,注意取值防止越界 for(int i = ;i < n;i++)
{
side.s = poly[i];
side.e = poly[(i+)%n]; if(OnSeg(p,side))return ; //如果平行轴则不考虑
if(sgn(side.s.y - side.e.y) == )
continue; if(OnSeg(side.s,ray))
{
if(sgn(side.s.y - side.e.y) > )cnt++;
}
else if(OnSeg(side.e,ray))
{
if(sgn(side.e.y - side.s.y) > )cnt++;
}
else if(inter(ray,side))
cnt++;
}
if(cnt % == )return ;
else return -;
}
Point p[];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
double R,x,y;
while(scanf("%d",&n) == )
{
if(n < )break;
scanf("%lf%lf%lf",&R,&x,&y);
for(int i = ;i < n;i++)
p[i].input();
if(!isconvex(p,n))
{
printf("HOLE IS ILL-FORMED\n");
continue;
}
Point P = Point(x,y);
if(inPoly(P,p,n) < )
{
printf("PEG WILL NOT FIT\n");
continue;
}
bool flag = true;
for(int i = ;i < n;i++)
{
if(sgn(dist(P,NearestPointToLineSeg(P,Line(p[i],p[(i+)%n]))) - R) < )
{
flag = false;
break;
}
}
if(flag)printf("PEG WILL FIT\n");
else printf("PEG WILL NOT FIT\n");
}
return ;
}

POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)的更多相关文章

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

    首先判断是不是凸多边形 然后判断圆是否在凸多边形内 不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板 POJ 1584 A Round Peg in a G ...

  2. POJ 1584 A Round Peg in a Ground Hole 判断凸多边形,判断点在凸多边形内

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5456   Acc ...

  3. POJ 1584 A Round Peg in a Ground Hole[判断凸包 点在多边形内]

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6682   Acc ...

  4. POJ 1584 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

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

    http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全 ...

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

    题意: 给一个圆和一个多边形,多边形点可能按顺时针给出,也可能按逆时针给出,先判断多边形是否为凸包,再判断圆是否在凸包内. 解法: 先判是否为凸包,沿着i=0~n,先得出初始方向dir,dir=1为逆 ...

  7. 简单几何(点的位置) POJ 1584 A Round Peg in a Ground Hole

    题目传送门 题意:判断给定的多边形是否为凸的,peg(pig?)是否在多边形内,且以其为圆心的圆不超出多边形(擦着边也不行). 分析:判断凸多边形就用凸包,看看点集的个数是否为n.在多边形内用叉积方向 ...

  8. POJ 1584 A Round Peg in a Ground Hole

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

  9. POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

随机推荐

  1. HibernateTemplate 查询

    Spring中常用的hql查询方法getHibernateTemplate()上     一.find(String queryString);   示例:this.getHibernateTempl ...

  2. staging server, source congtrol, deply workflow using git

    web项目开发中,有三个实践对于项目成功是非常重要的: 1. staging servers 2. Version control workflows 3. Tested, repeatable de ...

  3. Phpstorm Xdebug Web程序调试

    平时调试php程序的时候,可以通过在代码中添加var_dump等函数来实现简单的断点调试. 下面介绍另一种方法,通过Phpstorm和Xdebug来进行调试. 1.下载Xdebug 这个是官网下载地址 ...

  4. HDU 5326 work (回溯,树)

    题意:给一棵树,每个结点的子树下的结点都是它的统治对象,问有多少个统治对象数目为k的结点? 思路:每个结点都设一个cnt来记数,只要将每个结点往上回溯,直到树根,经过的每个结点都将计数器加1.最后再扫 ...

  5. ecshop 多语言切换

    1.打开includes/init.php找到下面两行代码并删除 require(ROOT_PATH . 'languages/' . $_CFG['lang'] . '/common.php'); ...

  6. 【英语】Bingo口语笔记(32) - 口语中的弱读

  7. 《Write Optimized B-Trees》读书报告

    论文原作者:Goetz Graefe, Microsoft.我读完这篇论文后颇有收获,所以写了一篇论文报告,旨在更精炼准确地阐述论文核心思想. 摘要:论文提出了一种方法,这种方法可以优化B树索引写性能 ...

  8. Linux下tail命令

    简述 tail命令从指定点开始将文件写到标准输出,使用tail命令的“-f”选项可以方便的查阅正在改变的日志文件,“tail -f filename”会把filename里最尾部的内容显示在屏幕上,并 ...

  9. Android studio gradle 打包 那些事

    总结了一下 目前觉得比较好用的gradle 和一些打包 经验.放在这里. 首先说下 渠道号 这个概念,我们经常会统计我们的api 访问来源 是来自于那个app store,这有利于 我们针对性的推广. ...

  10. 小结JS中的OOP(上)

    前言:大家都知道,OOP有三大特性:封装,继承,多态.下面是自己对这三个特性的理解: 封装:把属性与方法整合到某种数据类型中.目的是让类的使用者按类的编写者的意愿去使用类.在封装过程中会一般会做两件事 ...