How to resolve the algorithm Distance and Bearing step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Distance and Bearing step by step in the Nim programming language

Table of Contents

Problem Statement

It is very important in aviation to have knowledge of the nearby airports at any time in flight. Determine the distance and bearing from an Airplane to the 20 nearest Airports whenever requested. Use the non-commercial data from openflights.org airports.dat as reference.

A request comes from an airplane at position ( latitude, longitude ): ( 51.514669, 2.198581 ).

Your report should contain the following information from table airports.dat (column shown in brackets): Name(2), Country(4), ICAO(6), Distance and Bearing calculated from Latitude(7) and Longitude(8).

Distance is measured in nautical miles (NM). Resolution is 0.1 NM. Bearing is measured in degrees (°). 0° = 360° = north then clockwise 90° = east, 180° = south, 270° = west. Resolution is 1°.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Distance and Bearing step by step in the Nim programming language

Source code in the nim programming language

import std/[algorithm, math, parsecsv, strformat, strutils]

const
  R = 6_371 / 1.852      # Mean radius of Earth in nautic miles.
  PlaneLat = 51.514669
  PlaneLong = 2.198581

type

  Airport = object
    name: string
    country: string
    icao: string
    latitude: float
    longitude: float  # In radians.
    altitude: float   # In radians.

func distance(𝜑₁, λ₁, 𝜑₂, λ₂: float): float =
  ## Return the distance computed using the Haversine formula.
  ## Angles are in radians. The result is expressed in nautic miles.
  let a = sin((𝜑₂ - 𝜑₁) * 0.5)^2 + cos(𝜑₁) * cos(𝜑₂) * sin((λ₂ - λ₁) * 0.5)^2
  let c = 2 * arctan2(sqrt(a), sqrt(1 - a))
  result = R * c

func bearing(𝜑₁, λ₁, 𝜑₂, λ₂: float): float =
  ## Return the bearing.
  ## Angles are in radians. The result is expressed in degrees in range [0..360[.
  let Δλ = λ₂ - λ₁
  result = arctan2(sin(Δλ) * cos(𝜑₂), cos(𝜑₁) * sin(𝜑₂) - sin(𝜑₁) * cos(𝜑₂) * cos(Δλ))
  result = (result.radToDeg + 360) mod 360

# Parse the "airports.dat" file.
var parser: CsvParser
var airports: seq[Airport]
parser.open("airports.dat")
while parser.readRow():
  assert parser.row.len == 14
  airports.add Airport(name: parser.row[1],
                       country: parser.row[3],
                       icao: parser.row[5],
                       latitude: parser.row[6].parseFloat().degToRad,
                       longitude: parser.row[7].parseFloat().degToRad)

# Compute the distances then sort them keeping the airport index.
type Distances = tuple[value: float; index: int]
var distances : seq[Distances]
let 𝜑₁ = PlaneLat.degToRad
let λ₁ = PlaneLong.degToRad
for i, airport in airports:
  distances.add (distance(𝜑₁, λ₁, airport.latitude, airport.longitude), i)
distances.sort(Ascending)

# Display the result for the 20 nearest airports.
echo &"""{"Airport":<40}{"Country":<20}{"ICAO":<9}{"Distance":<9}{"Bearing":>9}"""
echo repeat("─", 88)
for i in 0..19:
  let (d, idx) = distances[i]
  let ap = airports[idx]
  let b = bearing(𝜑₁, λ₁, ap.latitude, ap.longitude)
  echo &"{ap.name:<40}{ap.country:<20}{ap.icao:<11}{d:4.1f}{b.toInt:10}"


  

You may also check:How to resolve the algorithm Count in factors step by step in the R programming language
You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Exceptions step by step in the E programming language
You may also check:How to resolve the algorithm Environment variables step by step in the Stata programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the COBOL programming language