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.
134 135 136 137 138 139 |
# File 'lib/cartesian_for_geo.rb', line 134 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.
132 133 134 |
# File 'lib/cartesian_for_geo.rb', line 132 def vectors @vectors end |
Class Method Details
.parse(text) ⇒ Object
141 142 143 144 145 146 |
# File 'lib/cartesian_for_geo.rb', line 141 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
148 149 150 151 152 |
# File 'lib/cartesian_for_geo.rb', line 148 def self.parse!(text) parse(text) rescue VectorsCountError nil end |
Instance Method Details
#concat(other) ⇒ Object
165 166 167 168 |
# File 'lib/cartesian_for_geo.rb', line 165 def concat(other) vectors.concat(other.vectors) self end |
#include?(other) ⇒ Boolean
172 173 174 |
# File 'lib/cartesian_for_geo.rb', line 172 def include?(other) side == other.side && lat_range.cover?(other.lat_range) end |
#lat_range ⇒ Object
176 177 178 |
# File 'lib/cartesian_for_geo.rb', line 176 def lat_range @lat_range ||= Range.new(*lat_edges) end |
#side ⇒ Object
180 181 182 |
# File 'lib/cartesian_for_geo.rb', line 180 def side vectors.first.from.side end |
#split ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/cartesian_for_geo.rb', line 154 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
184 185 186 187 |
# File 'lib/cartesian_for_geo.rb', line 184 def to_s points = vectors.map(&:to_a).flatten!.uniq "(#{points.map(&:to_s).join(',')})" end |