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

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

题意是按照一定顺序(顺时针或是逆时针)给定一些点,问这些点组成的图形是不是凸包。如果不是,输出“HOLE IS ILL-FORMED”。如果是,又有一个圆,问该圆是否在凸包里面。在里面,输出“PEG WILL FIT”。不在里面,输出“PEG WILL NOT FIT”。

自己对于输入过来的点,就看输入过来的点 叉积 是不是一直大于零,或是一直小于零。

然后对于圆心是不是在凸包里面,我的判断方法是计算面积。如果以圆心、凸包上的两个点为三条形的面积总和与凸包的总面积相等,那这个点一定在凸包里面。否则就在外面。

至于半径那部分,就是计算点到直线的距离,判断与半径之间的关系。

折磨了我整整一个上午。。。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct no
{
double x, y;
}node[2005], peg, orign; int n;
double peg_r; double dis(no n1, no n2)
{
if (n1.x == n2.x)
{
return fabs(n1.x - peg.x);
}
else
{
double k = (n2.y - n1.y) / (n2.x - n1.x);
double b = n2.y - k*n2.x;
return fabs(k*peg.x - peg.y + b) / sqrt(k*k + 1);
}
} double xmult(double x1, double y1, double x2, double y2)
{
return x1*y2 - x2*y1;
} double Across(no n1, no n2, no n3, no n4)
{
return xmult(n2.x - n1.x, n2.y - n1.y, n4.x - n3.x, n4.y - n3.y);
} bool convex()
{
int i;
double res, sign = 0;
for (i = 0; i < n; i++)
{
res = Across(node[i%n], node[(i + 1) % n], node[(i + 1) % n], node[(i + 2) % n]);
if (sign == 0)
{
sign = res;
}
else if (sign > 0)
{
if (res < 0)
return true;
}
else if (sign < 0)
{
if (res > 0)
return true;
}
}
return false; } int main()
{
int i, pos_x;
double min_x; while (cin >> n)
{
if (n < 3)
break;
cin >> peg_r >> peg.x >> peg.y;
min_x = 100005; for (i = 0; i < n; i++)
{
cin >> node[i].x >> node[i].y;
if (node[i].x < min_x)
{
min_x = node[i].x;
pos_x = i;
}
else if (min_x == node[i].x&&node[i].y < node[pos_x].y)
{
pos_x = i;
}
}
orign = node[pos_x]; if (convex())
{
cout << "HOLE IS ILL-FORMED" << endl;
}
else
{
int sign = 1;
double sum1 = 0;
for (i = 0; i<n; ++i)
{
sum1 += fabs(((node[i%n].x - node[1].x) * (node[(i + 1) % n].y - node[1].y) - (node[i%n].y - node[1].y) * (node[(i + 1) % n].x - node[1].x)));
} double sum2 = 0; for (i = 0; i < n; ++i)
{
sum2 += fabs(((node[i%n].x - peg.x) * (node[(i + 1) % n].y - peg.y) - (node[i%n].y - peg.y) * (node[(i + 1) % n].x - peg.x)));
} if (sum1 == sum2)
{
sign = 0;
} if (sign == 1)
{
cout << "PEG WILL NOT FIT" << endl;
}
else
{
double len;
sign = 0;
for (i = 0; i < n; i++)
{
len = dis(node[i%n], node[(i + 1) % n]);
if (len < peg_r)
{
sign = 1;
break;
}
}
if (sign == 1)
{
cout << "PEG WILL NOT FIT" << endl;
}
else
{
cout << "PEG WILL FIT" << endl;
}
}
}
} return 0;
}

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

POJ 1584:A Round Peg in a Ground Hole的更多相关文章

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

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

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

  3. A Round Peg in a Ground Hole(凸包应用POJ 1584)

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

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

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

  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: 6682   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. A Round Peg in a Ground Hole(判断是否是凸包,点是否在凸包内,圆与多边形的关系)

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

  9. poj1584 A round peg in a ground hole【计算几何】

    含[判断凸包],[判断点在多边形内],[判断圆在多边形内]模板  凸包:即凸多边形 用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点. The ...

随机推荐

  1. Linux CentOS7 VMware 相对和绝对路径、cd命令、mkdir/rmdir、rm命令——笔记

    一. 相对和绝对路径 绝对路径是从/(也被称为根目录)开始的,比如/usr.cd /root/ pwd 注:判断用户当前所处的位置 相对路径是以 . 或 .. 开始的 二.cd命令 cd 是进入到当前 ...

  2. CH10 泛型算法

    概述 大多数算法都定义在algorithm头文件中. Note:算法永远不会执行容器操作 泛型算法本身不会执行容器的操作,而是通过迭代器来访问.修改等操作 10.1 题目要求读取数据存入vector, ...

  3. 「NOIP2013」华容道

    传送门 Luogu 解题思路 预支一点东西: 这题其实有着更为思维的图模型,还十分考验码力,不简单啊 这居然是联赛题 讲正解: 显然我们对于一种合法方案,空格子肯定是一直围绕着特定棋子反复横跳的. 所 ...

  4. 5.4 Linux 安装2个tomcat

    Linux系统下怎样配置多个Tomcat同时运行呢,首先第一个tomcat配置不变,然后修改第二个tomcat启动的脚本 拷贝第一个tomcat的目录到第二个tomcat目录 [root@eshop- ...

  5. 每个项目中,你必须知道的11个Java第三方类库。

    Java第三方library ecosystem是一个很广阔的范畴.不久前有人撰文:每个项目中,你必须知道的11个Java第三方类库. 单元测试 1.DBUnit DBunit是一个基于junit扩展 ...

  6. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

  7. Matplotlib 教程

    Matplotlib是python中最流行的数据绘图库,使用matplotlib,您可以绘制任何类型的图形. 本教程的目标是让您轻松学会使用matplotlib绘制复杂的图形. 预备知识 熟悉Pyth ...

  8. 使用 Helm【转】

    Helm 安装成功后,可执行 helm search 查看当前可安装的 chart. 这个列表很长,这里只截取了一部分.大家不禁会问,这些 chart 都是从哪里来的? 前面说过,Helm 可以像 a ...

  9. Tomcat删除时问题——eclipse部署tomcat时弹出Resource'/Servers' does not exist

    如果你删除一个项目的Servers文件,或者相应文件损坏等,会出现错误, Resource '/Servers' does not exist 那么就需要把它在控制台出的Servers下所部署的Tom ...

  10. 解析基于keras深度学习框架下yolov3的算法

    一.前言 由于前一段时间以及实现了基于keras深度学习框架下yolov3的算法,本来想趁着余热将自己的心得体会进行总结,但由于前几天有点事就没有完成计划,现在趁午休时间整理一下. 二.Keras框架 ...