Due: 8:00am, Thursday, April 1. Value: 30 pts. Submit to Sauron.
Extend your program from Assignment 10 so that after drawing a
circle for each ZIP code in Arkansas, your program
accepts mouse clicks in the user. With each click, your program
should find the closest circle to the point clicked
and display that circle's ZIP code and location name.
You can use print to display the
ZIP code and location name — which means that this
information won't appear in the same window as where you clicked.
One way to do this is to create two dictionaries, one mapping circles to ZIP codes and another mapping circles to location names. Each dictionary would start out empty, but you would add new keys into it as you process each line from the file. After finishing the file, you would want a loop that waits for each click:
while not window.isClosed():
click = window.getMouse()
# process mouse click
With each click, you'd go through each key (each of which is a circle) for one of the dictionaries, finding which circle's center is closest to the point clicked. (You might use the below function to compute the distance between two points.) After testing all circles, you can display the value from each dictionary associated with that circle that you found was closest.
def distance(p0, p1):
dx = p0.getX() - p1.getX()
dy = p0.getY() - p1.getY()
return (dx * dx + dy * dy) ** 0.5