Class: Astronoby::Velocity

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/astronoby/velocity.rb

Overview

Represents a velocity with meters per second as its internal representation. Provides conversions between m/s, km/s, km/day, and AU/day.

Examples:

Create a velocity from km/s

velocity = Astronoby::Velocity.from_kmps(29.78)
velocity.aupd  # AU per day

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meters_per_second) ⇒ Velocity

Returns a new instance of Velocity.

Parameters:

  • meters_per_second (Numeric)

    the velocity in m/s



80
81
82
83
# File 'lib/astronoby/velocity.rb', line 80

def initialize(meters_per_second)
  @meters_per_second = meters_per_second
  freeze
end

Instance Attribute Details

#meters_per_secondNumeric (readonly) Also known as: mps

Returns the velocity in meters per second.

Returns:

  • (Numeric)

    the velocity in meters per second



76
77
78
# File 'lib/astronoby/velocity.rb', line 76

def meters_per_second
  @meters_per_second
end

Class Method Details

.from_astronomical_units_per_day(astronomical_units_per_day) ⇒ Astronoby::Velocity Also known as: from_aupd

Returns a new Velocity.

Parameters:

  • astronomical_units_per_day (Numeric)

    the velocity in AU/day

Returns:



48
49
50
51
52
# File 'lib/astronoby/velocity.rb', line 48

def from_astronomical_units_per_day(astronomical_units_per_day)
  meters_per_second = astronomical_units_per_day *
    Constants::ASTRONOMICAL_UNIT_IN_METERS / Constants::SECONDS_PER_DAY
  from_meters_per_second(meters_per_second)
end

.from_kilometers_per_day(kilometers_per_day) ⇒ Astronoby::Velocity Also known as: from_kmpd

Returns a new Velocity.

Parameters:

  • kilometers_per_day (Numeric)

    the velocity in km/day

Returns:



39
40
41
42
43
# File 'lib/astronoby/velocity.rb', line 39

def from_kilometers_per_day(kilometers_per_day)
  meters_per_second = kilometers_per_day *
    Constants::KILOMETER_IN_METERS / Constants::SECONDS_PER_DAY
  from_meters_per_second(meters_per_second)
end

.from_kilometers_per_second(kilometers_per_second) ⇒ Astronoby::Velocity Also known as: from_kmps

Returns a new Velocity.

Parameters:

  • kilometers_per_second (Numeric)

    the velocity in km/s

Returns:



30
31
32
33
34
# File 'lib/astronoby/velocity.rb', line 30

def from_kilometers_per_second(kilometers_per_second)
  meters_per_second = kilometers_per_second *
    Constants::KILOMETER_IN_METERS
  from_meters_per_second(meters_per_second)
end

.from_meters_per_second(meters_per_second) ⇒ Astronoby::Velocity Also known as: from_mps

Returns a new Velocity.

Parameters:

  • meters_per_second (Numeric)

    the velocity in m/s

Returns:



23
24
25
# File 'lib/astronoby/velocity.rb', line 23

def from_meters_per_second(meters_per_second)
  new(meters_per_second)
end

.light_speedAstronoby::Velocity

Returns the speed of light in vacuum.

Returns:



70
71
72
# File 'lib/astronoby/velocity.rb', line 70

def light_speed
  from_meters_per_second(Constants::LIGHT_SPEED_M_PER_S)
end

.vector_from_astronomical_units_per_day(array) ⇒ Astronoby::Vector<Astronoby::Velocity> Also known as: vector_from_aupd

Returns a vector of Velocities.

Parameters:

  • array (Array<Numeric>)

    array of AU/day values

Returns:



64
65
66
# File 'lib/astronoby/velocity.rb', line 64

def vector_from_astronomical_units_per_day(array)
  Vector.elements(array.map { from_aupd(_1) })
end

.vector_from_meters_per_second(array) ⇒ Astronoby::Vector<Astronoby::Velocity> Also known as: vector_from_mps

Returns a vector of Velocities.

Parameters:

  • array (Array<Numeric>)

    array of m/s values

Returns:



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

def vector_from_meters_per_second(array)
  Vector.elements(array.map { from_mps(_1) })
end

.zeroAstronoby::Velocity

Returns a zero velocity.

Returns:



17
18
19
# File 'lib/astronoby/velocity.rb', line 17

def zero
  new(0)
end

Instance Method Details

#+(other) ⇒ Astronoby::Velocity

Returns the sum.

Parameters:

Returns:



107
108
109
110
111
# File 'lib/astronoby/velocity.rb', line 107

def +(other)
  self.class.from_meters_per_second(
    @meters_per_second + other.meters_per_second
  )
end

#-(other) ⇒ Astronoby::Velocity

Returns the difference.

Parameters:

Returns:



115
116
117
118
119
# File 'lib/astronoby/velocity.rb', line 115

def -(other)
  self.class.from_meters_per_second(
    @meters_per_second - other.meters_per_second
  )
end

#-@Astronoby::Velocity

Returns the negated velocity.

Returns:



122
123
124
# File 'lib/astronoby/velocity.rb', line 122

def -@
  self.class.from_meters_per_second(-@meters_per_second)
end

#<=>(other) ⇒ Integer?

Returns -1, 0, or 1; nil if not comparable.

Parameters:

Returns:

  • (Integer, nil)

    -1, 0, or 1; nil if not comparable



153
154
155
156
157
# File 'lib/astronoby/velocity.rb', line 153

def <=>(other)
  return unless other.is_a?(self.class)

  meters_per_second <=> other.meters_per_second
end

#abs2Numeric

Returns the square of the velocity in (m/s)^2.

Returns:

  • (Numeric)

    the square of the velocity in (m/s)^2



142
143
144
# File 'lib/astronoby/velocity.rb', line 142

def abs2
  @meters_per_second**2
end

#astronomical_units_per_dayFloat Also known as: aupd

Returns the velocity in astronomical units per day.

Returns:

  • (Float)

    the velocity in astronomical units per day



99
100
101
102
# File 'lib/astronoby/velocity.rb', line 99

def astronomical_units_per_day
  @meters_per_second * Constants::SECONDS_PER_DAY /
    Constants::ASTRONOMICAL_UNIT_IN_METERS
end

#hashInteger

Returns hash value.

Returns:

  • (Integer)

    hash value



147
148
149
# File 'lib/astronoby/velocity.rb', line 147

def hash
  [@meters_per_second, self.class].hash
end

#kilometers_per_dayFloat Also known as: kmpd

Returns the velocity in kilometers per day.

Returns:

  • (Float)

    the velocity in kilometers per day



92
93
94
95
# File 'lib/astronoby/velocity.rb', line 92

def kilometers_per_day
  @meters_per_second * Constants::SECONDS_PER_DAY /
    Constants::KILOMETER_IN_METERS
end

#kilometers_per_secondFloat Also known as: kmps

Returns the velocity in kilometers per second.

Returns:

  • (Float)

    the velocity in kilometers per second



86
87
88
# File 'lib/astronoby/velocity.rb', line 86

def kilometers_per_second
  @meters_per_second / Constants::KILOMETER_IN_METERS.to_f
end

#negative?Boolean

Returns true if the velocity is negative.

Returns:

  • (Boolean)

    true if the velocity is negative



132
133
134
# File 'lib/astronoby/velocity.rb', line 132

def negative?
  @meters_per_second < 0
end

#positive?Boolean

Returns true if the velocity is positive.

Returns:

  • (Boolean)

    true if the velocity is positive



127
128
129
# File 'lib/astronoby/velocity.rb', line 127

def positive?
  @meters_per_second > 0
end

#zero?Boolean

Returns true if the velocity is zero.

Returns:

  • (Boolean)

    true if the velocity is zero



137
138
139
# File 'lib/astronoby/velocity.rb', line 137

def zero?
  @meters_per_second.zero?
end