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 ...
随机推荐
- Android BitmapFactory
android BitmapFactory BitmapFactory是一个工具类,用于从不同数据源解析,创建Bitmap对象.bitmap类代表位图. BitmapFactory常用方法 stati ...
- am335x reboot 命令分析
本文记录am335x运行reboot命令时,内核中运行过程. Tony Liu, 2016-6-8, Shenzhen 参考链接: http://blog.csdn.net/wavemcu/artic ...
- Intellij IDEA 使用学习
Intellij中名词解释: Project,就是一个完整的项目,类似Eclipse中的WorkSet(虽然WorkSet是人为归类的). Module,是Project中的模块,类似Eclipse中 ...
- (转)S5pv210 HDMI 接口在 Linux 3.0.8 驱动框架解析 (By liukun321 咕唧咕唧)
作者:liukun321 咕唧咕唧 日期:2014.1.18 转载请标明作者.出处:http://blog.csdn.net/liukun321/article/details/18452663 本文 ...
- nodejs基础 -- 事件循环
Node.js 事件循环 Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用, ...
- c++ __int64
C++的64位整数[原]by 赤兔 在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648 ...
- linux 访问远程务器代码
比如用SSH 访问远程 登陆名为hadoop 的IP为192.168.1.35的主机,则用ssh hadoop@192.168.1.35,然后依据提示输入密码即可.
- andriod sdk 安卓模拟器修改imei码,位置信息
imei码就是手机卡的信息一段15位数字,就好像pc的mac地址.很多app注册会检测你是否是手机登录的,就会读取你的imei码,如果读取不到,就说明你可能是用平板等移动设备上网的. app也可以 ...
- jQuery checkbox选中问题之prop与attr注意点分析
$(function () { // 全选 $("#btnCheckAll").bind("click", function () { $(&q ...
- 彩色图像的直方图均衡化matlab代码
彩色图像的直方图均衡化 - YangYudong2014的专栏 - CSDN博客 http://blog.csdn.net/yangyudong2014/article/details/4051503 ...