Author Topic: Haversine Formula (Rosetta Code)  (Read 2888 times)

0 Members and 1 Guest are viewing this topic.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Haversine Formula (Rosetta Code)
« on: April 17, 2021, 08:49:03 pm »
https://rosettacode.org/wiki/Haversine_formula#QB64

Code: QB64: [Select]
  1. '*** Haversine Formula
  2. '***
  3. '*** By George McGinn (04/16/2021)
  4. '***    Modified on 04/16/2021 From TechBASIC to QB64
  5. '***    NOTE: This program was originally submitted to the Rosetta Code
  6. '***          website as part of a solution to one of their challenges
  7. '***          (http://www.rosettacode.org/wiki/Roman_numerals/Decode#TechBASIC)
  8. '***
  9. '***    Code is set up so user can add input statements to get units of distance
  10. '***    and Lat/Lon for FROM/TO.
  11. '***
  12. '*** NOTE: A more accurate Radius is commented out. Code runs with original
  13. '***       problem showing correct solution.
  14.  
  15.  
  16. SCREEN _NEWIMAGE(800, 100, 32)
  17.  
  18. '*** Units: K=kilometers  M=miles  N=nautical miles
  19. DIM Distance AS STRING
  20. DIM Result AS DOUBLE
  21. DIM ANSWER AS DOUBLE
  22.  
  23. '*** Change the To/From Latittude/Logitudes for your run
  24.  
  25. '*** LAT/LON for Nashville International Airport (BNA)
  26. lat1 = 36.12
  27. Lon1 = -86.67
  28.  
  29. '*** LAT/LONG for Los Angeles International Airport (LAX)
  30. Lat2 = 33.94
  31. Lon2 = -118.40
  32.  
  33. '*** Initialize Values
  34. UNIT = "K"
  35. Distance = ""
  36. 'Radius = 6378.137
  37. Radius = 6372.8
  38.  
  39. '*** Calculate distance using Haversine Function
  40. lat1 = (lat1 * _PI / 180)
  41. Lon1 = (Lon1 * _PI / 180)
  42. Lat2 = (Lat2 * _PI / 180)
  43. Lon2 = (Lon2 * _PI / 180)
  44. DLon = Lon1 - Lon2
  45.  
  46. ANSWER = _ACOS(SIN(lat1) * SIN(Lat2) + COS(lat1) * COS(Lat2) * COS(DLon)) * Radius
  47.  
  48. '*** Adjust Answer based on Distance Unit (kilometers, miles, nautical miles)
  49.        CASE "M"
  50.             Result = ANSWER * 0.621371192
  51.             Distance = "miles"
  52.        CASE "N"
  53.             Result = ANSWER * 0.539956803
  54.             Distance = "nautical miles"
  55.        CASE ELSE
  56.             Result = ANSWER
  57.             Distance = "kilometers"
  58.  
  59. '*** Change PRINT statement with your labels for FROM/TO locations
  60. PRINT "The distance from Nashville International to Los Angeles International in "; Distance;
  61. PRINT USING " is: ##,###.##"; Result;
  62. PRINT "."
  63.  
  64.  
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)