0

When looking around for a way to determine if a point [X,Y] is within or on a polygon [[X,Y],[X,Y],...] I am running into the issue where the upper bounds (maxX and maxY) are being ignored.

Example: point = [10,10] polygon = [[0,0],[10,0],[10,10],[0,10]]

EDIT: polygon could look like [[0,0],[10,0],[10,10],[0,10],[0,0]] if needed; shape may vary and will include convex and concave polygons

It should return true for all cases where the integer is in or on the polygon

I tried nearly every google results/stackoverflow for point in polygon and each equation presented has the same upper bound issue where either I increase all points in the polygon to be 1 greater for all maxX and maxY for it to work properly.

Here are some links to functions I have been trying to use

Check if Point Is Inside A Polygon

Point inside a polygon javascript

How to check if point is in polygon in Javascript

Our usecase will only contain integer coordinates; this is for a game we are working on determining if a player is within a specific region of coordinates to label the region. Open to suggestions for best way to handle doing this.

Currently I have to do something like

function increaseUpperLimit( polygon ) {
  let maxX = -10000000
  let maxY = -10000000
  polygon.forEach(coordinate => {
    if(coordinate[0] > maxX)
      maxX = coordinate[0]
    if(coordinate[1] > maxY)
      maxY = coordinate[1]
  })
  polygon.forEach(coordinate => {
    if(coordinate[0] == maxX)
      coordinate[0] += 1
    if(coordinate[1] == maxY)
      coordinate[1] += 1
  })
  return polygon
}

This increase for all points at max fixes the issue, but it would be more ideal to just fix the original equation to include the upper bounding X and Y

Nardwe
  • 1
  • 1

0 Answers0