class AIXM::A

Angle from 0 to 359 degrees with an optional suffix used for azimuths, bearings, headings, courses etc.

@example Initialized with Numeric

a = AIXM.a(12)   # 12 degrees, 1 degree precision, no suffix
a.precision      # => 3 (three digits = steps of 1 degree)
a.to_s           # => "012"
a.suffix         # => nil
a.deg            # => 12
a.deg += 7       # => 19
a.deg += 341     # => 0     - deg is always within (0..359)
a.to_s           # => "000" - to_s is always within ("000".."359")

@example Initialized with String

a = AIXM.a('06L')   # 60 degrees, 10 degree precision, suffix :L
a.precision         # => 2 (two digits = steps of 10 degrees)
a.to_s              # => "06L"
a.suffix            # => :L
a.deg               # => 60
a.deg += 7          # => 70
a.deg += 190        # => 0     - deg is always within (0..359)
a.to_s              # => "36L" - to_s converts to ("01".."36")

Constants

SUFFIX_INVERSIONS

Attributes

deg[R]

@return [Integer] angle

precision[R]

@return [Integer] precision: 2 (10 degree steps) or 3 (1 degree steps)

suffix[R]

@return [Symbol, nil] suffix

Public Class Methods

new(deg_and_suffix) click to toggle source
   # File lib/aixm/a.rb
42 def initialize(deg_and_suffix)
43   case deg_and_suffix
44   when Numeric
45     self.deg, @precision = deg_and_suffix, 3
46   when String
47     fail(ArgumentError, "invalid angle") unless deg_and_suffix.to_s =~ /\A(\d+)([A-Z]+)?\z/
48     self.deg, @precision, self.suffix = $1.to_i * 10, 2, $2
49   when Symbol   # used only by private build method
50     fail(ArgumentError, "invalid precision") unless %i(2 3).include? deg_and_suffix
51     @deg, @precision = 0, deg_and_suffix.to_s.to_i
52   else
53     fail(ArgumentError, "invalid angle")
54   end
55 end

Public Instance Methods

+(numeric_or_angle) click to toggle source

Add degrees

@return [AIXM::A]

    # File lib/aixm/a.rb
110 def +(numeric_or_angle)
111   fail ArgumentError unless numeric_or_angle.respond_to? :round
112   build(precision: precision, deg: (deg + numeric_or_angle.round) % 360, suffix: suffix)
113 end
-(numeric_or_angle) click to toggle source

Subtract degrees

@return [AIXM::A]

    # File lib/aixm/a.rb
118 def -(numeric_or_angle)
119   fail ArgumentError unless numeric_or_angle.respond_to? :round
120   build(precision: precision, deg: (deg - numeric_or_angle.round + 360) % 360, suffix: suffix)
121 end
==(other) click to toggle source

@see Object#== @return [Boolean]

    # File lib/aixm/a.rb
130 def ==(other)
131   self.class === other  && deg == other.deg && precision == other.precision && suffix == other.suffix
132 end
Also aliased as: eql?
deg=(value) click to toggle source
   # File lib/aixm/a.rb
71 def deg=(value)
72   fail(ArgumentError, "invalid deg `#{value}'") unless value.is_a?(Numeric) && value.round.between?(0, 360)
73   @deg = (precision == 2 ? (value.to_f / 10).round * 10 : value.round) % 360
74 end
eql?(other)
Alias for: ==
hash() click to toggle source

@see Object#hash @return [Integer]

    # File lib/aixm/a.rb
137 def hash
138   to_s.hash
139 end
inspect() click to toggle source

@return [String]

   # File lib/aixm/a.rb
58 def inspect
59   %Q(#<#{self.class}[precision=#{precision}] #{to_s}>)
60 end
inverse_of?(other) click to toggle source

Check whether other angle is the inverse

@example

AIXM.a(120).inverse_of? AIXM.a(300)       # => true
AIXM.a("34L").inverse_of? AIXM.a("16R")   # => true
AIXM.a("33X").inverse_of? AIXM.a("33X")   # => true
AIXM.a("16R").inverse_of? AIXM.a("16L")   # => false

@return [Boolean] whether the inverted angle or not

    # File lib/aixm/a.rb
103 def inverse_of?(other)
104   invert == other
105 end
invert() click to toggle source

Invert an angle by 180 degrees

@example

AIXM.a(120).invert     # => AIXM.a(300)
AIXM.a("34L").invert   # => AIXM.a("16R")
AIXM.a("33X").invert   # => AIXM.a("33X")

@return [AIXM::A] inverted angle

   # File lib/aixm/a.rb
90 def invert
91   build(precision: precision, deg: (deg + 180) % 360, suffix: SUFFIX_INVERSIONS.fetch(suffix, suffix))
92 end
round() click to toggle source

@private

    # File lib/aixm/a.rb
124 def round
125   deg
126 end
suffix=(value) click to toggle source
   # File lib/aixm/a.rb
76 def suffix=(value)
77   fail(RuntimeError, "suffix only allowed when precision is 2") unless value.nil? || precision == 2
78   fail(ArgumentError, "invalid suffix") unless value.nil? || value.to_s =~ /\A[A-Z]+\z/
79   @suffix = value&.to_s&.to_sym
80 end
to_s() click to toggle source

@return [String] human readable representation according to precision

   # File lib/aixm/a.rb
63 def to_s
64   if precision == 2
65     [('%02d' % ((deg / 10 + 35) % 36 + 1)), suffix].map(&:to_s).join
66   else
67     ('%03d' % deg)
68   end
69 end

Private Instance Methods

build(precision:, deg:, suffix: nil) click to toggle source
    # File lib/aixm/a.rb
143 def build(precision:, deg:, suffix: nil)
144   self.class.new(precision.to_s.to_sym).tap do |a|
145     a.deg = deg
146     a.suffix = suffix
147   end
148 end