Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use points structure rather than create 2 arrays

  • Loading branch information...
commit 1c9393fd238ac33d907cbc7d2acd2a688fd232df 1 parent 9c5cbdb
@mnoack mnoack authored
Showing with 14 additions and 24 deletions.
  1. +12 −22 lib/geokit/polygon.rb
  2. +2 −2 test/test_polygon_contains.rb
View
34 lib/geokit/polygon.rb
@@ -2,38 +2,28 @@ module Geokit
# A complex polygon made of multiple points. End point must equal start point to close the poly.
class Polygon
- attr_accessor :poly_y, :poly_x
+ attr_accessor :points
+ # Pass in an array of Geokit::LatLng
def initialize(points)
- # Pass in an array of Geokit::LatLng
- @poly_x = []
- @poly_y = []
-
- points.each do |point|
- @poly_x << point.lng
- @poly_y << point.lat
- end
+ @points = points
# A Polygon must be 'closed', the last point equal to the first point
- if @poly_x[0] != @poly_x[-1] || @poly_y[0] != @poly_y[-1]
- # Append the first point to the array to close the polygon
- @poly_x << @poly_x[0]
- @poly_y << @poly_y[0]
- end
-
+ # Append the first point to the array to close the polygon
+ @points << points[0] if points[0] != points[-1]
end
def contains?(point)
- j = @poly_x.length - 1
+ last_point = @points[-1]
oddNodes = false
x = point.lng
y = point.lat
- for i in (0..j)
- yi = @poly_y[i]
- xi = @poly_x[i]
- yj = @poly_y[j]
- xj = @poly_x[j]
+ for p in @points
+ yi = p.lat
+ xi = p.lng
+ yj = last_point.lat
+ xj = last_point.lng
if (yi < y && yj >= y ||
yj < y && yi >= y)
if (xi + (y - yi) / (yj - yi) * (xj - xi) < x)
@@ -41,7 +31,7 @@ def contains?(point)
end
end
- j=i
+ last_point = p
end
oddNodes
View
4 test/test_polygon_contains.rb
@@ -93,8 +93,8 @@ def test_open_polygon
#puts "\t Does poly_x[0] (#{@open_polygon.poly_x[0]}) == poly_x[-1] (#{@open_polygon.poly_x[-1]}) ?"
#puts "\t Does poly_y[0] (#{@open_polygon.poly_y[0]}) == poly_y[-1] (#{@open_polygon.poly_y[-1]}) ?"
- assert @open_polygon.poly_x[0] == @open_polygon.poly_x[-1]
- assert @open_polygon.poly_y[0] == @open_polygon.poly_y[-1]
+ assert @open_polygon.points[0].lng == @open_polygon.points[-1].lng
+ assert @open_polygon.points[0].lat == @open_polygon.points[-1].lat
end
Please sign in to comment.
Something went wrong with that request. Please try again.