POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,判断圆是否在凸多边形内)
来源:互联网 发布:人肉软件 app 编辑:程序博客网 时间:2024/05/21 15:02
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).
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.
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
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.01.0 1.02.0 2.01.75 2.01.0 3.00.0 2.05 1.5 1.5 2.01.0 1.02.0 2.01.75 2.51.0 3.00.0 2.01
Sample Output
HOLE IS ILL-FORMEDPEG WILL NOT FIT
题意:让你判断一个多边形是否为凸多边形,如果是,那么再判断给定的圆是否在这个凸多边形内。
判断是否为凸多边形,可以用叉积来判断,如果一个多边形上存在连续的三条边,l1,l2,l3(方向得是一致的,都是顺时针或者都是逆时针),(l1 X l2) *(l2 X l3)的值为负,那么这个多边形就是凹多边形,否则就为凸多边形。(自己画一下图就能看出来了)
判断完如果是凸多边形,就要判断圆是否在凸多边形内。首先判断圆心在不在凸多边形内,可以用圆心和多边形上相邻的两条边叉积的符号来判断,如果两个叉积的符号相同,就说明圆心在这两条线段的同一边,如果符号都相同,那么圆心就在多边形内。
如果圆心在多边形内,就判断圆的半径与圆心到线段的距离关系就可以了。
点到线段的距离可以这样求:
点和线段的两个端点组成一个三角形,面积S = 1/2 * (PA X PB),S = 1/2 * |AB|*h,所以h = (PA X PB) / |AB|
#include<cmath>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const double eps = 1e-8;struct Point{ double x,y; Point(){} Point(double _x,double _y) { x = _x;y = _y; } Point operator -(const Point &b)const { return Point(x - b.x,y - b.y); } double operator *(const Point &b)const { return x*b.x + y*b.y; } double operator ^(const Point &b)const { return x*b.y - y*b.x; }};double dis(Point a,Point b){ return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}double xmult(Point p0,Point p1,Point p2){ return (p1-p0)^(p2-p0);}bool judge_convex(vector<Point> a){ for(int i=0;i<a.size();i++) { int i1 = (i+1)%a.size(); int i2 = (i+2)%a.size(); int i3 = (i+3)%a.size(); if(((a[i1]-a[i])^(a[i2]-a[i1])) * ((a[i2]-a[i1])^(a[i3]-a[i2])) < -eps) return false; } return true;}bool judge_Circle_in_Polygon(Point p,double r,vector<Point> a){ for(int i=0;i<a.size();i++)//判断圆心在不在多边形内 { int i1 = (i+1)%a.size(); int i2 = (i+2)%a.size(); if(xmult(p,a[i],a[i1])*xmult(p,a[i1],a[i2]) < eps) return false; } for(int i=0;i<a.size();i++)//判断半径与圆心到边的距离的关系 { int i1 = (i+1)%a.size(); double h = fabs(xmult(p,a[i],a[i1]))/dis(a[i],a[i1]); if(h - r < -eps) return false; } return true;}vector<Point> v;int main(void){ int n,i,j; double r,x,y; while(scanf("%d",&n)==1) { if(n < 3) break; scanf("%lf%lf%lf",&r,&x,&y); Point p(x,y); v.clear(); for(i=0;i<n;i++) { scanf("%lf%lf",&x,&y); v.push_back(Point(x,y)); } if(!judge_convex(v)) printf("HOLE IS ILL-FORMED\n"); else { if(judge_Circle_in_Polygon(p,r,v)) printf("PEG WILL FIT\n"); else printf("PEG WILL NOT FIT\n"); } } return 0;}
阅读全文
0 0
- poj 1584 A Round Peg in a Ground Hole(判断凸多边形+圆是否在凸多边形内)
- POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,判断圆是否在凸多边形内)
- poj 1584 A Round Peg in a Ground Hole 判断多边形是否为凸多边形 + 圆心是否在凸多边形内 + 圆是否在凸多边形内部
- POJ1584 A Round Peg in a Ground Hole 判断多边形是否为凸多边形,判断凸多边形是否在圆内
- POJ 1584 A Round Peg in a Ground Hole (判断凸多边形模板)
- JOJ 1984: A Round Peg in a Ground Hole (判断点在凸多边形内)
- poj_1584 A Round Peg in a Ground Hole(凸包判断凸多边形+判断点在多边形内)
- POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段最短距离)
- POJ 1584 A Round Peg in a Ground Hole(凸多边形判断+点与多边形的位置关系)
- A Round Peg in a Ground Hole(POJ1584点在多边形内+是否为凸多边形)
- poj1584 A Round Peg in a Ground Hole 凸多边形判断 和多边形与圆的位置
- POJ 1584:A Round Peg in a Ground Hole _判断点是否在多边形内(5种方法)
- POJ-1584-A Round Peg in a Ground Hole-计算几何-凸多边形+多边形包含圆
- POJ 1584 A Round Peg in a Ground Hole(判断是否是凸包以及圆是否完全在凸包内)
- POJ 1584 A Round Peg in a Ground Hole(是否凸包 && 圆是否在凸包内)
- POJ 1584 A Round Peg in a Ground Hole 圆是否包含在凸包内
- poj1584--A Round Peg in a Ground Hole(判断凸包,并且判断圆是否在凸包内)
- poj 1584A Round Peg in a Ground Hole(计算几何 判断凸包)
- c实现系统内存对齐
- 六月英语
- linux qgc编译提示缺少user_config.pri
- 安装jdk后出现bash: ./java: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
- 机器视觉之 ICP算法和RANSAC算法
- POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,判断圆是否在凸多边形内)
- 2 浅谈Web开发中的“前后端分离”
- C语言来实现字符串反转 只有单词顺序反转,单词里的字母不反转
- java 插入背景图片的三种方法
- 《算法》笔记-2.5 应用
- 如何在mac上配置vsftpd
- leetcode No117. Populating Next Right Pointers in Each Node II
- Java语言基础——数据类型与数据类型转换
- PHP上传文件