class CompassPoint
Constants
- POINTS
- VERSION
Public Class Methods
azimuth(s)
click to toggle source
# 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
back_azimuth(s)
click to toggle source
# File lib/compass_point.rb, line 63 def back_azimuth(s) azm = azimuth(s) return if azm.nil? azm > 180 ? azm - 180 : azm + 180 end
compass_quadrant_bearing(bearing)
click to toggle source
# File lib/compass_point.rb, line 90 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)
click to toggle source
# File lib/compass_point.rb, line 75 def max(s) point = find_point(normalize_input(s)) point && point[:max] end
min(s)
click to toggle source
# File lib/compass_point.rb, line 70 def min(s) point = find_point(normalize_input(s)) point && point[:min] end
min_max(s)
click to toggle source
# File lib/compass_point.rb, line 80 def min_max(s) point = find_point(normalize_input(s)) point && [point[:min], point[:max]] end
name(s)
click to toggle source
# File lib/compass_point.rb, line 85 def name(s) point = find_point(normalize_input(s)) point && point[:name] end
Private Class Methods
east_or_west(bearing)
click to toggle source
# File lib/compass_point.rb, line 122 def east_or_west(bearing) b = bearing.round (180..360).cover?(b.to_i) ? 'W' : 'E' end
find_point(s)
click to toggle source
# File lib/compass_point.rb, line 127 def find_point(s) find_point_by_abbrev(s) || find_point_by_name(s) end
find_point_by_abbrev(s)
click to toggle source
# File lib/compass_point.rb, line 131 def find_point_by_abbrev(s) POINTS[s.to_sym] end
find_point_by_name(s)
click to toggle source
# File lib/compass_point.rb, line 135 def find_point_by_name(s) POINTS.values.find { |v| v[:name].downcase == s } end
generate_compass_quadrant_bearing(b)
click to toggle source
# File lib/compass_point.rb, line 104 def generate_compass_quadrant_bearing(b) s = [] s << north_or_south(b) s << if north_or_south(b) == 'N' east_or_west(b) == 'W' ? (360 - b).abs : b else (180 - b).abs end.to_s s.last << '°' s << east_or_west(b) s.join(' ') end
normalize_input(name)
click to toggle source
# File lib/compass_point.rb, line 139 def normalize_input(name) name.to_s.strip.squeeze(' ').downcase end
north_or_south(bearing)
click to toggle source
# File lib/compass_point.rb, line 117 def north_or_south(bearing) b = bearing.round (0..90).cover?(b.to_i) || (270..360).cover?(b.to_i) ? 'N' : 'S' end