Class: Geokit::Polygon

Inherits:
Object show all
Defined in:
lib/geokit/polygon.rb

Overview

A complex polygon made of multiple points. End point must equal start point to close the poly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points) ⇒ Polygon

Returns a new instance of Polygon.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/geokit/polygon.rb', line 7

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
  
  # 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
  
end

Instance Attribute Details

#poly_xObject

Returns the value of attribute poly_x.



5
6
7
# File 'lib/geokit/polygon.rb', line 5

def poly_x
  @poly_x
end

#poly_yObject

Returns the value of attribute poly_y.



5
6
7
# File 'lib/geokit/polygon.rb', line 5

def poly_y
  @poly_y
end

Instance Method Details

#contains?(point) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/geokit/polygon.rb', line 26

def contains?(point)
  j = @poly_x.length - 1
  oddNodes = false
  x = point.lng
  y = point.lat

  for i in (0..j)
    if (@poly_y[i] < y && @poly_y[j] >= y ||
        @poly_y[j] < y && @poly_y[i] >= y)
      if (@poly_x[i] + (y - @poly_y[i]) / (@poly_y[j] - @poly_y[i]) * (@poly_x[j] - @poly_x[i]) < x)
        oddNodes = !oddNodes
      end
    end
 
    j=i
  end

  oddNodes
end