Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Version 5
- # POSTED ONLINE: https://pastebin.com/krGma4Z3
- # Colors.
- BLACK = '\033[30m'
- RED = '\033[31m'
- GREEN = '\033[32m'
- YELLOW = '\033[33m'
- BLUE = '\033[34m'
- MAGENTA = '\033[35m'
- CYAN = '\033[36m'
- WHITE = '\033[37m'
- BRIGHT_BLACK = '\033[90m'
- BRIGHT_RED = '\033[91m'
- BRIGHT_GREEN = '\033[92m'
- BRIGHT_YELLOW = '\033[93m'
- BRIGHT_BLUE = '\033[94m'
- BRIGHT_MAGENTA = '\033[95m'
- BRIGHT_CYAN = '\033[96m'
- BRIGHT_WHITE = '\033[97m'
- # Styles.
- RESET = '\033[0m' # Reset all styles.
- BOLD = '\033[1m'
- HALF_BRIGHT = '\033[2m'
- ITALIC = '\033[3m'
- UNDERLINE = '\033[4m'
- REVERSED = '\033[7m' # Reverse foreground and background colors.
- def color(var, color):
- return color + str(var) + RESET
- import os
- def clear():
- os.system('cls' if os.name in ('nt', 'dos') else 'clear')
- # -----
- def wait_for_any_keypress():
- import sys
- if sys.platform == 'win32':
- import os
- os.system('pause')
- elif sys.platform.startswith('linux') or sys.platform == 'darwin':
- print('Press any key to continue . . .')
- import termios
- import tty
- stdin_file_desc = sys.stdin.fileno()
- old_stdin_tty_attr = termios.tcgetattr(stdin_file_desc)
- try:
- tty.setraw(stdin_file_desc)
- sys.stdin.read(1)
- finally:
- termios.tcsetattr(stdin_file_desc, termios.TCSADRAIN, old_stdin_tty_attr)
- # ------
- import math
- def calculate_bearing(lat1, lon1, lat2, lon2):
- # Convert latitude and longitude from degrees to radians
- lat1 = math.radians(lat1)
- lon1 = math.radians(lon1)
- lat2 = math.radians(lat2)
- lon2 = math.radians(lon2)
- # Bearing formula.
- # SOURCE: https://www.movable-type.co.uk/scripts/latlong.html
- dlon = lon2 - lon1
- y = math.sin(dlon) * math.cos(lat2)
- x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dlon)
- theta = math.atan2(y, x)
- bearing = (math.degrees(theta) + 360) % 360
- return bearing
- def main():
- while True:
- try:
- clear()
- print(color('Position ', BRIGHT_BLUE), end='')
- inputs = input(color('Destination: ', RED))
- # Replace commas with spaces and split into a list.
- inputs = inputs.replace(',', ' ').split(' ')
- # Remove empty strings.
- inputs = list(filter(None, inputs))
- if len(inputs) != 4:
- raise ValueError('Expected Input: Position-Latitude Position-Longitude Destination-Latitude Destination-Longitude')
- latitude_1 = float(inputs[0])
- longitude_1 = float(inputs[1])
- latitude_2 = float(inputs[2])
- longitude_2 = float(inputs[3])
- bearing = calculate_bearing(latitude_1, longitude_1, latitude_2, longitude_2)
- print()
- print(f'{color('Bearing:', BRIGHT_GREEN)} {bearing:.2f}°')
- except Exception as e:
- print()
- print(e)
- print()
- wait_for_any_keypress()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement