class CartesianForGeo::Polygon

Class for Polygon (has many Vectors)

Attributes

vectors[R]

Public Class Methods

new(*vectors) click to toggle source
# 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
parse(text) click to toggle source
# 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) click to toggle source
# File lib/cartesian_for_geo.rb, line 148
def self.parse!(text)
        parse(text)
rescue VectorsCountError
        nil
end

Public Instance Methods

concat(other) click to toggle source
# File lib/cartesian_for_geo.rb, line 165
def concat(other)
        vectors.concat(other.vectors)
        self
end
include?(other) click to toggle source
# File lib/cartesian_for_geo.rb, line 172
def include?(other)
        side == other.side && lat_range.cover?(other.lat_range)
end
lat_range() click to toggle source
# File lib/cartesian_for_geo.rb, line 176
def lat_range
        @lat_range ||= Range.new(*lat_edges)
end
side() click to toggle source
# File lib/cartesian_for_geo.rb, line 180
def side
        vectors.first.from.side
end
split() click to toggle source
# 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() click to toggle source
# File lib/cartesian_for_geo.rb, line 184
def to_s
        points = vectors.map(&:to_a).flatten!.uniq
        "(#{points.map(&:to_s).join(',')})"
end

Private Instance Methods

center() click to toggle source
# File lib/cartesian_for_geo.rb, line 191
def center
        coords =
                vectors.each_with_object({ lat: 0, lng: 0 }.to_a) do |vector, arr|
                        arr.each { |el| el[-1] += vector.to.send(el.first) }
                end
        Point.new(coords.map! { |el| el.last / vectors.size })
end
lat_edges() click to toggle source
# File lib/cartesian_for_geo.rb, line 199
def lat_edges
        [vectors.first.from, vectors.last.to].map!(&:lat).sort!
end