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

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 这篇博客有测试数据:http://blog.csdn.net/lyy289065406/article/details/6648606
题意:输入n个点,圆的半径以及圆的坐标X,Y。然后是每个点的x,y坐标.
判断输入的点是不是能够组成凸包,不能的话输出 HOLE IS ILL-FORMED,判断圆是否在凸包内.如果在的话输出PEG WILL FIT.否则输出 PEG WILL NOT FIT
很多模板的综合应用.
这个题判断点是否在多边形内有两种模板:
一种局限性大,只适合按照顺(逆)时针输入的凸多边形,不过很容易理解:
/**判断点是否在凸多边形内(这个比较容易打并且适合这题)*/
bool pointInConvex(int n,double x,double y){
Point po;
p[n] = p[];
po.x = x,po.y = y;
bool flag = isCross(cross(p[],p[],po)); ///既然是判断了是凸多边形肯定相邻两个点是朝同一个方向转
for(int i=;i<n;i++){
if(isCross(cross(p[i],p[i+],po))!=flag){
return false;
}
}
return true;
}

另外一种具有普遍性,什么样子的多边形都OK,大牛总结的:

/**判断点是否在多边形内(具有普遍性,不过并不能看懂)*/
bool pointInPolygon(int n,double x,double y)
{
int i,j=n-;
bool oddNodes=;
for (i=; i<n; i++)
{
if((p[i].y< y && p[j].y>=y|| p[j].y<y && p[i].y>=y) && (p[i].x<=x||p[j].x<=x))
{
oddNodes^=(p[i].x+(y-p[i].y)/(p[j].y-p[i].y)*(p[j].x-p[i].x)<x);
}
j=i;
}
return oddNodes;
}

代码:

///题意:输入n个点,圆的半径以及圆的坐标X,Y。
///先是判断输入的点是不是能够组成凸包(这里的判断就是所有的点是不是往同一个方向"拐")
///然后判断圆心是否在凸包内.如果在的话就判断圆心的和凸包每条边的就离是否小于R。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = ;
const double eps = 1e-;
struct Point
{
double x,y;
} p[N];
Point c; ///圆心
double r; ///圆的半径
int n;
/**计算叉积*/
double cross(Point a,Point b,Point c)
{
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
/**判断叉积正负*/
int isCross(double k)
{
if(k<) return ;
return ;
}
/**是否为凸包*/
bool isConvex(int n)
{
p[n++]=p[];
p[n++]=p[];
int dir = isCross(cross(p[],p[],p[]));
for(int i=; i<n; i++)
{
if(isCross(cross(p[i-],p[i],p[i-]))!=dir)
{
return false;
}
}
return true;
}
/**判断点是否在多边形内(具有普遍性,不过并不能看懂)*/
bool pointInPolygon(int n,double x,double y)
{
int i,j=n-;
bool oddNodes=;
for (i=; i<n; i++)
{
if((p[i].y< y && p[j].y>=y|| p[j].y<y && p[i].y>=y) && (p[i].x<=x||p[j].x<=x))
{
oddNodes^=(p[i].x+(y-p[i].y)/(p[j].y-p[i].y)*(p[j].x-p[i].x)<x);
}
j=i;
}
return oddNodes;
}
/**判断点是否在凸多边形内(这个比较容易打并且适合这题)*/
bool pointInConvex(int n,double x,double y){
Point po;
p[n] = p[];
po.x = x,po.y = y;
bool flag = isCross(cross(p[],p[],po)); ///既然是判断了是凸多边形肯定相邻两个点是朝同一个方向转
for(int i=;i<n;i++){
if(isCross(cross(p[i],p[i+],po))!=flag){
return false;
}
}
return true;
}
///点积
double mult(Point a,Point b,Point c){
return (a.x-c.x)*(b.x-c.x)+(a.y-c.y)*(b.y-c.y);
}
///距离
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
///点c到线段ab的最短距离
double GetDist(Point a,Point b,Point c){
if(dis(a,b)<eps) return dis(b,c); ///a,b是同一个点
if(mult(b,c,a)<-eps) return dis(a,c); ///投影
if(mult(a,c,b)<-eps) return dis(b,c);
return fabs(cross(b,c,a)/dis(a,b)); }
/**判断圆是否在凸多边形内*/
bool circleInConvex(int n,double x,double y,double r){
Point po;
po.x = x,po.y = y;
p[n]=p[];
for(int i=;i<n;i++){
if(r>GetDist(p[i],p[i+],po)){
return false;
}
}
return true;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n>=)
{
scanf("%lf%lf%lf",&r,&c.x,&c.y);
for(int i=; i<n; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
if(!isConvex(n)) printf("HOLE IS ILL-FORMED\n");
else if(!circleInConvex(n,c.x,c.y,r)||!pointInConvex(n,c.x,c.y)){
printf("PEG WILL NOT FIT\n");
}else printf("PEG WILL FIT\n"); }
return ;
}

poj 1584(综合性强的计算几何,好题)的更多相关文章

  1. POJ 1061 青蛙的约会 数论水题

    http://poj.org/problem?id=1061 傻逼题不多说 (x+km) - (y+kn) = dL 求k 令b = n-m ; a = x - y ; 化成模线性方程一般式 : Lx ...

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

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

  3. POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道

    rt,计算几何入门: TOYS Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...

  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 计算几何

    思路: 求一遍凸包 用三角形面积(叉积求一下)/边长 求出来高,跟半径比一比 坑点:凸包上三点共线 //By SiriusRen #include <cmath> #include < ...

  6. 2018.07.04 POJ 1654 Area(简单计算几何)

    Area Time Limit: 1000MS Memory Limit: 10000K Description You are going to compute the area of a spec ...

  7. POJ 1066 Treasure Hunt(计算几何)

    题意:给出一个100*100的正方形区域,通过若干连接区域边界的线段将正方形区域分割为多个不规则多边形小区域,然后给出宝藏位置,要求从区域外部开辟到宝藏所在位置的一条路径,使得开辟路径所需要打通的墙壁 ...

  8. POJ 2398 Toy Storage(计算几何,叉积判断点和线段的关系)

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3146   Accepted: 1798 Descr ...

  9. hdu 4720 计算几何简单题

    昨天用vim练了一道大水题,今天特地找了道稍难一点的题.不过也不是很难,简单的计算几何而已.练习用vim编码,用gdb调试,结果居然1A了,没调试...囧... 做法很简单,无非就是两种情况:①三个巫 ...

随机推荐

  1. Alpha 冲刺5/10

    队名:我头发呢队 组长博客 杰(组长) 过去两天完成了哪些任务 继续翻阅Material Design2文档 翻阅网易云的web端网页 接下来的计划 音源爬取 还剩下哪些任务 app开发 燃尽图 有哪 ...

  2. 转---详细的Android开发环境搭建教程

    五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程 引言   在windows安装Android的开发环境不简单也说不上算复杂,本文写给第一次想在自己Windows上建立A ...

  3. lintcode-108-分割回文串 II

    108-分割回文串 II 给定一个字符串s,将s分割成一些子串,使每个子串都是回文. 返回s符合要求的的最少分割次数. 样例 比如,给出字符串s = "aab", 返回 1, 因为 ...

  4. 有关parent.frame.cols在firefox浏览器上不兼容的问题解决

    IE(不兼容FireFox): if(parent.myFrame.cols == "199,7,*") { parent.myFrame.cols="0,7,*&quo ...

  5. 【bzoj4976】宝石镶嵌 乱搞+dp

    题目描述 从$n$个数中选出$n-k$个,使得它们的二进制或(or)最大.输出这个值. 输入 第一行包含两个正整数$n,k(2\le n\le 100000,1\le k\le 100,k<n) ...

  6. BZOJ1975 SDOI2010魔法猪学院(启发式搜索+最短路+堆)

    对反图跑最短路求出每个点到终点的最短路径,令其为估价函数大力A*,第k次到达某个点即是找到了到达该点的非严格第k短路,因为估价函数总是不大于实际值.bzoj可能需要手写堆.正解是可持久化可并堆,至今是 ...

  7. 51nod 1779逆序对统计(状压DP)

    按照插入数的大小排序, 然后依次进行dp. 用一个状态表示n个数是否被选了 10110 就是表示第1.3.4个位置都选了 那么如果此时这个数该填到5这个位置,那么必定会造成一个逆序(因为下一个数会填到 ...

  8. 关于GDI+

    原文链接地址:http://www.2cto.com/kf/201107/97283.html 一 介绍 其实本人对GDI+不能算是专家,只是在几个小项目中应用了一些而已, 算是入门了. 刚好最近有点 ...

  9. 【BZOJ 3772】精神污染 主席树+欧拉序

    这道题的内存…………………真·精神污染……….. 这道题的思路很明了,我们就是要找每一个路径包含了多少其他路径那么就是找,有多少路径的左右端点都在这条路径上,对于每一条路径,我们随便选定一个端点作为第 ...

  10. 【COGS 2434】 暗之链锁 树上差分+LCA

    差分就是把一个值拆成许多差的和如 1 2 4 6 9 那么 把这个东西拆成 1 1 2 2 3 就是了,当然也可以理解为对一个问题分解为多个子问题并对其进行操作来得到原问题的答案. 树上差分就更玄妙了 ...