Class: OceanNames::Polygon

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

Instance Method Summary collapse

Constructor Details

#initialize(points) ⇒ Polygon

Returns a new instance of Polygon.



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

def initialize(points)
  @points = points
  @points << points[0] if points[0] != points[-1]
end

Instance Method Details

#contains?(lat:, lng:) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ocean_names/polygon.rb', line 10

def contains?(lat:, lng:)
  last_point = @points[-1]

  odd_node = false

  x = lng
  y = lat

  @points.each do |p|
    # p = [lng, lat]
    x1 = p.first
    y1 = p.last
    x2 = last_point.first
    y2 = last_point.last

    if x1 < x && x2 >= x || x2 < x && x1 >= x
      odd_node = !odd_node if y1 + (x - x1) / (x2 - x1) * (y2 - y1) < y
    end

    last_point = p
  end

  odd_node
end