-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
62 lines (48 loc) · 1.22 KB
/
Point.java
File metadata and controls
62 lines (48 loc) · 1.22 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
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point(Point p) {
this.x = p.getX();
this.y = p.getY();
}
public void move(Direction d, int value) {
switch(d) {
case UP: this.y -= value; break;
case DOWN: this.y += value; break;
case RIGHT: this.x += value; break;
case LEFT: this.x -= value; break;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Point setX(int x) {
this.x = x;
return this;
}
public Point setY(int y) {
this.y = y;
return this;
}
public boolean equals(Point p) {
return this.x == p.getX() && this.y == p.getY();
}
public String toString() {
return "(" + x + ", " + y + ")";
}
public boolean intersects(Point p) {
return intersects(p, 10);
}
public boolean intersects(Point p, int tolerance) {
int diffX = Math.abs(x - p.getX());
int diffY = Math.abs(y - p.getY());
return this.equals(p) || (diffX <= tolerance && diffY <= tolerance);
}
}