Class: DirectionVector

Inherits:
GeoVector show all
Includes:
GeoDirection, GeoDistance::Extract
Defined in:
lib/geo_vectors/direction_vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GeoDirection

#bearing_to_dir, #dir_to_bearing, #directions, #get_valid_direction, #random_direction, #to_bearing_vector, #to_point_vector, #valid_direction?, #valid_directions

Methods included from GeoDistance::Extract

#extract_distance

Methods inherited from GeoVector

#/, #add, #add!, #scale, #sub, #sub!

Constructor Details

#initialize(dist, dir) ⇒ DirectionVector

Returns a new instance of DirectionVector.



10
11
12
13
14
# File 'lib/geo_vectors/direction_vector.rb', line 10

def initialize dist, dir
  dist, dir = [dir, dist] if dir.kind_of? GeoDistance
  @distance   = extract_distance dist
  @direction  = get_valid_direction dir
end

Instance Attribute Details

#directionObject

direction symbol :N, :S, :SW, etc.



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

def direction
  @direction
end

#distanceObject

GeoDistance object



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

def distance
  @distance
end

Instance Method Details

#add_to_point(point) ⇒ Object

return new point from adding vector to point



45
46
47
48
# File 'lib/geo_vectors/direction_vector.rb', line 45

def add_to_point point
  vec = as_bearing_vector
  point.destination_point vec.bearing, vec.distance.in_kms
end

#add_to_point!(point) ⇒ Object

add vector directly to point (destructive update)



51
52
53
54
55
56
57
# File 'lib/geo_vectors/direction_vector.rb', line 51

def add_to_point! point
  vec = as_bearing_vector
  dest = point.destination_point vec.bearing, vec.distance.in_kms
  point.lat = dest.lat
  point.lng = dest.lng
  point
end

#as_bearing_vectorObject



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

def as_bearing_vector 
  to_bearing_vector direction, distance
end

#as_point_vectorObject



40
41
42
# File 'lib/geo_vectors/direction_vector.rb', line 40

def as_point_vector 
  to_point_vector direction, distance
end

#random_vectorObject



16
17
18
# File 'lib/geo_vectors/direction_vector.rb', line 16

def random_vector                             
  DirectionVector.new distance.random, random_direction
end

#scale!(scale) ⇒ Object



28
29
30
# File 'lib/geo_vectors/direction_vector.rb', line 28

def scale! scale
  @distance *= scale 
end

#unitObject



32
33
34
# File 'lib/geo_vectors/direction_vector.rb', line 32

def unit
  distance.unit
end