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

凸包:即凸多边形

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

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. C# 如何使用NPOI操作Excel以及读取合并单元格等

    C#操作Excel方法有很多,以前用的需要电脑安装office才能用,但因为版权问题公司不允许安装office.所以改用NPOI进行Excel操作,基本上一些简单的Excel操作都没有问题,读写合并单 ...

  2. C++函数的高级特性

    对比于 C 语言的函数,C++增加了重载(overloaded).内联(inline).const 和 virtual 四种新机制.其中重载和内联机制既可用于全局函数也可用于类的成员函数,const ...

  3. win7语音识别开发(sapi)

    参考:http://msdn.microsoft.com/en-us/library/ee125663(v=vs.85).aspx    (sapi5.4 reference) http://msdn ...

  4. QWidget:Must construct a QApplication before a QWidget。

    异常描述: 用PyQt开发的界面程序,再新增加了几个module后, 在eric6开发环境下启动后什么都没出现,什么错误提示也都没有, 在控制台下:python   XXXX.py 后显示 QWidg ...

  5. 【Java面试题】33 HashMap和Hashtable的区别

    1 HashMap不是线程安全的 hastmap是一个接口 是map接口的子接口,是将键映射到值的对象,其中键和值都是对象,并且不能包含重复键,但可以包含重复值.HashMap允许null key和n ...

  6. 在Delphi中编写res文件

    delphiimagedosinterfaceborland脚本先用记事本编写一个rc的文件. 如内容为: _Comms RCData Comms.jpg Comms.jpg为图片名称, 然后在这个r ...

  7. xampp更改网站存放目录

    改完后重启xampp 如何更改监听端口8080

  8. PHP大小写是否敏感问题

    一.大小写敏感 1. 变量名区分大小写 所有变量均区分大小写,包括普通变量以及$_GET,$_POST,$_REQUEST,$_COOKIE,$_SESSION,$GLOBALS,$_SERVER,$ ...

  9. Git高级操作

    本文是在Git操作指南基础上衍生出来的高级操作,如果你对git不是很熟悉,建议你先阅读Git操作指南. 一.忽略提交特定文件 如果你不想让一些文件上传到git仓库中,可以让Git忽略特定文件或是目录, ...

  10. spring xml properties split with comma for list

    在注入spring bean 属性值的时候常常会用到list, 一般使用方式例如以下: <bean id="testBean" class="com.mytest. ...