Class: CompassPoint

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

Constant Summary collapse

VERSION =
'1.2.0'.freeze
POINTS =
{
  n:    {min: 354.38, mid: 0.0,    max: 5.62,   name: 'North'},
  nbe:  {min: 5.63,   mid: 11.25,  max: 16.87,  name: 'North by east'},
  nne:  {min: 16.88,  mid: 22.5,   max: 28.12,  name: 'North-northeast'},
  nebn: {min: 28.13,  mid: 33.75,  max: 39.37,  name: 'Northeast by north'},
  ne:   {min: 39.38,  mid: 45.0,   max: 50.62,  name: 'Northeast'},
  nebe: {min: 50.63,  mid: 56.25,  max: 61.87,  name: 'Northeast by east'},
  ene:  {min: 61.88,  mid: 67.5,   max: 73.12,  name: 'East-northeast'},
  ebn:  {min: 73.13,  mid: 78.75,  max: 84.37,  name: 'East by north'},
  e:    {min: 84.38,  mid: 90.0,   max: 95.62,  name: 'East'},
  ebs:  {min: 95.63,  mid: 101.25, max: 106.87, name: 'East by south'},
  ese:  {min: 106.88, mid: 112.5,  max: 118.12, name: 'East-southeast'},
  sebe: {min: 118.13, mid: 123.75, max: 129.37, name: 'Southeast by east'},
  se:   {min: 129.38, mid: 135.0,  max: 140.62, name: 'Southeast'},
  sebs: {min: 140.63, mid: 146.25, max: 151.87, name: 'Southeast by south'},
  sse:  {min: 151.88, mid: 157.5,  max: 163.12, name: 'South-southeast'},
  sbe:  {min: 163.13, mid: 168.75, max: 174.37, name: 'South by east'},
  s:    {min: 174.38, mid: 180.0,  max: 185.62, name: 'South'},
  sbw:  {min: 185.63, mid: 191.25, max: 196.87, name: 'South by west'},
  ssw:  {min: 196.88, mid: 202.5,  max: 208.12, name: 'South southwest'},
  swbs: {min: 208.13, mid: 213.75, max: 219.37, name: 'Southwest by south'},
  sw:   {min: 219.38, mid: 225.0,  max: 230.62, name: 'Southwest'},
  swbw: {min: 230.63, mid: 236.25, max: 241.87, name: 'Southwest by west'},
  wsw:  {min: 241.88, mid: 247.5,  max: 253.12, name: 'West-southwest'},
  wbs:  {min: 253.13, mid: 258.75, max: 264.37, name: 'West by south'},
  w:    {min: 264.38, mid: 270.0,  max: 275.62, name: 'West'},
  wbn:  {min: 275.63, mid: 281.25, max: 286.87, name: 'West by north'},
  wnw:  {min: 286.88, mid: 292.5,  max: 298.12, name: 'West-northwest'},
  nwbw: {min: 298.13, mid: 303.75, max: 309.37, name: 'Northwest by west'},
  nw:   {min: 309.38, mid: 315.0,  max: 320.62, name: 'Northwest'},
  nwbn: {min: 320.63, mid: 326.25, max: 331.87, name: 'Northwest by north'},
  nnw:  {min: 331.88, mid: 337.50, max: 343.12, name: 'North northwest'},
  nbw:  {min: 343.13, mid: 348.75, max: 354.37, name: 'North by west'}
}.freeze

Class Method Summary collapse

Class Method Details

.azimuth(s) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/compass_point.rb', line 40

def azimuth(s)
  input = normalize_input(s)
  if point = find_point(input)
    point[:mid]
  elsif match = input.match(/(n|s)\s(\d{1,3}).?\s(e|w)/)
    base = if match[1] == 'n'
      match[3] == 'w' ? 360 : 0
    else
      180
    end

    adjust = match[2].to_i

    operation = if (match[1] == 'n' && match[3] == 'w') || (match[1] == 's' && match[3] == 'e')
      :-
    else
      :+
    end

    base.send(operation, adjust)
  end
end

.compass_quadrant_bearing(bearing) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/compass_point.rb', line 83

def compass_quadrant_bearing(bearing)
  b = bearing.round
  case b
  when 0, 360 then 'N'
  when 90     then 'E'
  when 180    then 'S'
  when 270    then 'W'
  else
    generate_compass_quadrant_bearing(b)
  end
end

.max(s) ⇒ Object



68
69
70
71
# File 'lib/compass_point.rb', line 68

def max(s)
  point = find_point(normalize_input(s))
  point && point[:max]
end

.min(s) ⇒ Object



63
64
65
66
# File 'lib/compass_point.rb', line 63

def min(s)
  point = find_point(normalize_input(s))
  point && point[:min]
end

.min_max(s) ⇒ Object



73
74
75
76
# File 'lib/compass_point.rb', line 73

def min_max(s)
  point = find_point(normalize_input(s))
  point && [point[:min], point[:max]]
end

.name(s) ⇒ Object



78
79
80
81
# File 'lib/compass_point.rb', line 78

def name(s)
  point = find_point(normalize_input(s))
  point && point[:name]
end