Skip to content

Instantly share code, notes, and snippets.

@msimpson
Created January 9, 2012 21:33
Show Gist options
  • Save msimpson/1585061 to your computer and use it in GitHub Desktop.
Save msimpson/1585061 to your computer and use it in GitHub Desktop.

Revisions

  1. msimpson renamed this gist Jan 9, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. msimpson created this gist Jan 9, 2012.
    20 changes: 20 additions & 0 deletions pip_example
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    var data = {
    polygon: [[0,0], [0,2], [1,3], [2,1], [3,2], [4,0], [3,1], [2,0], [1,1]],
    a: [2, 2],
    b: [1, 2]
    };

    function pointInPolygon(polygon, point) {
    var len = polygon.length,
    x = point[0], y = point[1],
    j = len - 1, i = 0, c = 0;

    for (i, j; i < len; j = i++) {
    y_st = (polygon[i][1] <= y && y < polygon[j][1]) || (polygon[j][1] <= y && y < polygon[i][1]);
    x_st = x < (polygon[j][0] - polygon[i][0]) * (y - polygon[i][1]) / (polygon[j][1] - polygon[i][1]) + polygon[i][0];
    if (y_st && x_st) c = !c;
    }
    return c;
    }

    document.write(pointInPolygon(data.polygon, data.b));