replace direct access with getter/setter methods
[aco.git] / Coordinate.java
blob240bf4638cc57f393c6f80c6b98dbf931d59ac5f
1 import java.util.HashMap;
3 class Coordinate {
5 protected Graph graph;
6 protected final HashMap<Integer, CoordinatePair<Integer, Integer>> Coordinates;
8 Coordinate(Graph graph) {
9 this.graph = graph;
10 this.Coordinates = new HashMap<Integer, CoordinatePair<Integer, Integer>>(graph.getNumOfCities());
13 Coordinate(Graph graph, TSPLibReader tsplibreader) {
14 this.graph = graph;
15 this.Coordinates = tsplibreader.getCoordinates();
16 graph.setNumOfCities(Coordinates.size());
19 public int[][] computeDistances() {
21 int[][] Distance = new int[graph.getNumOfCities()][graph.getNumOfCities()];
23 for (int k1 : Coordinates.keySet()) {
24 for (int k2 : Coordinates.keySet()) {
25 if (k1 == k2)
26 Distance[k1][k2] = graph.getINFINITY();
27 else
28 Distance[k1][k2] = computeDistance(getCoordinates(k1), getCoordinates(k2));
32 return Distance;
35 public int computeDistance(CoordinatePair<Integer, Integer> CityOne,
36 CoordinatePair<Integer, Integer> CityTwo) {
37 /* pythagoras: a^2 + b^2 = c^2 => c = sqrt( a^2 + b^2 ) */
38 return (int)Math.sqrt(
39 Math.pow((double)(CityOne.getFirst() - CityTwo.getFirst()), 2) +
40 Math.pow( (double)(CityOne.getSecond() - CityTwo.getSecond()), 2 ) );
43 public CoordinatePair<Integer, Integer> getCoordinates(int City) {
44 return Coordinates.get(City);