判断是否是凸多边形,判断点是否在多边形内,点到直线的距离

来源:互联网 发布:云服务器软件 编辑:程序博客网 时间:2024/05/16 11:07
B - A Round Peg in a Ground Hole
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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

题目大意:

顺时针或逆时针给出n个点,问这n个点围成的图形是不是个凸多边形。如果是凸多边形,给一个圆,以圆心的坐标和半径表示,问这个圆是不是完全在凸多边形内部。

解题思路:

1、先判断是不是凸多边形。我们可以通过两个向量的叉积来判断第三个点在前两个点的什么方向。(见《算法艺术与信息学竞赛》第三章)

因为n个点给出时的方向不确定(有可能是顺时针有可能是逆时针),只要相邻两个向量叉积出来的正负是相同的,或者某一个是0,一定是在相同方向.(叉积是0表示两向量共线)。

2、再判断点是否在凸多边形内。如果在凸多边形内的话,对于枚举凸多边形上的每一个点和它的下一个点所组成的向量,与这个点与圆心组成的向量的叉积符号一定是相同的,因为对于凸多边形来说,如果某一个点在其内部的话,一定在其所有边的相同一侧。

3,最后判断圆心到每个边距离的最小值是否大于圆的半径。这个要用到点到直线距离的公式。

4、完成判断后满足什么要求输出什么就行了。

#include<stdio.h>  #include<iostream>  #include<string>  #include<algorithm>  #include<string.h>  #include<map>   #include<math.h>  #include<set>  #include<queue>  #include<vector>  #include<stack>  using namespace std;  struct node{double x,y;}f[10005];double x0,y00,r;double f1(int i,int j,int k)//判断是否是凸多边形 {double x1,x2,y1,y2;x1=f[j].x-f[i].x;y1=f[j].y-f[i].y;x2=f[k].x-f[i].x;y2=f[k].y-f[i].y; return x1*y2-x2*y1;}double fs(int j,int k)//计算多边形的面积 {    double s;    double x1,x2,y1,y2;x1=f[j].x-f[1].x;y1=f[j].y-f[1].y;x2=f[k].x-f[1].x;y2=f[k].y-f[1].y;s=fabs(x1*y2-x2*y1)*0.5;return s;}double fs1(int j,int k)//判断点是否在多边形内部 {    double s;    double x1,x2,y1,y2;x1=f[j].x-x0;y1=f[j].y-y00;x2=f[k].x-x0;y2=f[k].y-y00;s=fabs(x1*y2-x2*y1)*0.5;return s;}double l(double x1,double y1,double x2,double y2)//两点间距离 {return sqrt(pow(x1-x2,2)+pow(y1-y2,2));}double fs2(int j,int k)//点到直线的距离 {    double s;    double x1,x2,y1,y2;x1=f[j].x-x0;y1=f[j].y-y00;x2=f[k].x-x0;y2=f[k].y-y00;s=fabs(x1*y2-x2*y1)/l(f[j].x,f[j].y,f[k].x,f[k].y);return s;}int main(){int n,flag,i;double x,y;while(~scanf("%d",&n)&&n>=3){scanf("%lf%lf%lf",&r,&x0,&y00);bool b=false,pre,biao=true;flag=0;for(i=1;i<=n;i++){  scanf("%lf%lf",&f[i].x,&f[i].y);}f[n+1]=f[1];f[n+2]=f[2];double a=0;int flag=0;for(i=1;i<=n;i++){a=f1(i,i+1,i+2);if(a!=0){if(flag==0){if(a<0)pre=true;elsepre=false;flag=1;}else{if(a<0)b=true;elseb=false;if(b!=pre){biao=false;break;}}  }     }if(!biao){printf("HOLE IS ILL-FORMED\n");}else{double s=0,s1=0;for(i=2;i<=n;i++){s+=fs(i,i+1);}for(i=1;i<=n;i++){s1+=fs1(i,i+1);} if(s1!=s){printf("PEG WILL NOT FIT\n");}else{flag=0;for(i=1;i<=n;i++){if(fs2(i,i+1)<r){flag=1;break;}}if(flag){printf("PEG WILL NOT FIT\n");}elseprintf("PEG WILL FIT\n");}}}}

原创粉丝点击