function testPolygonPoint(polygon, point) {
  // TODO handle polygon that is a straight line, current impl gives a non-deterministic output, that is depending on number of vertices
  if (toFewEdges(polygon) || !testRectPoint(polygon.boundingRect(), point)) {
    return false;
  }
  const { x, y } = point;
  const vertices = polygon.vertices;
  let inside;
  let l;
  let j;
  let i;
  let tx;
  for (inside = false, i = -1, l = vertices.length, j = l - 1; ++i < l; j = i) {
    if (vertices[i].x === x && vertices[i].y === y) {
      // polygon vertice
      return true;
    }
    if (vertices[i].y === vertices[j].y && vertices[i].y === y && (x - vertices[i].x) * (x - vertices[j].x) <= 0) {
      // on horizontal edge of polygon
      return true;
    }
    if ((vertices[i].y < y && y <= vertices[j].y) || (vertices[j].y < y && y <= vertices[i].y)) {
      tx = ((vertices[j].x - vertices[i].x) * (y - vertices[i].y)) / (vertices[j].y - vertices[i].y) + vertices[i].x;
      if (x === tx) {
        // on polygon edge
        return true;
      }
      if (x < tx) {
        inside = !inside;
      }
    }
  }
  return inside;
}
/**
 * Test if a Polygon contains a Point. If so, returns true and false otherwise.
 * Polygon must contain at least 2 vertices
 * @private
 * @param {object} polygon
 * @param {Array} polygon.vertices - Array of vertices
 * @param {object} polygon.vertices.vertex
 * @param {number} polygon.vertices.vertex.x - x-coordinate
 * @param {number} polygon.vertices.vertex.y - y-coordinate
 * @param {Array} polygon.edges - Array of edges
 * @param {Array} polygon.edges.edge - Array of points
 * @param {object} polygon.edges.edge.point
 * @param {number} polygon.edges.edge.point.x - x-coordinate
 * @param {number} polygon.edges.edge.point.y - y-coordinate
 * @param {object} point
 * @param {number} point.x - x-coordinate
 * @param {number} point.y - y-coordinate
 * @return {boolean} true if polygon conatins point
 */
Test if a Polygon contains a Point. If so, returns true and false otherwise.
Test if a Polygon contains a Point . If so returns true and false otherwise .
function testPolygonPoint(polygon, point) {
  if (toFewEdges(polygon) || !testRectPoint(polygon.boundingRect(), point)) {
    return false;
  }
  const { x, y } = point;
  const vertices = polygon.vertices;
  let inside;
  let l;
  let j;
  let i;
  let tx;
  for (inside = false, i = -1, l = vertices.length, j = l - 1; ++i < l; j = i) {
    if (vertices[i].x === x && vertices[i].y === y) {
      return true;
    }
    if (vertices[i].y === vertices[j].y && vertices[i].y === y && (x - vertices[i].x) * (x - vertices[j].x) <= 0) {
      return true;
    }
    if ((vertices[i].y < y && y <= vertices[j].y) || (vertices[j].y < y && y <= vertices[i].y)) {
      tx = ((vertices[j].x - vertices[i].x) * (y - vertices[i].y)) / (vertices[j].y - vertices[i].y) + vertices[i].x;
      if (x === tx) {
        return true;
      }
      if (x < tx) {
        inside = !inside;
      }
    }
  }
  return inside;
}
function testPolygonPoint ( polygon , point ) { if ( toFewEdges ( polygon ) || ! testRectPoint ( polygon . boundingRect ( ) , point ) ) { return false ; } const { x , y } = point ; const vertices = polygon . vertices ; let inside ; let l ; let j ; let i ; let tx ; for ( inside = false , i = - 1 , l = vertices . length , j = l - 1 ; ++ i < l ; j = i ) { if ( vertices [ i ] . x === x && vertices [ i ] . y === y ) { return true ; } if ( vertices [ i ] . y === vertices [ j ] . y && vertices [ i ] . y === y && ( x - vertices [ i ] . x ) * ( x - vertices [ j ] . x ) <= 0 ) { return true ; } if ( ( vertices [ i ] . y < y && y <= vertices [ j ] . y ) || ( vertices [ j ] . y < y && y <= vertices [ i ] . y ) ) { tx = ( ( vertices [ j ] . x - vertices [ i ] . x ) * ( y - vertices [ i ] . y ) ) / ( vertices [ j ] . y - vertices [ i ] . y ) + vertices [ i ] . x ; if ( x === tx ) { return true ; } if ( x < tx ) { inside = ! inside ; } } } return inside ; }
Test if a Polygon contains a Point.
Test if a Polygon contains a Point .
// TODO handle polygon that is a straight line, current impl gives a non-deterministic output, that is depending on number of vertices // polygon vertice // on horizontal edge of polygon // on polygon edge