含【判断凸包】,【判断点在多边形内】,【判断圆在多边形内】模板 

凸包:即凸多边形

用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点。

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 (x i+1, y i+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

题意:

给定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【计算几何】的更多相关文章

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

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

  2. poj1584 A Round Peg in a Ground Hole 判断多边形凹凸,点到线的距离【基础计算几何】

    大致思路:首先对于所给的洞的点,判断是否是凸多边形,图形的输入和输出可以是顺时针或者逆时针,而且允许多点共线 Debug 了好几个小时,发现如下问题 判断三点是否共线,可用斜率公式判断 POINT p ...

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

随机推荐

  1. 写给大忙人的JavaSE 8 - 学习

    前面有提到过lambda和函数式接口,但是JavaSE 8 除了这两个新特性之后还提供了很多有用的东西.例如Stream. 摸索了几天,终于弄明白Stream的应用了. 先推荐一篇文章:Java 8 ...

  2. (转)_declspec(dllexport)

    先看代码:以下是在dev-c++里建立自已的dll时的dll.h里面的代码,这里面有一个:_declspec(dllexport) #ifndef _DLL_H_#define _DLL_H_//防重 ...

  3. perl 函数的参数列表

    在perl中,定义一个函数的时候,不需要在圆括号内指定具体的参数,所有的参数都从@_ 这个列表中得到 代码示例: sub test { my ($a, $b) = @_; print qq{$a\t$ ...

  4. js 查找指定函数的内容

    function test(){  //hahahhahahhahahha }alert(test.toString());

  5. 利用PHPExcel导出Excel相关设置

    功能包括: 1.设置单元格格式,包括单元格边框.单元格高度.单元格宽度 2.合并指定的单元格 3.设置Excel数据源,并将数据源保护起来(这个是为了实现单元格下拉选项功能) 4.设置字体样式 pub ...

  6. 小明A+B(杭电2096)

    /*小明A+B Problem Description 小明今年3岁了, 如今他已经可以认识100以内的非负整数, 而且可以进行100以内的非负整数的加法计算. 对于大于等于100的整数, 小明仅保留 ...

  7. Linux安装php运行环境

    安装apache: yum install httpd httpd-devel  启动apache: /etc/init.d/httpd start 此时输入服务器的IP地址,应该看到apache的服 ...

  8. Maven------pom.xml自动加载各种类库代码

    转载: http://lavasoft.blog.51cto.com/62575/1388866/ 一般要加<type>jar</type> <dependency> ...

  9. 实现Runnable接口和继承Thread类区别

    如果一个类继承Thread,则不适合资源共享.但是如果实现了Runable接口的话,则很容易的实现资源共享. 实现Runnable接口比继承Thread类所具有的优势: 1):适合多个相同的程序代码的 ...

  10. grep递归查找子目录

    想要在各种文件里面找一个指定的文本,本来的方法太土了,在网上搜了一下,发现个好的方法,不过也有些问题.原文如下: 第一个,这个是看别人脚本的,配合find实现,-maxdepth指定深度,如果查找到底 ...