Created
January 9, 2012 21:33
-
-
Save msimpson/1585061 to your computer and use it in GitHub Desktop.
Revisions
-
msimpson renamed this gist
Jan 9, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
msimpson created this gist
Jan 9, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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));