A Round Peg in a Ground Hole

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 5684 Accepted: 1827

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

题意:给出一个多边形和一个圆,问是否是凸多边形,若是则再问圆是否在凸多边形内部。

1、判断是否是凸多边形

2、判断点是否在多边形内部

3、判断点到各边的距离是否大于等于半径

#include <iostream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <algorithm>
using namespace std; const int INF = 0x3f3f3f3f; const double Pi = 3.141592654; const double eps = 1e-6; typedef struct node
{
double x;
double y;
} Point;
Point *p;
Point peg;
double pegR;
int n;
double dotdet(node a,node b,node c)//计算点积
{
return (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);
}
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
} double cross(Point a,Point b,Point c)//计算叉积
{
return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
} double Dis(Point a,Point b)//计算距离
{
return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
} int precision(double s)//精度控制
{
if(fabs(s)<=eps)
{
return 0;
}
return s>0?1:-1;
} bool JudgeConvex()//判断是否是凸包
{
int temp=0;
int ans;
for(int i=0; i<n; i++)
{
ans=precision(cross(p[i],p[i+1],p[i+2]));
if(!temp)
{
temp=ans;
}
if(temp*ans<0)//如果不是同一的角度则不是凸包
{
return false;
}
}
return true;
}
double CalAngle(node a,node b)//计算角度
{
return acos((dotdet(peg,a,b))/(Dis(peg,a)*(Dis(peg,b))));
}
bool JudgeCenter()//判断圆心与凸包的关系
{
double Angle=0;
int ans;
for(int i=0; i<n; i++)
{
ans=precision(cross(peg,p[i],p[i+1]));//由叉积判断角度的方向.
if(ans>=0)
{
Angle+=CalAngle(p[i],p[i+1]);
}
else
{
Angle-=CalAngle(p[i],p[i+1]);
}
}
Angle=fabs(Angle);
if(precision(Angle)==0)//如果环绕角等于0说明圆心在凸包外侧
{
return false;
}
else if(precision(Angle-Pi)==0)//如果环绕角为180,圆心在凸包的边上(不包括顶点);
{
if(precision(pegR)==0)//只有半径为0的时候才能成立
return true;
}
else if(precision(Angle-2*Pi)==0)//如果环绕角为360,圆心在凸包的里面
{
return true;
}
else
{
if(precision(pegR)==0)//如果环绕角为0-360之间的角度则圆心在凸包的顶点所以只有半径为0的时候才符合
{
return true;
}
}
return false;
} bool JudgeRadius()//判断半径是不是符合:算出圆心到各边的距离和半径进行比较,如果所有的距离都小于半径,则半径是符合的.
{
for(int i=0;i<n;i++)
{
int k=precision(fabs(cross(peg,p[i],p[i+1])/Dis(p[i],p[i+1]))-pegR);
if(k<0)//如果距离小于半径则不符合
{
return false;
}
}
return true;
} int main()
{
while(scanf("%d",&n)&&n>=3)
{
p =new Point[n+10];
scanf("%lf %lf %lf",&pegR,&peg.x,&peg.y);
for(int i=1; i<=n; i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
}
p[0]=p[n];
p[n+1]=p[1];
if(!JudgeConvex())
{
printf("HOLE IS ILL-FORMED\n");
}
else
{
bool flag1=JudgeCenter();
bool flag2=JudgeRadius();
if(flag1&&flag2)
{
printf("PEG WILL FIT\n");
}
else
{
printf("PEG WILL NOT FIT\n");
}
}
}
return 0;
}
/*
HOLE IS ILL-FORMED
PEG WILL NOT FIT
PEG WILL FIT
PEG WILL NOT FIT
PEG WILL FIT
PEG WILL FIT
PEG WILL NOT FIT
PEG WILL FIT
PEG WILL NOT FIT
PEG WILL NOT FIT
PEG WILL NOT FIT
PEG WILL FIT
PEG WILL NOT FIT
HOLE IS ILL-FORMED
HOLE IS ILL-FORMED
PEG WILL FIT
HOLE IS ILL-FORMED
*/
/*
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
3 0.1 0.2 0.0
-0.5 1.0
0.5 -1.0
0.5 1.0
3 0.25 0.2 0.0
-0.5 1.0
0.5 -1.0
0.5 1.0
3 0.1 1.6 1.2
1.0 1.0
2.0 1.0
1.0 2.0
6 0.1 1.6 1.2
1.0 1.0
1.5 1.0
2.0 1.0
1.2 1.8
1.0 2.0
1.0 1.5
3 0.1 2.0 2.0
1.0 1.0
2.0 1.0
1.0 2.0
4 1.0 2.0 1.0
0.0 0.0
0.0 4.0
4.0 4.0
4.0 0.0
4 1.0 3.5 1.0
0.0 0.0
0.0 4.0
4.0 4.0
4.0 0.0
4 0.2 1.5 1.0
1.0 1.0
2.0 2.0
1.0 3.0
0.0 2.0
4 0.4 1.5 1.0
1.0 1.0
2.0 2.0
1.0 3.0
0.0 2.0
5 0.2 1.5 2.5
1.0 1.0
2.0 2.0
1.75 2.75
1.0 3.0
0.0 2.0
5 0.2 1.5 2.5
1.0 1.0
2.0 2.0
1.75 2.5
1.0 3.0
0.0 2.0
9 0.2 0.5 2.5
0.0 0.0
1.0 0.0
1.0 1.0
2.0 1.0
2.0 0.0
3.0 0.0
3.0 5.0
1.5 5.0
0.0 5.0
9 0.2 0.5 2.5
0.0 0.0
1.0 0.0
1.0 -1.0
2.0 -1.0
2.0 0.0
3.0 0.0
3.0 5.0
1.5 5.0
0.0 5.0
7 0.2 0.5 2.5
0.0 0.0
1.0 0.0
2.0 0.0
3.0 0.0
3.0 5.0
1.5 5.0
0.0 5.0
4 0.1 1 0.5
0 2
1 0
2 2
1 1
1
*/

A Round Peg in a Ground Hole(凸包应用POJ 1584)的更多相关文章

  1. POJ1584 A Round Peg in a Ground Hole 凸包判断 圆和凸包的关系

    POJ1584 题意:给定n条边首尾相连对应的n个点 判断构成的图形是不是凸多边形 然后给一个圆 判断圆是否完全在凸包内(相切也算) 思路:首先运用叉积判断凸多边形 相邻三条边叉积符号相异则必有凹陷 ...

  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: 6682   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: 4438   Acc ...

  4. POJ 1584:A Round Peg in a Ground Hole

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

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

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

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

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

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

  9. A Round Peg in a Ground Hole(判断是否是凸包,点是否在凸包内,圆与多边形的关系)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4628   Accepted: 1434 Description The D ...

随机推荐

  1. jdk安装与环境变量配置(一劳永逸)

    换了一个项目组做新的项目,新的机器,又得重新打环境,懒得去百度下,这里做个备份,下回直接看就行,如下 进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1.下载jdk(http ...

  2. 重复点击主界面(TabBar)按钮刷新界面--点击状态栏回到顶部

    1.监听按钮点击   2.判断是否是点击的同一个按钮(记录上次点击的按钮)   3.当重复点击相同按钮时,需要获取当前按钮对应控制器刷新界面      3.1 判断是否重复点击按钮,代码写在哪里?   ...

  3. Java堆内存

    Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象. 在 Java 中,堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ( Yo ...

  4. span和div的区别

    <span> 在CSS定义中属于一个行内元素,在行内定义一个区域,也就是一行内可以被 <span> 划分成好几个区域,从而实现某种特定效果. <span> 本身没有 ...

  5. 在windows下配置pthread

    http://blog.csdn.net/qianchenglenger/article/details/16907821 简单介绍windows平台下的pthread线程库

  6. 避免硬编码你的PostgreSQL数据库密码

    一个密码文件包含了我们需要连接的五个字段,所以我们可以使用文件权限来使密码更安全. host:port:dbname:user:password such as myhost:5432:postgre ...

  7. 转:Python requests 快速入门

    迫不及待了吗?本页内容为如何入门Requests提供了很好的指引.其假设你已经安装了Requests.如果还没有, 去 安装 一节看看吧. 首先,确认一下: ·Requests 已安装 ·Reques ...

  8. [Reprint]C++函数前和函数后加const修饰符区别

    c++中关于const的用法有很多,const既可以修饰变量,也可以函数,不同的环境下,是有不同的含义.今天来讲讲const加在函数前和函数后面的区别.比如: 01 #include<iostr ...

  9. Java编程思想(一):大杂烩

    在java中一切都被视为对象.尽管一切都是对象,但是操纵的标识符实际上是对象的一个引用,可以将引用想象成是遥控器(引用)来操纵电视机(对象).没有电视机,遥控器也可以单独存在,即引用可以独立存在,并不 ...

  10. 关于内存 GetMemory( ) 笔试分析

    1. #include<stdio.h>#include<string.h>void GetMemory(char *p){ p=(char *)malloc(100); }i ...