Skip to content

Instantly share code, notes, and snippets.

@realmyst
Created March 16, 2013 15:20
Show Gist options
  • Save realmyst/5176845 to your computer and use it in GitHub Desktop.
Save realmyst/5176845 to your computer and use it in GitHub Desktop.
def contains_point?(point, poly)
c = false
i = -1
j = poly.size - 1
while (i += 1) < poly.size
if ((poly[i][:y] <= point[:y] && point[:y] < poly[j][:y]) || (poly[j][:y] <= point[:y] && point[:y] < poly[i][:y]))
if (point[:x] < (poly[j][:x] - poly[i][:x]) * (point[:y] - poly[i][:y]) / (poly[j][:y] - poly[i][:y]) + poly[i][:x])
c = !c
end
j = i
end
end
c
end
poly = [
{x:2, y:0},
{x:4, y:-2},
{x:2, y:-4},
{x:0, y:-2},
]
point1 = {x: 2, y: -2}
point2 = {x: 1, y: 1}
point3 = {x: 1, y: -2}
point4 = {x: 1, y: -1}
puts contains_point?(point1, poly)
puts contains_point?(point2, poly)
puts contains_point?(point3, poly)
puts contains_point?(point4, poly)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment