Class: AIXM::A

Inherits:
Object
  • Object
show all
Defined in:
lib/aixm/a.rb

Overview

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

Examples:

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")

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")

Constant Summary collapse

SUFFIX_INVERSIONS =
{
  R: :L,
  L: :R
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#degInteger

Returns angle.

Returns:

  • (Integer)

    angle


34
35
36
# File 'lib/aixm/a.rb', line 34

def deg
  @deg
end

#precisionInteger (readonly)

Returns precision: 2 (10 degree steps) or 3 (1 degree steps).

Returns:

  • (Integer)

    precision: 2 (10 degree steps) or 3 (1 degree steps)


37
38
39
# File 'lib/aixm/a.rb', line 37

def precision
  @precision
end

#suffixSymbol?

Returns suffix.

Returns:

  • (Symbol, nil)

    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

Returns:


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

Returns:


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?

Returns:

  • (Boolean)

See Also:

  • Object#==

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

#hashInteger

Returns:

  • (Integer)

See Also:

  • Object#hash

137
138
139
# File 'lib/aixm/a.rb', line 137

def hash
  to_s.hash
end

#inspectString

Returns:

  • (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

Examples:

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

Returns:

  • (Boolean)

    whether the inverted angle or not


103
104
105
# File 'lib/aixm/a.rb', line 103

def inverse_of?(other)
  invert == other
end

#invertAIXM::A

Invert an angle by 180 degrees

Examples:

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

Returns:


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

#to_sString

Returns human readable representation according to precision.

Returns:

  • (String)

    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