Class: LocalGeocoder::Geometry::Polygon

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points) ⇒ Polygon

Returns a new instance of Polygon.



7
8
9
# File 'lib/local_geocoder/geometry/polygon.rb', line 7

def initialize(points)
  @points = points
end

Instance Attribute Details

#pointsObject

Returns the value of attribute points.



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

def points
  @points
end

Class Method Details

.from_point_array(points) ⇒ Object



11
12
13
# File 'lib/local_geocoder/geometry/polygon.rb', line 11

def self.from_point_array(points)
  Polygon.new(points.map { |p| Point.new(*p) })
end

Instance Method Details

#[](i) ⇒ Object



19
20
21
# File 'lib/local_geocoder/geometry/polygon.rb', line 19

def [](i)
  @points[i]
end

#bounding_boxObject



23
24
25
26
27
28
29
# File 'lib/local_geocoder/geometry/polygon.rb', line 23

def bounding_box
  @bounding_box ||= begin
    min_x, max_x = @points.minmax_by { |p| p.x }.map { |p| p.x }
    min_y, max_y = @points.minmax_by { |p| p.y }.map { |p| p.y }
    Rect.new(min_x, min_y, max_x-min_x, max_y-min_y)
  end
end

#contains_point?(point) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/local_geocoder/geometry/polygon.rb', line 31

def contains_point?(point)
  return false if !self.bounding_box.contains_point?(point)
  
  contains_point = false
  i = -1
  j = self.number_of_points - 1
  while (i += 1) < self.number_of_points
    p1 = self[i]; p2 = self[j]
    if within_y_bounds?(point, p1, p2)
      if intersects_line_segment?(point, p1, p2)
        contains_point = !contains_point
      end
    end
    j = i
  end
  return contains_point
end

#inspectObject



49
50
51
# File 'lib/local_geocoder/geometry/polygon.rb', line 49

def inspect
  @points.map { |p| p.inspect }.join(",")
end

#number_of_pointsObject



15
16
17
# File 'lib/local_geocoder/geometry/polygon.rb', line 15

def number_of_points
  @points.size
end