poj1584 A round peg in a ground 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 (x i+1, y i+1) for i = 1 . . . n − 1 and (xn, yn) to (x1, y1).
Input
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
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
题意:
给定n个点 这n个点组成一个多边形
给定一个peg的坐标和半径
首先判断这个多边形是不是凸多边形 若不是 输出“HOLE IS ILL-FORMED”
否则判断peg和多边形的关系 若peg所代表的圆在多边形内部输出“PEG WILL FIT”
否则输出“PEG WILL NOT FIT”
思路:
首先将n个点构造成封闭图形,判断是不是一个凸包
求连续两条边的叉乘,如果正负号与之前的出现了不同,说明不是凸包
再判断圆心与多边形的关系
设圆心为P,逐条枚举n边形的边AB,利用

计算PA和PB的夹角,最后求和得到的就是环顾角。
(1) 圆心在多边形内部时,环顾角=±360
(2) 圆心在多边形外部时,环顾角=0
(3) 圆心在多边形边上时(不包括顶点),环顾角=±180
(4) 圆心在多边形顶点时,环顾角为(0,360)之间的任意角,其实就是圆心所在的顶点的两条邻接边的夹角。
最后判断整个圆是否在多边形内部
只需要求出圆心到边的最短距离 若大于半径则在多边形内
设圆心为P,逐条枚举n边形的边AB,利用
得到△PAB的面积,
再根据公式S=0.5*|AB|*h,可以得到
枚举所有h与圆的半径R比对,只要所有的边都有h - R>=0,则说明圆在多边形内
//#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h> using namespace std;
typedef long long int LL; const double eps = 1e-;
const double pi = 3.141592654;
int n;
double radius;
struct point{
double x, y;
}peg; int precision(double x)
{
if(fabs(x) <= eps){
return ;
}
return x > ? : -;
} double dotdet(double x1, double y1, double x2, double y2)
{
return x1 * x2 + y1 * y2;
} double det(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
} double cross(point a, point b, point c, point d)
{
return det(b.x - a.x, b.y - a.y, d.x - c.x, d.y - c.y);
} double distant(point a, point b)
{
return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
} double angle(point a, point b, point p)
{
return acos(dotdet(a.x - p.x, a.y - p.y, b.x - p.x, b.y - p.y) / (distant(a, p) * distant(b, p)));
} bool isconvex(point *vectex)
{
int direction = ;
//1, 逆时针;-1, 顺时针
for(int i = ; i < n; i++){
int temp = precision(cross(vectex[i], vectex[i + ], vectex[i + ], vectex[i + ])); if(!direction){
direction = temp;
}
if(direction * temp < ){
return false;
}
}
return true;
} bool is_in(point *vectex)
{
double circleAngle = 0.0;
for(int i = ; i <= n; i++){
if(precision(cross(peg, vectex[i], peg, vectex[i + ])) >= ){
circleAngle += angle(vectex[i], vectex[i + ], peg);
}
else{
circleAngle -= angle(vectex[i], vectex[i + ], peg);
}
} if(precision(circleAngle) == ){
return false;
//peg在多边形外部
}
else if(precision(circleAngle - pi) == || precision(circleAngle + pi) == ){
//peg在多边形边上
if(precision(radius) == ){
return true;
}
}
else if(precision(circleAngle - * pi) == || precision(circleAngle + * pi) == ){
return true;
}
else{
//peg在多边形顶点上
if(precision(radius) == ){
return true;
}
}
return false;
} bool isfit(point *vectex)
{
for(int i = ; i <= n; i++){
int k = precision(fabs(cross(peg, vectex[i], peg, vectex[i + ]) / distant(vectex[i], vectex[i + ])) - radius);
if(k < ){
return false;
}
}
return true;
} int main()
{
while(scanf("%d", &n) != EOF && n >= ){
cin>> radius >> peg.x >> peg.y;
point *vectex = new point[n + ]; for(int i = ; i <= n; i++){
cin>>vectex[i].x >> vectex[i].y;
} //构成封闭多边形
vectex[].x = vectex[n].x;
vectex[].y = vectex[n].y;
vectex[n + ].x = vectex[].x;
vectex[n + ].y = vectex[].y; if(!isconvex(vectex)){
cout<<"HOLE IS ILL-FORMED"<<endl;
}
else{
bool flag1 = is_in(vectex);
bool flag2 = isfit(vectex); if(flag1 && flag2){
cout<<"PEG WILL FIT"<<endl;
}
else{
cout<<"PEG WILL NOT FIT"<<endl;
}
}
delete vectex;
}
return ;
}
poj1584 A round peg in a ground hole【计算几何】的更多相关文章
- POJ1584 A Round Peg in a Ground Hole 凸包判断 圆和凸包的关系
POJ1584 题意:给定n条边首尾相连对应的n个点 判断构成的图形是不是凸多边形 然后给一个圆 判断圆是否完全在凸包内(相切也算) 思路:首先运用叉积判断凸多边形 相邻三条边叉积符号相异则必有凹陷 ...
- poj1584 A Round Peg in a Ground Hole 判断多边形凹凸,点到线的距离【基础计算几何】
大致思路:首先对于所给的洞的点,判断是否是凸多边形,图形的输入和输出可以是顺时针或者逆时针,而且允许多点共线 Debug 了好几个小时,发现如下问题 判断三点是否共线,可用斜率公式判断 POINT p ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- mysql 中查看指定表的字段名 (可根据字段变量生成c#后台代码)
select DISTINCT data_type from COLUMNS where table_name='表名' 用ConCat();构造生成代码.....
- uniqid,md5,microtime
<?php header("content-type:text/html;charset=utf-8"); $str = uniqid(md5(microtime(true) ...
- [hibernate]org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter
org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type sette ...
- ubuntu普通用户无法使用usdo命令
1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是: chmod u+w /etc/sudoers 3.编辑sudoers文件 vi /etc/s ...
- 零基础的人怎么学习Java
编程语言Java,已经21岁了.从1995年诞生以来,就一直活跃于企业中,名企应用天猫,百度,知乎......都是Java语言编写,就连现在使用广泛的XMind也是Java编写的.Java应用的广泛已 ...
- Effective C++ Item 33 Avoid hiding inherited names
class Base { private: int x; public: ; virtual void mf2(); void mf3(); ... }; class Derived: public ...
- ios开发之--iOS 11适配:iOS11导航栏返回偏移
UIBarButtonItem 左边间隙过大,解决方案(ios11之前): 调用下面的方法,设置negativeSpacer.width = -15;就可以解决间隙过大的问题: UIBarButton ...
- Android NDK开发-1-环境搭建
1.NDK介绍 Android NDK 是在SDK前面又加上了“原生”二字,即Native Development Kit,因此又被Google称为“NDK”.众所周知,Android程序运行在Dal ...
- grep递归查找子目录
想要在各种文件里面找一个指定的文本,本来的方法太土了,在网上搜了一下,发现个好的方法,不过也有些问题.原文如下: 第一个,这个是看别人脚本的,配合find实现,-maxdepth指定深度,如果查找到底 ...
- RecyclerView的通用适配器,和滚动时不加载图片的封装
对于RecyclerView我们需要使用RecyclerAdapter,使用方式与ListViewAdapter类似,具体代码大家可以在网上搜索,这里就只教大家使用封装后的简洁RecyclerAdap ...