Class: GeoVectors

Inherits:
Array
  • Object
show all
Includes:
Enumerable
Defined in:
lib/geo_vectors/geo_vectors.rb

Overview

Operations that apply on a collection of GeoVector

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#extract, #mean, #sum, #to_point

Methods included from GeoVector::Macros

#point_vector, #vector

Constructor Details

#initialize(*vectors) ⇒ GeoVectors

Returns a new instance of GeoVectors.



7
8
9
# File 'lib/geo_vectors/geo_vectors.rb', line 7

def initialize *vectors
  self.vectors = vectors
end

Instance Attribute Details

#vectorsObject

include GeoVector



5
6
7
# File 'lib/geo_vectors/geo_vectors.rb', line 5

def vectors
  @vectors
end

Instance Method Details

#/(scalar) ⇒ Object Also known as: reduce



67
68
69
# File 'lib/geo_vectors/geo_vectors.rb', line 67

def / scalar
  scale (1.0 / scalar)
end

#add_to_point(point) ⇒ Object

return new point from adding vector to point



35
36
37
# File 'lib/geo_vectors/geo_vectors.rb', line 35

def add_to_point point
  add_to_point! point.dup
end

#add_to_point!(point) ⇒ Object

add vector directly to point (destructive update)



40
41
42
43
44
45
# File 'lib/geo_vectors/geo_vectors.rb', line 40

def add_to_point! point
  vectors.each do |v|
    point.add! v
  end
  point
end

#directionsObject

iterate each and call direction



18
19
20
# File 'lib/geo_vectors/geo_vectors.rb', line 18

def directions
  vectors.map {|v| v.direction }
end

#distancesObject



22
23
24
# File 'lib/geo_vectors/geo_vectors.rb', line 22

def distances
  vectors.map {|v| v.distance }
end

#each(&block) ⇒ Object



30
31
32
# File 'lib/geo_vectors/geo_vectors.rb', line 30

def each &block
  vectors.each { |v| yield v }
end

#reverse_directionsObject



73
74
75
76
77
78
# File 'lib/geo_vectors/geo_vectors.rb', line 73

def reverse_directions
  vectors.each do |v|
    v.reverse!
  end    
  self
end

#scale(scalar) ⇒ Object Also known as: *



60
61
62
# File 'lib/geo_vectors/geo_vectors.rb', line 60

def scale scalar
  self.dup.scale! scalar
end

#scale!(scalar) ⇒ Object



55
56
57
58
# File 'lib/geo_vectors/geo_vectors.rb', line 55

def scale! scalar
  each {|v| v.scale! scalar }
  self
end

#sizeObject Also known as: length, count



80
81
82
# File 'lib/geo_vectors/geo_vectors.rb', line 80

def size
  vectors.size
end

#sub_from_point(point) ⇒ Object



47
48
49
# File 'lib/geo_vectors/geo_vectors.rb', line 47

def sub_from_point point
  reverse_directions.add_to_point point    
end

#sub_from_point!(point) ⇒ Object



51
52
53
# File 'lib/geo_vectors/geo_vectors.rb', line 51

def sub_from_point! point
  reverse_directions.add_to_point! point
end

#to_sObject



86
87
88
89
90
91
# File 'lib/geo_vectors/geo_vectors.rb', line 86

def to_s
  vectors.inject([]) do |res, v|
    res << v.to_s
    res
  end.join(' ; ')
end

#total_distance(unit = :km) ⇒ Object



26
27
28
# File 'lib/geo_vectors/geo_vectors.rb', line 26

def total_distance unit = :km
  distances.map(&:"#{unit}").sum
end