Created
September 22, 2011 22:43
-
-
Save kidbrax/1236253 to your computer and use it in GitHub Desktop.
Check whether a point is within a polygon #ruby
This file contains 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
def point_in_polygon?(polygonPoints) | |
return false if self.latitude.blank? or self.longitude.blank? | |
polygonPoints.each do |point| | |
point[0] = point[0].to_f | |
point[1] = point[1].to_f | |
end | |
contains_point = false | |
i = -1 | |
j = polygonPoints.size - 1 | |
while (i += 1) < polygonPoints.size | |
a_point_on_polygon = polygonPoints[i] | |
trailing_point_on_polygon = polygonPoints[j] | |
if point_is_between_the_ys_of_the_line_segment?(a_point_on_polygon, trailing_point_on_polygon) | |
if ray_crosses_through_line_segment?(a_point_on_polygon, trailing_point_on_polygon) | |
contains_point = !contains_point | |
end | |
end | |
j = i | |
end | |
contains_point | |
end | |
private | |
def point_is_between_the_ys_of_the_line_segment?(a_point_on_polygon, trailing_point_on_polygon) | |
(a_point_on_polygon[0] <= self.latitude && self.latitude < trailing_point_on_polygon[0]) || | |
(trailing_point_on_polygon[0] <= self.latitude && self.latitude < a_point_on_polygon[0]) | |
end | |
def ray_crosses_through_line_segment?(a_point_on_polygon, trailing_point_on_polygon) | |
(self.longitude < (trailing_point_on_polygon[1] - a_point_on_polygon[1]) * (self.latitude - a_point_on_polygon[0]) / | |
(trailing_point_on_polygon[0] - a_point_on_polygon[0]) + a_point_on_polygon[1]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really helpful script 😄