Class: CartesianForGeo::Polygon
- Inherits:
-
Object
- Object
- CartesianForGeo::Polygon
- Defined in:
- lib/cartesian_for_geo.rb
Overview
Class for Polygon (has many Vectors)
Instance Attribute Summary collapse
-
#vectors ⇒ Object
readonly
Returns the value of attribute vectors.
Class Method Summary collapse
Instance Method Summary collapse
- #concat(other) ⇒ Object
- #include?(other) ⇒ Boolean
-
#initialize(*vectors) ⇒ Polygon
constructor
A new instance of Polygon.
- #lat_range ⇒ Object
- #side ⇒ Object
- #split ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*vectors) ⇒ Polygon
Returns a new instance of Polygon.
123 124 125 126 127 128 |
# File 'lib/cartesian_for_geo.rb', line 123 def initialize(*vectors) @vectors = vectors.flatten raise VectorsCountError if @vectors.size < 2 first_crossing = @vectors.index(&:crossing?) @vectors.rotate!(first_crossing).push(@vectors.first) if first_crossing end |
Instance Attribute Details
#vectors ⇒ Object (readonly)
Returns the value of attribute vectors.
121 122 123 |
# File 'lib/cartesian_for_geo.rb', line 121 def vectors @vectors end |
Class Method Details
.parse(text) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/cartesian_for_geo.rb', line 130 def self.parse(text) points = text.tr('() ', '').split(',').each_slice(2).to_a.map! do |point| Point[point.map(&:to_f)] end new points.map.with_index(-1) { |point, ind| Vector[points[ind], point] } end |
.parse!(text) ⇒ Object
137 138 139 140 141 |
# File 'lib/cartesian_for_geo.rb', line 137 def self.parse!(text) parse(text) rescue VectorsCountError nil end |
Instance Method Details
#concat(other) ⇒ Object
154 155 156 157 |
# File 'lib/cartesian_for_geo.rb', line 154 def concat(other) vectors.concat(other.vectors) self end |
#include?(other) ⇒ Boolean
161 162 163 |
# File 'lib/cartesian_for_geo.rb', line 161 def include?(other) side == other.side && lat_range.cover?(other.lat_range) end |
#lat_range ⇒ Object
165 166 167 |
# File 'lib/cartesian_for_geo.rb', line 165 def lat_range @lat_range ||= Range.new(*lat_edges) end |
#side ⇒ Object
169 170 171 |
# File 'lib/cartesian_for_geo.rb', line 169 def side vectors.first.from.side end |
#split ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/cartesian_for_geo.rb', line 143 def split @polygons = PolygonsCollection.new @vectors.each_with_object([]) do |vector, vectors_arr| next vectors_arr << vector unless vector.crossing? @polygons << Polygon.new(vectors_arr.push(*vector.split).slice!(0..-2)) rescue VectorsCountError next end @polygons.empty? ? @polygons << Polygon.new(@vectors) : @polygons end |
#to_s ⇒ Object
173 174 175 176 |
# File 'lib/cartesian_for_geo.rb', line 173 def to_s points = vectors.map(&:to_a).flatten!.uniq "(#{points.map(&:to_s).join(',')})" end |