# Allison Obourn
# CSC 110, Spring 2018
# Lecture 27

# Prompts the user for an earthquake radius and epicenter
# draws the earthquake range on a DrawingPanel as well as all
# cities in the input file. Cities are black if they aren't affected,
# red if they are. Red cities are output to a file. 

from DrawingPanel import *
import math

def main():
    cities = read_data()

    x_quake = int(input("Epicenter x? "))
    y_quake = int(input("Epicenter y? "))
    radius = int(input("Radius? "))

    # draws each city and outputs to a file if hit by earthquake 
    out_file = open("out.txt", "w")
    panel = DrawingPanel(200, 200)
    for i in range(len(cities)):
        x = cities[i][1]
        y = cities[i][2]
        color = "black"
        if distance(x, y, x_quake, y_quake) < radius:
            color = "red"
            out_file.write(str(cities[i]) + "\n")
        panel.fill_oval(x, y, 5, 5, color)
        panel.draw_string(cities[i][0], x, y, color)

    
    panel.draw_oval(x_quake - radius, y_quake - radius,
                    radius * 2, radius * 2)

# computes the distance between a point at (x1, y1)
# and a point at (x2, y2)
def distance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

# reads city data from a file into a list of tuples
# where each tuple contains the city name, x and y coordinates
# assumes the file has one city per line with name x y on it
# separated by spaces. Returns the list of cities
def read_data():
    file = open("cities.txt")
    lines = file.readlines()
    cities = []

    for i in range(len(lines)):
        line = lines[i].split()
        city = (line[0], int(line[1]), int(line[2]))
        cities.append(city)
        
    return cities
    

main()
