-
-
Save itsSaad/71445483bd50ee670c1b to your computer and use it in GitHub Desktop.
Revisions
-
kidbrax created this gist
Sep 22, 2011 .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,34 @@ 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