Created
November 8, 2011 17:28
-
-
Save timruffles/1348452 to your computer and use it in GitHub Desktop.
Point in poly in coffeescript
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 characters
pointInPoly = (point,poly) -> | |
segments = for pointA, index in poly | |
pointB = poly[(index + 1) % poly.length] | |
[pointA,pointB] | |
intesected = (segment for segment in segments when rayIntesectsSegment(point,segment)) | |
intesected.length % 2 != 0 | |
rayIntesectsSegment = (p,segment) -> | |
[p1,p2] = segment | |
[a,b] = if p1.y < p2.y | |
[p1,p2] | |
else | |
[p2,p1] | |
if p.y == b.y || p.y == a.y | |
p.y += Number.MIN_VALUE | |
if p.y > b.y || p.y < a.y | |
false | |
else if p.x > a.x && p.x > b.x | |
false | |
else if p.x < a.x && p.x < b.x | |
true | |
else | |
mAB = (b.y - a.y) / (b.x - a.x) | |
mAP = (p.y - a.y) / (p.x - a.x) | |
mAP > mAB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment