Class: AIXM::A
Overview
Angle in the range of -360 < angle < 360 degrees (used for azimuths or courses) and with an optional one-letter suffix (used for runways).
Constant Summary collapse
- SUFFIX_INVERSIONS =
{ R: :L, L: :R }.freeze
- RUNWAY_RE =
/\A(0[1-9]|[12]\d|3[0-6])([A-Z])?\z/
Instance Attribute Summary collapse
-
#deg ⇒ Object
Angle in the range of -360 < angle < 360.
-
#suffix ⇒ Object
One-letter suffix.
Instance Method Summary collapse
-
#+(value) ⇒ AIXM::A
Add degrees.
-
#-(value) ⇒ AIXM::A
Subtract degrees.
-
#-@ ⇒ AIXM::A
Negate degrees.
- #==(other) ⇒ Object
- #hash ⇒ Object
-
#initialize(value) ⇒ A
constructor
See the overview for examples.
- #inspect ⇒ String
-
#inverse_of?(other) ⇒ Boolean
Whether other angle is the inverse.
-
#invert ⇒ AIXM::A
Invert an angle by 180 degrees.
-
#to_f ⇒ Float
Degrees in the range of 0.0…360.0.
-
#to_i ⇒ Integer
Degrees in the range of 0..359.
-
#to_s(type = :human) ⇒ String
Degrees as formatted string.
Methods included from Concerns::HashEquality
Constructor Details
#initialize(value) ⇒ A
See the overview for examples.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/aixm/a.rb', line 53 def initialize(value) case value when String fail(ArgumentError, "invalid angle") unless value =~ RUNWAY_RE self.deg, self.suffix = $1.to_i * 10, $2 when Numeric self.deg = value else fail(ArgumentError, "invalid angle") end end |
Instance Attribute Details
#deg ⇒ Integer #deg=(value) ⇒ Object
Angle in the range of -360 < angle < 360.
42 43 44 |
# File 'lib/aixm/a.rb', line 42 def deg @deg end |
#suffix ⇒ Symbol? #suffix=(value) ⇒ Object
One-letter suffix.
50 51 52 |
# File 'lib/aixm/a.rb', line 50 def suffix @suffix end |
Instance Method Details
#+(value) ⇒ AIXM::A
Add degrees.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/aixm/a.rb', line 153 def +(value) case value when Numeric value.zero? ? self : self.class.new(deg + value).tap { _1.suffix = suffix } when AIXM::A value.deg.zero? ? self : self.class.new(deg + value.deg).tap { _1.suffix = suffix } else fail ArgumentError end end |
#-(value) ⇒ AIXM::A
Subtract degrees.
168 169 170 |
# File 'lib/aixm/a.rb', line 168 def -(value) self + -value end |
#-@ ⇒ AIXM::A
Negate degrees.
145 146 147 |
# File 'lib/aixm/a.rb', line 145 def -@ deg.zero? ? self : self.class.new(-deg).tap { _1.suffix = suffix } end |
#==(other) ⇒ Object
173 174 175 |
# File 'lib/aixm/a.rb', line 173 def ==(other) self.class === other && deg == other.deg && suffix == other.suffix end |
#hash ⇒ Object
178 179 180 |
# File 'lib/aixm/a.rb', line 178 def hash [self.class, deg, suffix].join.hash end |
#inspect ⇒ String
66 67 68 |
# File 'lib/aixm/a.rb', line 66 def inspect %Q(#<#{self.class} #{to_s} #{to_s(:runway).inspect}>) end |
#inverse_of?(other) ⇒ Boolean
Whether other angle is the inverse.
138 139 140 |
# File 'lib/aixm/a.rb', line 138 def inverse_of?(other) invert == other end |
#invert ⇒ AIXM::A
Invert an angle by 180 degrees.
123 124 125 126 127 |
# File 'lib/aixm/a.rb', line 123 def invert self.class.new(deg.negative? ? deg - 180 : deg + 180).tap do |angle| angle.suffix = SUFFIX_INVERSIONS.fetch(suffix, suffix) end end |
#to_f ⇒ Float
Degrees in the range of 0.0…360.0
80 81 82 |
# File 'lib/aixm/a.rb', line 80 def to_f ((deg + 360) % 360).to_f end |
#to_i ⇒ Integer
Degrees in the range of 0..359
73 74 75 |
# File 'lib/aixm/a.rb', line 73 def to_i (deg.round + 360) % 360 end |
#to_s(type = :human) ⇒ String
Degrees as formatted string
Types are:
-
:human - degrees within -359.9~..359.9~ as D.D° (default)
-
:bearing - degrees within 0.0..359.9~ as DDD.DDDD
-
:runway - degrees within “01”..“36” plus optional suffix
94 95 96 97 98 99 100 101 102 |
# File 'lib/aixm/a.rb', line 94 def to_s(type=:human) return '' unless deg case type when :runway then [('%02d' % (((deg / 10).round + 35) % 36 + 1)), suffix].join when :bearing then '%08.4f' % to_f.round(4) when :human then [deg.to_s('F').sub(/\.0$/, ''), '°'].join else fail ArgumentError end end |