Search HashMap coordinates

I am working on a graphical application. The graphical interface consists of a map with cities. Each city has an X and Y coordinate. Cities are stored in the HashMap as follows:

cities.put(new Coordinates(X, Y), "City Name");

Where X and Y are just some integers that represent the midpoint of the city. As if you need to mark a city with a circle, X and Y will represent the center of that circle.

It’s not difficult for me to get the coordinates of a mouse click. However, my problem is that I do not know how to search through HashMap and get the nearest city. No person can perfectly click on a specific X and a specific Y coordinate. Therefore, I must allow, for example, + 15.

+4
source share
1

, .

, 100 100, , 10 10 , :

top = y - y%10;
left = x - x%10;

:

Map<Coordinates, City>

City - , ( ).

, .

, City.

EDIT: , hashcode .equals() Coordinates, .

+1

All Articles