Class: AIXM::A
Overview
Angle from 0 to 359 degrees with an optional suffix used for azimuths, bearings, headings, courses etc.
Constant Summary collapse
- SUFFIX_INVERSIONS =
{ R: :L, L: :R }.freeze
Instance Attribute Summary collapse
-
#deg ⇒ Integer
Angle.
-
#precision ⇒ Integer
readonly
Precision:
2
(10 degree steps) or3
(1 degree steps). -
#suffix ⇒ Symbol?
Suffix.
Instance Method Summary collapse
-
#+(numeric_or_angle) ⇒ AIXM::A
Add degrees.
-
#-(numeric_or_angle) ⇒ AIXM::A
Subtract degrees.
- #==(other) ⇒ Boolean (also: #eql?)
- #hash ⇒ Integer
-
#initialize(deg_and_suffix) ⇒ A
constructor
A new instance of A.
- #inspect ⇒ String
-
#inverse_of?(other) ⇒ Boolean
Check whether
other
angle is the inverse. -
#invert ⇒ AIXM::A
Invert an angle by 180 degrees.
- #round ⇒ Object
-
#to_s ⇒ String
Human readable representation according to precision.
Constructor Details
#initialize(deg_and_suffix) ⇒ A
Returns a new instance of A.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/aixm/a.rb', line 42 def initialize(deg_and_suffix) case deg_and_suffix when Numeric self.deg, @precision = deg_and_suffix, 3 when String fail(ArgumentError, "invalid angle") unless deg_and_suffix.to_s =~ /\A(\d+)([A-Z]+)?\z/ self.deg, @precision, self.suffix = $1.to_i * 10, 2, $2 when Symbol # used only by private build method fail(ArgumentError, "invalid precision") unless %i(2 3).include? deg_and_suffix @deg, @precision = 0, deg_and_suffix.to_s.to_i else fail(ArgumentError, "invalid angle") end end |
Instance Attribute Details
#deg ⇒ Integer
Returns angle.
34 35 36 |
# File 'lib/aixm/a.rb', line 34 def deg @deg end |
#precision ⇒ Integer (readonly)
Returns precision: 2
(10 degree steps) or 3
(1 degree steps).
37 38 39 |
# File 'lib/aixm/a.rb', line 37 def precision @precision end |
#suffix ⇒ Symbol?
Returns suffix.
40 41 42 |
# File 'lib/aixm/a.rb', line 40 def suffix @suffix end |
Instance Method Details
#+(numeric_or_angle) ⇒ AIXM::A
Add degrees
110 111 112 113 |
# File 'lib/aixm/a.rb', line 110 def +(numeric_or_angle) fail ArgumentError unless numeric_or_angle.respond_to? :round build(precision: precision, deg: (deg + numeric_or_angle.round) % 360, suffix: suffix) end |
#-(numeric_or_angle) ⇒ AIXM::A
Subtract degrees
118 119 120 121 |
# File 'lib/aixm/a.rb', line 118 def -(numeric_or_angle) fail ArgumentError unless numeric_or_angle.respond_to? :round build(precision: precision, deg: (deg - numeric_or_angle.round + 360) % 360, suffix: suffix) end |
#==(other) ⇒ Boolean Also known as: eql?
130 131 132 |
# File 'lib/aixm/a.rb', line 130 def ==(other) self.class === other && deg == other.deg && precision == other.precision && suffix == other.suffix end |
#hash ⇒ Integer
137 138 139 |
# File 'lib/aixm/a.rb', line 137 def hash to_s.hash end |
#inspect ⇒ String
58 59 60 |
# File 'lib/aixm/a.rb', line 58 def inspect %Q(#<#{self.class}[precision=#{precision}] #{to_s}>) end |
#inverse_of?(other) ⇒ Boolean
Check whether other
angle is the inverse
103 104 105 |
# File 'lib/aixm/a.rb', line 103 def inverse_of?(other) invert == other end |
#invert ⇒ AIXM::A
Invert an angle by 180 degrees
90 91 92 |
# File 'lib/aixm/a.rb', line 90 def invert build(precision: precision, deg: (deg + 180) % 360, suffix: SUFFIX_INVERSIONS.fetch(suffix, suffix)) end |
#round ⇒ Object
124 125 126 |
# File 'lib/aixm/a.rb', line 124 def round deg end |
#to_s ⇒ String
Returns human readable representation according to precision.
63 64 65 66 67 68 69 |
# File 'lib/aixm/a.rb', line 63 def to_s if precision == 2 [('%02d' % ((deg / 10 + 35) % 36 + 1)), suffix].map(&:to_s).join else ('%03d' % deg) end end |