Advertisement
FocusedWolf

Elite:Dangerous: Bearing Calculator

Dec 26th, 2024 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Version 5
  4.  
  5. # POSTED ONLINE: https://pastebin.com/krGma4Z3
  6.  
  7. # Colors.
  8. BLACK = '\033[30m'
  9. RED = '\033[31m'
  10. GREEN = '\033[32m'
  11. YELLOW = '\033[33m'
  12. BLUE = '\033[34m'
  13. MAGENTA = '\033[35m'
  14. CYAN = '\033[36m'
  15. WHITE = '\033[37m'
  16. BRIGHT_BLACK = '\033[90m'
  17. BRIGHT_RED = '\033[91m'
  18. BRIGHT_GREEN = '\033[92m'
  19. BRIGHT_YELLOW = '\033[93m'
  20. BRIGHT_BLUE = '\033[94m'
  21. BRIGHT_MAGENTA = '\033[95m'
  22. BRIGHT_CYAN = '\033[96m'
  23. BRIGHT_WHITE = '\033[97m'
  24.  
  25. # Styles.
  26. RESET = '\033[0m' # Reset all styles.
  27. BOLD = '\033[1m'
  28. HALF_BRIGHT = '\033[2m'
  29. ITALIC = '\033[3m'
  30. UNDERLINE = '\033[4m'
  31. REVERSED = '\033[7m' # Reverse foreground and background colors.
  32.  
  33. def color(var, color):
  34.     return color + str(var) + RESET
  35.  
  36. import os
  37. def clear():
  38.     os.system('cls' if os.name in ('nt', 'dos') else 'clear')
  39.  
  40. # -----
  41.  
  42. def wait_for_any_keypress():
  43.     import sys
  44.     if sys.platform == 'win32':
  45.         import os
  46.         os.system('pause')
  47.     elif sys.platform.startswith('linux') or sys.platform == 'darwin':
  48.         print('Press any key to continue . . .')
  49.         import termios
  50.         import tty
  51.         stdin_file_desc = sys.stdin.fileno()
  52.         old_stdin_tty_attr = termios.tcgetattr(stdin_file_desc)
  53.         try:
  54.             tty.setraw(stdin_file_desc)
  55.             sys.stdin.read(1)
  56.         finally:
  57.             termios.tcsetattr(stdin_file_desc, termios.TCSADRAIN, old_stdin_tty_attr)
  58.  
  59. # ------
  60.  
  61. import math
  62.  
  63. def calculate_bearing(lat1, lon1, lat2, lon2):
  64.     # Convert latitude and longitude from degrees to radians
  65.     lat1 = math.radians(lat1)
  66.     lon1 = math.radians(lon1)
  67.     lat2 = math.radians(lat2)
  68.     lon2 = math.radians(lon2)
  69.  
  70.     # Bearing formula.
  71.     # SOURCE: https://www.movable-type.co.uk/scripts/latlong.html
  72.     dlon = lon2 - lon1
  73.     y = math.sin(dlon) * math.cos(lat2)
  74.     x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dlon)
  75.     theta = math.atan2(y, x)
  76.     bearing = (math.degrees(theta) + 360) % 360
  77.     return bearing
  78.  
  79. def main():
  80.     while True:
  81.         try:
  82.             clear()
  83.  
  84.             print(color('Position ', BRIGHT_BLUE), end='')
  85.             inputs = input(color('Destination: ', RED))
  86.  
  87.             # Replace commas with spaces and split into a list.
  88.             inputs = inputs.replace(',', ' ').split(' ')
  89.  
  90.             # Remove empty strings.
  91.             inputs = list(filter(None, inputs))
  92.  
  93.             if len(inputs) != 4:
  94.                 raise ValueError('Expected Input: Position-Latitude Position-Longitude Destination-Latitude Destination-Longitude')
  95.  
  96.             latitude_1 = float(inputs[0])
  97.             longitude_1 = float(inputs[1])
  98.             latitude_2 = float(inputs[2])
  99.             longitude_2 = float(inputs[3])
  100.  
  101.             bearing = calculate_bearing(latitude_1, longitude_1, latitude_2, longitude_2)
  102.  
  103.             print()
  104.             print(f'{color('Bearing:', BRIGHT_GREEN)} {bearing:.2f}°')
  105.  
  106.         except Exception as e:
  107.             print()
  108.             print(e)
  109.  
  110.         print()
  111.         wait_for_any_keypress()
  112.  
  113. if __name__ == '__main__':
  114.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement