class TileCodeComparator implements Comparator<TileCode> {

	@Override
	public int compare(TileCode o1, TileCode o2) {
		// -1: o1 is closer to center
		//  0: same distance to center
		//  1: o2 is closer to center
		int x1 = o1.getX() - offsetX;
		int x2 = o2.getX() - offsetX;
		int y1 = o1.getY() - offsetY;
		int y2 = o2.getY() - offsetY;

		int o1d = Math.max(Math.abs(x1), Math.abs(y1));
		int o2d = Math.max(Math.abs(x2), Math.abs(y2));

		if (x1 == x2 && y1 == y2) { // points same position
			return 0;
		} else if (o1d != o2d) { // points on different sphere
			return (o1d < o2d) ? -1 : 1;
		} else { // points on same sphere
			if (y1 == o1d) { // Case: o1 on top row
				if (y1 == y2) {
					return (x1 < x2) ? -1 : 1;
				} else {
					return -1;
				}
			} else if (x1 == o1d) { // Case: o1 on right column
				if (y2 == o1d) {
					return 1;
				} else if (x1 == x2) {
					return (y1 > y2) ? -1 : 1;
				} else {
					return -1;
				}
			} else if (y1 == -1 * o1d) { // Case: o1 on bottom row
				if (y2 == o1d || x2 == o1d) {
					return 1;
				} else if (y1 == y2) {
					return (x1 > x2) ? -1 : 1;
				} else {
					return -1;
				}
			} else if (x1 == -1 * o1d) { // Case: o1 on left column
				if (y2 == o1d || x2 == o1d || y2 == -1 * o1d) {
					return 1;
				} else {
					return (y1 < y2) ? -1 : 1;
				}
			}
			return 0; // Should not reach this
		}
	}

	public  TileCodeComparator(int offsetX, int offsetY);

}