Class: Runby::PaceCalculator

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

Overview

Encapsulates the algorithms used to calculate target paces.

Constant Summary collapse

DATA_POINTS_COUNT =

The number of data points plotted on our line of 5K times.

We take 5K times from 14:00 to 42:00 with a sample rate
of 30 seconds, and out pops 57.
57
MIDPOINT_X =

The midpoint along the X axis of our pace data “graph”

28

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(golden_pace_set, midpoint_radius_divisor) ⇒ PaceCalculator

Returns a new instance of PaceCalculator.



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

def initialize(golden_pace_set, midpoint_radius_divisor)
  @fastest_pace_km = golden_pace_set.fastest
  @slowest_pace_km = golden_pace_set.slowest
  @midpoint_radius_divisor = midpoint_radius_divisor
end

Instance Attribute Details

#fastest_pace_kmObject (readonly)

The fastest pace within the run type data set.

Given a personal record of 14 minutes for a 5K race,
this is the prescribed pace for this run type.


15
16
17
# File 'lib/runby_pace/pace_calculator.rb', line 15

def fastest_pace_km
  @fastest_pace_km
end

#midpoint_radius_divisorObject (readonly)

For maximum flexibility, we assume the radius of the curve

of the pace data to be equal to the X axis midpoint, a perfect circle.
Use the midpoint_radius_divisor to reduce the height of the curve
until it matches that of the data. (See #curve_minutes)


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

def midpoint_radius_divisor
  @midpoint_radius_divisor
end

#slowest_pace_kmObject (readonly)

The slowest pace for the run type data set.

Given a personal record of 42 minutes for a 5K race,
this is the prescribed pace for this run type.


20
21
22
# File 'lib/runby_pace/pace_calculator.rb', line 20

def slowest_pace_km
  @slowest_pace_km
end

Instance Method Details

#calc(five_k_time, distance_units = :km) ⇒ Object

Calculate the prescribed pace for the given 5K time



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/runby_pace/pace_calculator.rb', line 40

def calc(five_k_time, distance_units = :km)
  five_k_time = Runby::RunbyTime.new five_k_time
  distance_units = Runby::DistanceUnit.new distance_units
  x2 = ((five_k_time.total_minutes * 2) - (MIDPOINT_X - 1)) - 1
  minutes_per_km = slope * x2 + @fastest_pace_km.time.total_minutes + curve_minutes(x2)
  minutes_per_unit = minutes_per_km * distance_units.conversion_factor

  # TODO: Is there a way to clean up all of this "newing up"?
  time = RunbyTime.from_minutes(minutes_per_unit)
  distance = Distance.new distance_units, 1
  Pace.new time, distance
end

#slopeObject

Calculate the slope of the line between the fastest and slowest paces



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

def slope
  (@slowest_pace_km.time.total_minutes - @fastest_pace_km.time.total_minutes) / (DATA_POINTS_COUNT - 1)
end