Class: AIXM::A

Inherits:
Object show all
Includes:
Concerns::HashEquality
Defined in:
lib/aixm/a.rb

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

Examples:

Initialization

AIXM.a(-36.9)   # => #<AIXM::A -36.9° "32">
AIXM.a(12)      # => #<AIXM::A 12° "01">
AIXM.a("12L")   # => #<AIXM::A 120° "12L">
AIXM.a(360)     # => #<AIXM::A 0° "36">
AIXM.a(-400)    # => #<AIXM::A -40° "32">

Calculations

a = AIXM.a("02L")
a += 5               # => #<AIXM::A 25° "03L">
a -= AIXM.a(342.8)   # => #<AIXM::A -317.8° "04L">
a.to_s               # => "-317.8°"
a.to_s(:runway)      # => "04L"
a.to_s(:bearing)     # => "042.2000"
a.to_f               # => 42.2
a.to_i               # => 42
a.invert             # => #<AIXM::A -137.8° "22R">
a.to_s(:runway)      # => "22R"

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

Instance Method Summary collapse

Methods included from Concerns::HashEquality

#eql?

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

#degInteger #deg=(value) ⇒ Object

Angle in the range of -360 < angle < 360.

Overloads:

  • #degInteger

    Returns:

    • (Integer)
  • #deg=(value) ⇒ Object

    Parameters:

    • value (Integer)


42
43
44
# File 'lib/aixm/a.rb', line 42

def deg
  @deg
end

#suffixSymbol? #suffix=(value) ⇒ Object

One-letter suffix.

Overloads:

  • #suffixSymbol?

    Returns:

    • (Symbol, nil)
  • #suffix=(value) ⇒ Object

    Parameters:

    • value (Symbol, nil)


50
51
52
# File 'lib/aixm/a.rb', line 50

def suffix
  @suffix
end

Instance Method Details

#+(value) ⇒ AIXM::A

Add degrees.

Parameters:

Returns:



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.

Parameters:

Returns:



168
169
170
# File 'lib/aixm/a.rb', line 168

def -(value)
  self + -value
end

#-@AIXM::A

Negate degrees.

Returns:



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

See Also:

  • Object#==


173
174
175
# File 'lib/aixm/a.rb', line 173

def ==(other)
  self.class === other  && deg == other.deg && suffix == other.suffix
end

#hashObject

See Also:

  • Object#hash


178
179
180
# File 'lib/aixm/a.rb', line 178

def hash
  [self.class, deg, suffix].join.hash
end

#inspectString

Returns:

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

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)


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

def inverse_of?(other)
  invert == other
end

#invertAIXM::A

Invert an angle by 180 degrees.

Examples:

AIXM.a(120).invert     # (300°)
AIXM.a("34L").invert   # (160° suffix "R")

Returns:



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_fFloat

Degrees in the range of 0.0…360.0

Returns:

  • (Float)


80
81
82
# File 'lib/aixm/a.rb', line 80

def to_f
  ((deg + 360) % 360).to_f
end

#to_iInteger

Degrees in the range of 0..359

Returns:

  • (Integer)


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

Parameters:

  • type (Symbol, nil) (defaults to: :human)

    either :runway, :bearing or nil

  • unit (String)

    unit to postfix

Returns:

  • (String)


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