-
Notifications
You must be signed in to change notification settings - Fork 1
/
Point.java
91 lines (80 loc) · 3.07 KB
/
Point.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
/*
Copyright (C) 2004 Geoffrey Alan Washburn
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
*/
import java.io.Serializable;
/**
* An integral representation of a point in two dimensional space.
* @author Geoffrey Washburn <<a href="mailto:geoffw@cis.upenn.edu">geoffw@cis.upenn.edu</a>>
* @version $Id: Point.java 339 2004-01-23 20:06:22Z geoffw $
*/
public class Point implements Serializable {
private final int x;
private final int y;
/**
* Create a new {@link Point} from coordinates.
* @param x Coordinate in the X-dimension.
* @param y Coordinate in the Y-dimension.
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Create a new {@link Point} from another {@link Point}.
* @param point {@link Point} to copy from.
*/
public Point(Point point) {
assert(point != null);
this.x = point.x;
this.y = point.y;
}
/**
* Obtain the X-coordinate of this {@link Point}.
* @return The X-coordinate of this {@link Point}.
*/
public int getX() {
return x;
}
/**
* Obtain the Y-coordinate of this {@link Point}.
* @return The Y-coordinate of this {@link Point}.
*/
public int getY() {
return y;
}
/**
* Create a new {@link Point} by moving from this
* one by a single unit in a given {@Direction}.
* North corresponds to postive Y-movement.
* East corresponds to postive X-movement.
* South corresponds to negative Y-movement.
* West corresponds to negative X-movement.
* @return A new {@link Point} one unit away from this one in the given {@link Direction}.
*/
Point move(Direction d) {
assert(d != null);
if(d.equals(Direction.North)) {
return new Point(x, y + 1);
} else if(d.equals(Direction.East)) {
return new Point(x + 1, y);
} else if(d.equals(Direction.South)) {
return new Point(x, y - 1);
} else if(d.equals(Direction.West)) {
return new Point(x - 1, y);
}
/* Impossible */
return null;
}
}