Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4628   Accepted: 1434

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 OutputHOLE IS ILL-FORMED

PEG WILL NOT FIT

题意:给n个点的坐标,以及一个圆的圆心和半径。
   判断这n个点能否形成凸包,若不能输出“HOLE IS ILL-FORMED”,否则,再判断圆心以及整个圆是否在凸多边形内; 思路: 分三步;
先形成一个封闭的多边形,
1.判断是否是凸包,循环取相邻的两条边,作叉乘,若结果为负说明不是凸包; 2.判断圆心是否在凸多边形内, 用环顾法:

设圆心为P,逐条枚举n边形的边AB,利用 

计算PA和PB的夹角,最后求和得到的就是环顾角。

(1)       圆心在多边形内部时,环顾角=±360

(2)       圆心在多边形外部时,环顾角=0

(3)       圆心在多边形边上时(不包括顶点),环顾角=±180

(4)       圆心在多边形顶点时,环顾角为(0,360)之间的任意角,其实就是圆心所在的顶点的两条邻接边的夹角。

3.判断圆与多边形的关系

 当圆与多边形每条边的距离都小于半径时,说明圆在多边形内部。

 
 #include<stdio.h>
#include<string.h>
#include<math.h>
const double eps = 1e-;
const double PI = 3.1415926535898;
int cmp(double x)
{
if(fabs(x) < eps)
return ;
if(x > )
return ;
return -;
}
int n;//多边形点的个数
double r;//钉子半径 struct Point
{
double x,y;
Point (){}
Point(double a,double b):x(a),y(b) {}
}point[],p;//p为钉子坐标
//叉乘
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
//分别以a、b和 c、d为端点的两条线段的叉乘
double edge_det(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 dot(double x1,double y1,double x2,double y2)
{
return x1*x2 + y1*y2;
}
//分别以a、p和 b、p为端点的两条线段的点乘
double edge_dot(const Point &p, const Point &a, const Point &b)
{
return dot(a.x-p.x,a.y-p.y,b.x-p.x,b.y-p.y);
}
//两点间的距离
double dis(const Point &a, const Point &b)
{
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
//向量PA和PB 间的夹角
double cal_angle(const Point &a, const Point &b, const Point &p)
{
double res = edge_dot(p,a,b);
double L1 = dis(a,p);
double L2 = dis(b,p);
return acos(res/(L1*L2));
} bool is_convex_hull()//判断是否为凸包,任意两相临边作叉乘,若结果为负说明不是凸包;
{
double base = ;
for(int i = ; i < n; i++)
{
double tmp = cmp(edge_det(point[i],point[i+],point[i+],point[i+]));
if(!base)
base = tmp;
if(base * tmp < )
return false;
}
return true;
}
bool in_convex_hull()//判断圆心是否在凸包内
{
double angle = ;
for(int i = ; i <= n; i++)
{
if(cmp(edge_det(p,point[i],p,point[i+])) >= )
angle += cal_angle(point[i],point[i+],p);
else angle -= cal_angle(point[i],point[i+],p);
}
if(cmp(angle) == )//在多边形外部
return false;
if(cmp(angle+PI) == || cmp(angle-PI) == )//在多边形边上,不包括顶点。
{
if(cmp(r) == )
return true;
}
if(cmp(angle+*PI) == || cmp(angle-*PI) == )//在多边形内部;
return true;
else//在多边形顶点上;
{
if(cmp(r) == )
return true;
}
return false;
}
bool is_fit()//判断以r为半径的圆是否在凸包内
{
for(int i = ; i < n; i++)
{
double res = fabs(edge_det(point[i],p,point[i+],p)/dis(point[i],point[i+]));
if(cmp(res-r) < )
return false;
}
return true;
}
int main()
{
while(~scanf("%d",&n) && n >= )
{
scanf("%lf %lf %lf",&r,&p.x,&p.y); for(int i = ; i < n; i++)
scanf("%lf %lf",&point[i].x,&point[i].y); point[n] = point[];
point[n+] = point[]; if (!is_convex_hull())//不是一个凸包;
{
printf("HOLE IS ILL-FORMED\n");
continue;
}
bool f1 = in_convex_hull();
bool f2 = is_fit();
if(f1 && f2)
printf("PEG WILL FIT\n");
else printf("PEG WILL NOT FIT\n"); }
return ;
}

A Round Peg in a Ground Hole(判断是否是凸包,点是否在凸包内,圆与多边形的关系)的更多相关文章

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

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

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

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

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

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

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

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

  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. [转] Express 4 中的变化

    http://www.cnblogs.com/haogj/p/3985438.html 概览 从 Express 3 到Express 4 是一个巨大的变化,这意味着现存的 Express 3 应用在 ...

  2. Eclipse 每行 80 字符限制的提示线

    有时候希望eclipse和C++编辑器之类有条对齐线 打开 Eclipse, Windows -> Prefereces -> General -> Editors -> Te ...

  3. javascript新的原生态API

    以下是最新的w3c标准的javascript,目前支持运行在firefox, chrome,IE9以上版本的浏览器 参考资料:https://developer.mozilla.org/ru/docs ...

  4. listview的动态加载数据问题

    1:调用adapter.notifyDataSetChanged()却不起作用 原因可能有一下几点 1.数据源没有更新,调用notifyDataSetChanged无效. 2.数据源更新了,但是它指向 ...

  5. window.showModalDialog()复制内容

    ShowModalDialog 打开的 页面上加入个 <span id="mySpan" name="mySpan" contentEditable=&q ...

  6. Javascript实现Web颜色值转换

    最近一直忙碌于完成业务需求,好长时间没有写博客了.今天稍微有些时间,翻看了一下最近项目中的一些前端代码,看到Web颜色转换功能的时候,突然想到当我们在做一些颜色设置/编辑的需求时,经常会涉及到各种颜色 ...

  7. C# 线程间互相通信

    C#线程间互相通信主要用到两个类:AutoResetEvent和ManualResetEvent. 一.AutoResetEvent AutoResetEvent 允许线程通过发信号互相通信,线程通过 ...

  8. 寒假挑战PythonTip(一人一python)总结——算法是程序的灵魂,程序员的心法

        2014年2月中旬,我上升到挑战python英雄榜第3名.这是我寒假修炼算法的成果之一.来一下总结吧! Linux的创始人Linus Torvalds在一次演讲中有一段涉及“什么才是优秀程序员 ...

  9. Linux技巧总结(个人经验版)

    1:善用桌面:1.图形界面的编辑,2.终端只要开机就在第2桌面,3.浏览器在第3桌面,4.娱乐在第4桌面. 2:cd命令中,输入中文目录很不方便,用 ln -s 桌面 desktop 创建软链接,不必 ...

  10. CentOS 忘记root密码,解决方法

    1.开机后,在倒数5秒结束前,按下任意键 2.在显示centos...的那个界面下,按e键(edit) 3.会出现三行的界面,选择中间 kernel...那行,然后按e键 4.在接着出现的那个界面最后 ...