-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridCoordinate.java
More file actions
207 lines (188 loc) · 4.74 KB
/
GridCoordinate.java
File metadata and controls
207 lines (188 loc) · 4.74 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* This class represents objects with x and y coordinate values and
* directional (N,S,E,W) values. Booleans are used to store whether there
* is a block above, below, to the left, or to the right of the object.
* GridCoordinate objects are used in the GridGraph class to represent
* the vertices of a graph.
* <p>
* Amy and Michelle were equally responsible for the implementation of
* this class.
*
* @author Amy Qiu, Michelle Gao
* @version December 2 2014
*/
public class GridCoordinate {
// instance variables
private int x;
private int y;
private boolean n;
private boolean w;
private boolean s;
private boolean e;
private GridCoordinate previous; //used in GridGraph.bfs()
/**
* Constructorthat takes in an (x,y) coordinate pair.
* The first coordinate has x = 0 and y = 0.
*
* @param x the x-coordinate
* @param y the y-coordinate
*/
public GridCoordinate(int x, int y){
this.x = x;
this.y = y;
n = true;
w = true;
s = true;
e = true;
}
/**
* Sets n (northern neighbor).
*
* @param value a boolean for whether this object has a northern neighbor
*/
public void setN(boolean value){
n = value;
}
/**
* Sets e (eastern neighbor).
*
* @param value a boolean for whether this object has a eastern neighbor
*/
public void setE(boolean value){
e = value;
}
/**
* Sets s (southern neighbor).
*
* @param value a boolean for whether this object has a southern neighbor
*/
public void setS(boolean value){
s = value;
}
/**
* Sets w (western neighbor).
*
* @param value a boolean for whether this object has a western neighbor
*/
public void setW(boolean value){
w = value;
}
/**
* Returns n (northern neighbor).
*
* @return a boolean for whether this object has a northern neighbor
*/
public boolean getN(){
return n;
}
/**
* Returns e (eastern neighbor).
*
* @return a boolean for whether this object has a eastern neighbor
*/
public boolean getE(){
return e;
}
/**
* Returns s (southern neighbor).
*
* @return a boolean for whether this object has a southern neighbor
*/
public boolean getS(){
return s;
}
/**
* Returns w (western neighbor).
*
* @return a boolean for whether this object has a western neighbor
*/
public boolean getW(){
return w;
}
/**
* Returns a 4-digit (XXYY) int representation of a coordinate pair.
* The first 2 digits represent the x coor, last 2 digits the y coor.
*
* @return a 4-digit int representation of a coordinate pair
*/
public int getCoordinate(){
int result = 100*x + y;
//x coor is XXYY/100
//y coor is XXYY%100
return result;
}
/**
* Returns a backwards 4-digit (YYXX) int representation of a coordinate pair.
* The first 2 digits represent the y coor, last 2 digits the x coor.
*
* @return a 4-digit int representation of a coordinate pair
*/
public int getBackwardsCoordinate(){
return 100*y + x;
}
/**
* Returns x (x-coordinate).
*
* @return the x-coordinate
*/
public int getX() {
return x;
}
/**
* Returns y (y-coordinate).
*
* @return the y-coordinate
*/
public int getY() {
return y;
}
/**
* Sets previous reference.
*
* @param n the GridCoordinate to be stored in the previous reference.
*/
public void setPrevious(GridCoordinate n) {
previous = n;
}
/**
* Returns the GridCoordinate stored in the previous reference.
*
* @return the GridCoordinate stored in the previous reference.
*/
public GridCoordinate getPrevious() {
return previous;
}
/**
* Returns true if this GridCoordinate's previous reference isn't null.
*
* @return the boolean that indicates whether the previous refers to null
*/
public boolean hasPrevious() {
return (previous != null);
}
/**
* Returns true if this GridCoordinate shares the same x and y values
* as the given GridCoordinate.
*
* @param other the GridCoordinate to be compared
* @return boolean that indicates equality
*/
public boolean equals(GridCoordinate other) {
return (this.x == other.x && this.y == other.y);
}
/**
* Returns a String representation of a GridCoordinate object.
*
* @return a String that represents the coordinate and directional values
* of this object.
*/
public String toString(){
String result = "";
result += "("+x+","+y+")";
result += "N:"+ (n == true ? 1 : 0);
result += " E:" + (e == true ? 1 : 0);
result += " S:" + (s == true ? 1 : 0);
result += " W:" + (w == true ? 1 : 0) + " ";
return result;
}
}