-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.java
104 lines (74 loc) · 2.66 KB
/
Utils.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @author carmine carella
*
* Utility class for managing conversion of coordinates x,y into String key
* or Direction N,E,S,W
*
*/
public final class Utils {
//North square coordinates are (x, y+1) regards to current coordinates' square
public static String getNorthSquare(Square current) {
String key;
key = current.getX() + "" + (current.getY() + 1);
return key;
}
//East square coordinates are (x+1, y) regards to current coordinates' square
public static String getEastSquare(Square current) {
String key;
key = (current.getX() + 1) + "" + current.getY();
return key;
}
//South square coordinates are (x, y-1) regards to current coordinates' square
public static String getSouthSquare(Square current) {
String key;
key = current.getX() + "" + (current.getY() - 1);
return key;
}
//West square coordinates are (x-1, y) regards to current coordinates' square
public static String getWestSquare(Square current) {
String key;
key = (current.getX()-1) + "" + current.getY();
return key;
}
public static Integer approximateDistance(Square s1, Square s2) {
if (s1 == null || s2 == null)
return Integer.MAX_VALUE;
return Math.abs(s1.getX() - s2.getX()) + Math.abs(s1.getY() - s2.getY());
}
//A square s is in north direction regards to current square if current.y + 1 = s.y
public static boolean isNorthSquare(Square square, Square current) {
boolean result = false;
if( current.getX().intValue() == square.getX().intValue() &&
(current.getY().intValue()+1) == square.getY().intValue()){
result = true;
}
return result;
}
//A square s is in east direction regards to current square if current.x + 1 = s.x
public static boolean isEastSquare(Square square, Square current) {
boolean result = false;
if( current.getY().intValue() == square.getY().intValue() &&
(current.getX().intValue()+1) == square.getX()){
result = true;
}
return result;
}
//A square s is in south direction regards to current square if current.y - 1 = s.y
public static boolean isSouthSquare(Square square, Square current) {
boolean result = false;
if( current.getX().intValue() == square.getX().intValue() &&
(current.getY().intValue()-1) == square.getY().intValue()){
result = true;
}
return result;
}
//A square s is in west direction regards to current square if current.x - 1 = s.x
public static boolean isWestSquare(Square square, Square current) {
boolean result = false;
if( current.getY().intValue() == square.getY().intValue() &&
(current.getX().intValue()-1) == square.getX().intValue()){
result = true;
}
return result;
}
}