-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCity.java
51 lines (45 loc) · 1.08 KB
/
City.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
package tsp;
public class City {
private int x;
private int y;
/**
* Initalize a city
*
* @param x X position of city
* @param y Y position of city
*/
public City(int x, int y){
this.x = x;
this.y = y;
}
/**
* Calculate distance from a city
*
* @param city The city to calculate the distance from
* @return distance The distance from the given city
*/
public double distanceFrom(City city){
// Give difference in x,y
double deltaXSq = Math.pow((city.getX() - this.getX()), 2);
double deltaYSq = Math.pow((city.getY() - this.getY()), 2);
// Calculate shortest path
double distance = Math.sqrt(Math.abs(deltaXSq + deltaYSq));
return distance;
}
/**
* Get x position of city
*
* @return x X position of city
*/
public int getX(){
return this.x;
}
/**
* Get y position of city
*
* @return y Y position of city
*/
public int getY(){
return this.y;
}
}