def walk_distance(user_location, canteen_location, bus_route): """Finds the distance of user and starting bus stop, and canteen and end bus stop. Args: user_location ((int, int) -> tuple): Coordinates of location that is marked by the user. canteen_location ((int, int) -> tuple): Coordinates of the canteen chosen by the user. bus_route ([[str, (int, int)]] -> list): List of bus stops with their respective coordinates, starting from the starting bus stop to the destination. Returns: distance_bus_stop_user (float): Distance between user and the starting bus stop, in meters. distance_bus_stop_canteen (float): Distance between canteen and the end bus stop, in meters. """ start_coordinates = bus_route[0][1] end_coordinates = bus_route[-1][1] # find straight walking distance from user to starting bus stop, # and from end bus stop to canteen, in pixels distance_bus_stop_user = data.distance_a_b(start_coordinates, user_location) distance_bus_stop_canteen = data.distance_a_b(end_coordinates, canteen_location) distance_bus_stop_user = convert.pixel_to_meter(distance_bus_stop_user) distance_bus_stop_canteen = convert.pixel_to_meter( distance_bus_stop_canteen) return distance_bus_stop_user, distance_bus_stop_canteen
def display_info(database, stall): """Displays information of stall in the form of table. Args: database (dict): Canteen database. stall ((key, value) -> tuple): Data of a stall. Format is similar to database but type is tuple. """ key, value = stall # format data for display purposes avg_price = convert.float_to_dollar(value[1]) distance = convert.pixel_to_meter(value[2]) distance = " ".join([str(distance), "m"]) menu = display_food_menu(value[3]) # displays directions in pygame and command line directions = transport.display_directions(stall, user_location) # create table object table = PrettyTable() # table header is on the leftmost column table.add_column('', [ 'Canteen', 'Stall Name', 'Category', 'Rating', 'Average Price', 'Distance', 'Menu', 'Directions' ]) table.add_column('Information', [ key[0], key[2], key[3], value[0], avg_price, distance, menu, directions ]) print(table) print("0: Back to main menu") print("1: Back to canteen stall selection") user_option = check.user_input_index(0, 1) if user_option == 0: main_menu() elif user_option == 1: # go back to table of all stalls display_table(database) choose_canteen(database)
def bus_distance(bus_route, bus_loop): """Finds the distance travelled using bus. Args: bus_route ([[str, (int, int)]] -> list): List of bus stops with their respective coordinates, starting from the starting bus stop to the destination. bus_loop (str): 'red' or 'blue' for choosing which loop. Returns: total_distance (float): Distance travelled using bus along the route, in meters. """ route_nodes = get_route_nodes(bus_route, bus_loop) total_distance = 0 # find the sum of straight distance between each nodes, # starting from the starting bus stop to the end bus stop for i in range(len(route_nodes) - 1): distance = data.distance_a_b(route_nodes[i], route_nodes[i + 1]) total_distance += distance total_distance = convert.pixel_to_meter(total_distance) return total_distance
def display_table(database): """Displays the database in the form of table. Args: database (dict): Canteen database. """ # table header row table = PrettyTable([ 'No.', 'Canteen', 'Stall Name', 'Category', 'Rating', 'Average Price', 'Distance' ]) num = 0 for key, value in database.items(): num += 1 # formatting for display purposes distance = convert.pixel_to_meter(value[2]) distance = "".join([str(distance), " m"]) average_price = convert.float_to_dollar(value[1]) table.add_row( [num, key[0], key[2], key[3], value[0], average_price, distance]) print("\nSearch Results\n") print(table)
def display_directions(stall, user_location): """Displays directions from user to canteen. Args: stall ((key, value) -> tuple): Tuple with same format with database, but for 1 stall. user_location ((int, int) -> tuple): Coordinates of location that is marked by the user. Returns: str: String of directions that will be displayed on the stall information. """ # declare global to minimize use of parameters global red_coordinates global blue_coordinates # get data of red and blue bus stop coordinates red_coordinates = data.get_bus_coordinates('red') blue_coordinates = data.get_bus_coordinates('blue') canteen_location = stall[0][1] distance_user_canteen = stall[1][2] # for each loop, check whether the user can walk straight to the canteen, # and find the bus route from user position to canteen red_walk, red_route = directions(canteen_location, user_location, distance_user_canteen, red_coordinates) blue_walk, blue_route = directions(canteen_location, user_location, distance_user_canteen, blue_coordinates) # display route in pygame pygame_bus_route(blue_route, red_route, user_location, canteen_location) str_list = ["Recommended Routes\n"] if red_walk or blue_walk: distance_meters = convert.pixel_to_meter(distance_user_canteen) str_list.extend([ "\nYou are near to the canteen. Walk straight ahead. (", str(distance_meters), " m)" ]) else: # find total bus distance from user to canteen for the 2 bus loops red_bus_distance = bus_distance(red_route, 'red') blue_bus_distance = bus_distance(blue_route, 'blue') # for each bus loop, find straight walking distance from user to starting bus stop, # and from end bus stop to canteen red_walk_distance = walk_distance(user_location, canteen_location, red_route) blue_walk_distance = walk_distance(user_location, canteen_location, blue_route) # find total travel distance from user to canteen for both bus loops red_travel_distance = red_bus_distance + sum(red_walk_distance) blue_travel_distance = blue_bus_distance + sum(blue_walk_distance) # shows the two loops if both loops' total number of bus stops are different within 1 stop # e.g. blue: 6 stops, red: 7 stops # usually, people think that 1 extra stop is still ok as it isn't that much of a difference if len(blue_route) <= len(red_route) + 1: # blue loop route str_list.append("\nBlue Loop\n\n") str_list.extend(display_bus_route(blue_route, blue_walk_distance)) str_list.extend( ["\nTotal bus distance: ", str(blue_bus_distance), " m"]) str_list.extend( ["\nTotal travel distance: ", str(blue_travel_distance), " m"]) if len(red_route) <= len(blue_route) + 1: # red loop route str_list.append("\nRed Loop\n\n") str_list.extend(display_bus_route(red_route, red_walk_distance)) str_list.extend( ["\nTotal bus distance: ", str(red_bus_distance), " m"]) str_list.extend( ["\nTotal travel distance: ", str(red_travel_distance), " m"]) return "".join(str_list)