# 点在多边形内算法——判断一个点是否在一个复杂多边形的内部

（注意：这个算法适用于顺时针和逆时针绘制的多边形。）

C代码例子

// Globals which should be set before calling this function:
//
// int    polySides  =  how many cornersthe polygon has
// float  polyX[]    =  horizontalcoordinates of corners
// float  polyY[]    =  verticalcoordinates of corners
// float  x,y       =  point to be tested
//
// (Globals are used in this example for purposes of speed.  Change as
// desired.)
//
//  Thefunction will return YES if the point x,y is inside the polygon, or
//  NOif it is not.  If the point is exactly on the edge of the polygon,
// then the function may return YES or NO.
//
// Note that division by zero is avoided because the division is protected
//  bythe "if" clause which surrounds it.

bool pointInPolygon() {

  int   i,j=polySides-1 ;
  bool  oddNodes=NO     ;

  for (i=0;i<polySides; i++) {
    if(polyY[i]<y && polyY[j]>=y
    ||  polyY[j]<y&& polyY[i]>=y) {
      if(polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {
        oddNodes=!oddNodes;}}
    j=i;}

  returnoddNodes; }

// Globals which should be set before calling this function:
//
// int    polySides  =  how many cornersthe polygon has
// float  polyX[]    =  horizontalcoordinates of corners
// float  polyY[]    =  verticalcoordinates of corners
// float  x,y       =  point to be tested
//
// (Globals are used in this example for purposes of speed.  Change as
// desired.)
//
//  Thefunction will return YES if the point x,y is inside the polygon, or
//  NOif it is not.  If the point is exactly on the edge of the polygon,
// then the function may return YES or NO.
//
// Note that division by zero is avoided because the division is protected
//  bythe "if" clause which surrounds it.

bool pointInPolygon() {

  int   i,j=polySides-1 ;
  bool  oddNodes=NO     ;

  for (i=0;i<polySides; i++) {
    if((polyY[i]< y && polyY[j]>=y
    ||   polyY[j]<y && polyY[i]>=y)
    &&  (polyX[i]<=x || polyX[j]<=x)){
      if(polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {
        oddNodes=!oddNodes;}}
    j=i;}

  returnoddNodes; }

// Globals which should be set before calling this function:
//
// int    polySides  =  how many cornersthe polygon has
// float  polyX[]    =  horizontalcoordinates of corners
// float  polyY[]    =  verticalcoordinates of corners
// float  x,y       =  point to be tested
//
// (Globals are used in this example for purposes of speed.  Change as
// desired.)
//
//  Thefunction will return YES if the point x,y is inside the polygon, or
//  NOif it is not.  If the point is exactly on the edge of the polygon,
// then the function may return YES or NO.
//
// Note that division by zero is avoided because the division is protected
//  bythe "if" clause which surrounds it.

bool pointInPolygon() {

  int   i,j=polySides-1 ;
  bool  oddNodes=NO     ;

  for (i=0;i<polySides; i++) {
    if((polyY[i]< y && polyY[j]>=y
    ||   polyY[j]<y && polyY[i]>=y)
    && (polyX[i]<=x || polyX[j]<=x)) {
      oddNodes^=(polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x);}
    j=i;}

  returnoddNodes; }