-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoordinate.java
94 lines (81 loc) · 1.97 KB
/
Coordinate.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
/**
* Abstraction for a Coordinate. Why do this over using a simple pair, or even
* coordinates encoded as integers? Good question - adding in this abstraction
* makes the code elsewhere easier to understand
*
* @author Richard
* @version Dec 30, 2016
* @author Project: Brick-Pop-Solver
*
*/
public class Coordinate implements Comparable<Coordinate>
{
int i, j;
/**
* @param i
* first value in a coordinate pair
* @param j
* second value in a coordinate pair
*/
public Coordinate( int i, int j )
{
this.i = i;
this.j = j;
}
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo( Coordinate other )
{
if ( this.i < other.i || ( this.i == other.i && this.j < other.j ) )
{
return 1;
}
if ( this.i > other.i || ( this.i == other.i && this.j > other.j ) )
{
return -1;
}
return 0;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals( Object other )
{
if ( !( other instanceof Coordinate ) )
{
return false;
}
Coordinate converted = (Coordinate)other;
return converted.i == this.i && converted.j == this.j;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return "(" + i + ", " + j + ")";
}
/**
* Returns a new Coordinate object shifted by the supplied offsets
*
* @param i_off
* offset to i
* @param j_off
* offset to j
* @return a new Coordinate object shifted by the supplied offsets
*/
public Coordinate offset( int i_off, int j_off )
{
return new Coordinate( this.i + i_off, this.j + j_off );
}
}