Class: CartesianForGeo::Polygon

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

Overview

Class for Polygon (has many Vectors)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*vectors) ⇒ Polygon

Returns a new instance of Polygon.

Raises:



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

#vectorsObject (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

Returns:

  • (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_rangeObject



165
166
167
# File 'lib/cartesian_for_geo.rb', line 165

def lat_range
  @lat_range ||= Range.new(*lat_edges)
end

#sideObject



169
170
171
# File 'lib/cartesian_for_geo.rb', line 169

def side
  vectors.first.from.side
end

#splitObject



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_sObject



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