-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
222 lines (201 loc) · 6.1 KB
/
Point.java
File metadata and controls
222 lines (201 loc) · 6.1 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.lang.Math;
import java.util.ArrayList;
/**
* The Point class represents points on the grid where game pieces may appear.
*/
public class Point {
/**
* Constants in pixels for determining and locating valid Points on the grid.
*/
public static final int[] PIXEL_VALUES = getPixelValues();
public static final int NORTH = 0;
public static final int NORTHEAST = 45;
public static final int EAST = 90;
public static final int SOUTHEAST = 135;
public static final int SOUTH = 180;
public static final int SOUTHWEST = 225;
public static final int WEST = 270;
public static final int NORTHWEST = 315;
public static final int[] DIRECTIONS = {NORTH, NORTHEAST, EAST, SOUTHEAST,
SOUTH, SOUTHWEST, WEST, NORTHWEST};
/**
* Instance variables that represent a Cartesian (pixel) point on the grid.
*/
private int x;
private int y;
/**
* Class constructor initalizes this to (0, 0), which is off the grid.
*/
public Point() {
x = 0;
y = 0;
}
/**
* Class constructor that sets x and y to given parameters.
*
* @param newX distance on horizontal x-axis
* @param newY distance on vertical y-axis
*/
public Point(int newX, int newY) {
x = newX;
y = newY;
}
/**
* Generate an array of valid pixel values for Points on the grid.
*
* @return an array of valid pixel values for Points on the grid
*/
private static int[] getPixelValues() {
int[] pixelValues = new int[Display.GRID_ROWS];
for (int i = 0; i < Display.GRID_ROWS; i++) {
pixelValues[i] = Display.MARGIN * (i+1);
}
return pixelValues;
}
/**
* Accessor method for x.
*
* @return x
*/
public int getX() { return x; }
/**
* Accessor method for y.
*
* @return y
*/
public int getY() { return y; }
/**
* Mutator method for x.
*
* @param newX distance on horizontal x-axis
*/
public void setX(int newX) { x = newX; }
/**
* Mutator method for y.
*
* @param y distance on vertical y-axis
*/
public void setY(int newY) { y = newY; }
/**
* Mutator method for both x and y.
*
* @param newX distance on horizontal x-axis
* @param newY distance on vertical y-axis
*/
public void setAll(int newX, int newY) {
x = newX;
y = newY;
}
/**
* Format Point as String in Cartesian format.
*
* @return a String
*/
public String toString() {
return "(" + x + ", " + y + ")";
}
/**
* Compare two Point objects by content.
*
* @param obj generic Object that should be a Point
* @return true if this is equal to obj
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Point)) {
return false;
}
Point pointObj = (Point) obj;
if (this.getX() == pointObj.getX() && this.getY() == pointObj.getY()) {
return true;
} else {
return false;
}
}
/**
* Get closest Point value given a pixel distance.
*
* @param px the pixel distance of either x or y
* @return valid Point value closest to pixel
*/
private static int roundToPointValue(int px) {
int min = 1000; // cannot be a possible min value
int val = 0;
for (int value : Point.PIXEL_VALUES) {
int diff = Math.abs(px-value);
if (diff < min) {
min = diff;
val = value;
}
}
return val;
}
/**
* Create a Point object that is closest to given pixel distances.
*
* For example: 25px x 25px is (1, 1) Point on the grid.
*
* @param x distance on horizontal x-axis
* @param y distance on vertical y-axis
* @return the closest Point object
*/
public static Point convertFromPixel(int x, int y) {
int newX = Point.roundToPointValue(x);
int newY = Point.roundToPointValue(y);
return new Point(newX, newY);
}
/**
* Get adjacent Point in the given direction.
*
* @param dir the direction in which to look for a Point
* @return the adjacent Point or null if out of bounds of the grid
*/
public Point getAdjacent(int dir) {
int newX = 0;
int newY = 0;
if (dir == NORTH) {
newX = this.getX();
newY = this.getY() - Display.MARGIN;
} else if (dir == NORTHEAST) {
newX = this.getX() + Display.MARGIN;
newY = this.getY() - Display.MARGIN;
} else if (dir == EAST) {
newX = this.getX() + Display.MARGIN;
newY = this.getY();
} else if (dir == SOUTHEAST) {
newX = this.getX() + Display.MARGIN;
newY = this.getY() + Display.MARGIN;
} else if (dir == SOUTH) {
newX = this.getX();
newY = this.getY() + Display.MARGIN;
} else if (dir == SOUTHWEST) {
newX = this.getX() - Display.MARGIN;
newY = this.getY() + Display.MARGIN;
} else if (dir == WEST) {
newX = this.getX() - Display.MARGIN;
newY = this.getY();
} else if (dir == NORTHWEST) {
newX = this.getX() - Display.MARGIN;
newY = this.getY() - Display.MARGIN;
}
if (newX < Display.MARGIN || newX > Display.END_MARGIN ||
newY < Display.MARGIN || newY > Display.END_MARGIN) {
return null;
}
return new Point(newX, newY);
}
/**
* Add all Points adjacent to a specified location in clockwise direction
* to a list.
*
* @return a list of all adjacent Points including null if not valid
*/
public ArrayList<Point> getAdjacentAll () {
ArrayList<Point> list = new ArrayList<Point>();
for (int dir : Point.DIRECTIONS) {
Point adjPoint = this.getAdjacent(dir);
list.add(adjPoint);
}
return list;
}
}