POJ 1584 A Round Peg in a Ground Hole
来源:互联网 发布:eclipse java tomcat 编辑:程序博客网 时间:2024/05/18 02:14
A Round Peg in a Ground Hole
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4240 Accepted: 1293
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
Source
Mid-Atlantic 2003
注意数据给出的顺序,先是点数,然后是半径,再然后才是圆心坐标。
这个题目要判断的是 是不是凸包, 圆心是否在多边形内, 圆是否完全在多边形内;
(1)判断凸包: 因为点是顺序给出的相邻两条边求叉乘 ,看符号是否一致, 结果等于0的情况忽略。 符号都一致则是凸包,否则不是。
(2)判断圆心是否在多边形内: 圆心如果在多边形内或者边上,相邻两条边的公共点与圆心构成的向量分别与这两条边构成的向量求叉乘, 结果应该异号,忽略结果=0的情况
(3)判断圆是否在多边形内: 圆心到边上的距离(h) 应该 >= 半径。 求圆心到边上的距离(h)可以推倒一下公式: 假如线上两点坐标 是(x1 ,y1)(x2,y2) 圆心坐标是(x3 ,y3).显然这三个点可以构成一个三角形,可以得出 (x1-x3,y1-y3) (x2-x3,y2-y3) 这两个向量 而向量的叉乘的结果取绝对值就是 以这两条临边构成的平行四边形的面积。 则三角形的面积s = 平行四边形的面积/2 = (x1-x3,y1-y3)(x2-x3,y2-y3)/2 ;
三角形的面积还等于 l(直线的距离)*h(圆心到直线的距离)/2 l=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
h 为 所求,联立一下求就可以了。h=(x1-x3,y1-y3)(x2-x3,y2-y3) / l
#include <stdio.h>#include <string.h>#include <math.h>struct{ double x,y;}map[1000000];int chacheng(double x1,double y1,double x2,double y2){ double t= x1 * y2 - y1 * x2; if(fabs(t)<=1e-7) { return 0; }else if(t>0) { return 1; }else { return -1; }}int main(){ int i,j,n,m,s,t,pos1,pos2,pos3,tag,k,t1,t2; double peg_x,peg_y,peg_r,x1,y1,x2,y2,dis,S; while(scanf("%d",&n)!=EOF) { if(n<=2) { break; } scanf("%lf %lf %lf",&peg_r,&peg_x,&peg_y); for(i=0;i<=n-1;i++) { scanf("%lf %lf",&map[i].x,&map[i].y); } for(i=0,k=0;i<=n-1;i++) { pos1=i; pos2=(i+1)%n; pos3=(i+2)%n; x1 = map[pos2].x - map[pos1].x; y1 = map[pos2].y - map[pos1].y; x2 = map[pos3].x - map[pos2].x; y2 = map[pos3].y - map[pos2].y; t = chacheng(x1,y1,x2,y2); if(t!=0&&k==0) { tag=t; k=1; }else if(t!=0&&k==1) { if(tag!=t) { break; } } } if(i!=n) { printf("HOLE IS ILL-FORMED\n"); continue; } for(i=0;i<=n-1;i++) { pos1 = i; pos2 = (i+1)%n; pos3 = (i+2)%n; x1= peg_x - map[pos2].x; y1= peg_y - map[pos2].y; x2= map[pos1].x - map[pos2].x; y2= map[pos1].y - map[pos2].y; t1= chacheng(x1,y1,x2,y2); x2= map[pos3].x - map[pos2].x; y2= map[pos3].y - map[pos2].y; t2=chacheng(x1,y1,x2,y2); if(t1*t2>0) { break; } } if(i!=n) { printf("PEG WILL NOT FIT\n"); continue; } for(i=0;i<=n-1;i++) { pos1=i; pos2=(i+1)%n; x1= map[pos1].x - peg_x; y1= map[pos1].y - peg_y; x2= map[pos2].x - peg_x; y2= map[pos2].y - peg_y; S= fabs((x1 * y2 - y1 * x2)); x1= map[pos1].x - map[pos2].x; y1= map[pos1].y - map[pos2].y; dis= sqrt(x1 * x1 + y1 * y1); if(S/dis<peg_r) { break; } } if(i==n) { printf("PEG WILL FIT\n"); }else { printf("PEG WILL NOT FIT\n"); } } return 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
- 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
- POJ 1584 A Round Peg in a Ground Hole
- A Round Peg in a Ground Hole - POJ 1584
- 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]题解
- POJ 1584.A Round Peg in a Ground Hole
- A Round Peg in a Ground Hole
- poj1584A Round Peg in a Ground Hole
- poj 1584 A Round Peg in a Ground Hole(判断凸多边形+圆是否在凸多边形内)
- POJ 1584 A Round Peg in a Ground Hole 圆是否包含在凸包内
- JS等比例缩小图片尺寸
- vc++ 学习笔记
- POJ 1511 Invitation Cards (SPFA)
- MySQL 批量模式
- Android属性之build.prop,及property_get/property_set
- POJ 1584 A Round Peg in a Ground Hole
- 时隔两年再读《编程之美》
- 深入剖析Android消息机制
- JS分页
- Eclipse和Maven的结合使用--Please make sure the -vm option in eclipse.ini
- javascript 日期时间转换
- ROWID
- 浅淡管理风格
- MFC CListCtrl的用法