summaryrefslogtreecommitdiff
path: root/src/common/PeerCoordinates.java
blob: 13c32e13aea7ee5dde6b3df56780ec5089b38934 (plain)
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
package common;
import java.io.*;

public class PeerCoordinates implements Serializable {
	private static final long serialVersionUID = 3667108226485766929L;
	public static final int MIN_X = 0;
	public static final int MAX_X = 99;
	public static final int MIN_Y = 0;
	public static final int MAX_Y = 99;
	
	private int x;
	private int y;

	public PeerCoordinates() {
		setToRandomCoordinate();
	}
	
	public PeerCoordinates(int x, int y) {
		if (x < MIN_X || x > MAX_X || y < MIN_Y || y > MAX_Y)
			throw new IllegalArgumentException();
		
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}

	public double distanceTo(PeerCoordinates other) {
		return Math.sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
	}

	public String toString() {
		return "(" + x + ", " + y + ")";
	}

	public boolean equals(Object other) {
		if (!(other instanceof PeerCoordinates))
			return false;
		
		return x == ((PeerCoordinates)other).x && y == ((PeerCoordinates)other).y;
	}

	public void setToRandomCoordinate() {
		x = MIN_X + (int)((MAX_X - MIN_X + 1) * Math.random());
		y = MIN_Y + (int)((MAX_Y - MIN_Y + 1) * Math.random());
	}
}