-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyMap.java
More file actions
41 lines (34 loc) · 1.21 KB
/
KeyMap.java
File metadata and controls
41 lines (34 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// KeyMap.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class KeyMap {
private static class KeyCoordinate {
public final int x, y;
public KeyCoordinate(int _x, int _y) {
x = _x;
y = _y;
}
public double distanceTo(KeyCoordinate other) {
return Math.hypot(x - other.x, y - other.y);
}
}
private Map<Character, KeyCoordinate> letterToCoord;
public KeyMap() {
letterToCoord = new TreeMap<Character, KeyCoordinate>();
}
public void associateCoordinates(char c, int x, int y) {
letterToCoord.put(c, new KeyCoordinate(x, y));
}
public double getDistanceBetween(char a, char b) {
KeyCoordinate ka = letterToCoord.get(a);
KeyCoordinate kb = letterToCoord.get(b);
if(ka == null)
throw new IllegalArgumentException(String.format("Could not find character '%c' in KeyMap", a));
if(kb == null)
throw new IllegalArgumentException(String.format("Could not find character '%c' in KeyMap", b));
return letterToCoord.get(a).distanceTo(letterToCoord.get(b));
}
}