hunnu11325(点到多边形的距离)

首先判断点是否在多边形上,然后求点到多边形每条边的最短距离

代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define N 105
#define inf 0xffffffff
#define eps 1e-9
#define pi acos(-1.0)
#define P system("pause")
using namespace std;
struct point 
{
     double x,y;     
     point(double a,double b){x=a;y=b;}
     point (){};
}p[N],A;
int n;
point operator - (point A,point B)
{
           return point(A.x-B.x,A.y-B.y);       
}
double dot(point A,point B)
{
    return A.x*B.x+A.y*B.y;       
}
double cross(point A,point B)
{
    return A.x*B.y-A.y*B.x;       
}
int dcmp(double x)
{
    if(fabs(x)0&&d1<=0&&d2>0) wn++;
          if(k<0&&d2<=0&&d1>0)  wn--;                            
     }    
     if(wn!=0) return 1;
     return 0;
}
double length(point B)
{
    return sqrt(dot(B,B));       
}
double dist(point a1,point a2 )
{
    if(a1.x==a2.x&&a1.y==a2.y) return length(A-a1 );       
    point v1=a2-a1,v2=A-a1,v3=A-a2;
    if(dcmp(dot(v1,v2)<0)) return length(v2);
    else if(dcmp(dot(v1,v3))>0) return length(v3);
    else return fabs(cross(v1,v2))/length(v1);
}

int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
    int m;
  
    while(scanf("%d",&n)&&n)
    {
          int i;  
          scanf("%lf%lf",&A.x,&A.y);                  
          for(i=0;i


你可能感兴趣的:(acm之计算几何,点到多边形的距离)