Class: LocalGeocoder::Geometry::Polygon
- Inherits:
-
Object
- Object
- LocalGeocoder::Geometry::Polygon
- Defined in:
- lib/local_geocoder/geometry/polygon.rb
Instance Attribute Summary collapse
-
#points ⇒ Object
Returns the value of attribute points.
Class Method Summary collapse
Instance Method Summary collapse
- #[](i) ⇒ Object
- #bounding_box ⇒ Object
- #contains_point?(point) ⇒ Boolean
-
#initialize(points) ⇒ Polygon
constructor
A new instance of Polygon.
- #inspect ⇒ Object
- #number_of_points ⇒ Object
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
#points ⇒ Object
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
Instance Method Details
#[](i) ⇒ Object
19 20 21 |
# File 'lib/local_geocoder/geometry/polygon.rb', line 19 def [](i) @points[i] end |
#bounding_box ⇒ Object
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
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 |
#inspect ⇒ Object
49 50 51 |
# File 'lib/local_geocoder/geometry/polygon.rb', line 49 def inspect @points.map { |p| p.inspect }.join(",") end |
#number_of_points ⇒ Object
15 16 17 |
# File 'lib/local_geocoder/geometry/polygon.rb', line 15 def number_of_points @points.size end |