Class: Journey

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Journey

Returns a new instance of Journey.



6
7
8
9
10
11
# File 'lib/journey.rb', line 6

def initialize(type)
  @samples = Array.new
  @type = type
  @max_long_index, @min_long_index = 0, 0
  @max_lat_index, @min_lat_index = 0, 0   
end

Instance Attribute Details

#samplesObject (readonly)

Returns the value of attribute samples.



4
5
6
# File 'lib/journey.rb', line 4

def samples
  @samples
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/journey.rb', line 4

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object



53
54
55
# File 'lib/journey.rb', line 53

def <=>(other)
  self.start_time <=> other.start_time
end

#[](index) ⇒ Object



24
25
26
# File 'lib/journey.rb', line 24

def [](index)
  @samples[index]
end

#add_sample(sample) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/journey.rb', line 13

def add_sample(sample)
  @samples.push(sample)
  new_long = sample.long
  new_lat = sample.lat
  
  @max_long_index = @samples.length-1 if new_long > @samples[@max_long_index].long
  @min_long_index = @samples.length-1 if new_long < @samples[@min_long_index].long
  @max_lat_index = @samples.length-1 if new_lat > @samples[@max_lat_index].lat
  @min_lat_index = @samples.length-1 if new_lat < @samples[@min_lat_index].lat
end

#eachObject



49
50
51
# File 'lib/journey.rb', line 49

def each
  samples.each {|sample| yield sample}
end

#find_centerObject



28
29
30
31
32
# File 'lib/journey.rb', line 28

def find_center
  lat = (@samples[@max_lat_index].lat + @samples[@min_lat_index].lat) / 2
  long = (@samples[@max_long_index].long +  @samples[@min_long_index].long) / 2
  [lat, long]
end

#find_distance_of_longest_vectorObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/journey.rb', line 34

def find_distance_of_longest_vector
  max_long_point = @samples[@max_long_index].point
  min_long_point = @samples[@min_long_index].point
  long_distance = max_long_point.distance_from(min_long_point)
  
  max_lat_point = @samples[@max_lat_index].point
  min_lat_point = @samples[@min_lat_index].point
  lat_distance = max_lat_point.distance_from(min_lat_point)
  [lat_distance, long_distance].max
end

#sizeObject



45
46
47
# File 'lib/journey.rb', line 45

def size
  @samples.size
end

#start_timeObject



57
58
59
# File 'lib/journey.rb', line 57

def start_time
  samples[0].time
end

#to_sObject



61
62
63
64
65
66
67
68
# File 'lib/journey.rb', line 61

def to_s
  time = @samples[0].time
  journey = "#{journey_type} at #{time}:\n"
  @samples.each do |sample| 
    journey += sample.to_s + "\n"
  end
  journey
end