TorusAgent implements ILocated2D { public double[] distances(TorusAgent[] others) { double[] ds = new double[others.length]; for (int i = 0; i < others.length; i++) { ds[i] = this.distance(others[i]); } return ds; } TorusAgent(TorusWorld world, int startX, int startY, int type); int getAgentType(); @Override Tuple2D getPosition(); @Override double distance(ILocated2D other); @Override double getX(); @Override double getY(); void move(int dx, int dy); Color getColor(); boolean isCoLocated(TorusAgent other); boolean isCoLocated(TorusAgent[] others); TorusAgent closestAgent(TorusAgent[] others); double[] distances(TorusAgent[] others); double shortestXOffset(TorusAgent torusAgent); double shortestYOffset(TorusAgent torusAgent);  }
@Test public void testDistances() { TorusAgent[] preys = new TorusAgent[3]; TorusAgent[] preds = new TorusAgent[3]; preds[0] = new TorusAgent(new TorusWorld(100, 100), 10, 10, 0); preds[1] = new TorusAgent(new TorusWorld(100, 100), 20, 20, 0); preds[2] = new TorusAgent(new TorusWorld(100, 100), 30, 30, 0); preys[0] = new TorusAgent(new TorusWorld(100, 100), 40, 50, 1); preys[1] = new TorusAgent(new TorusWorld(100, 100), 95, 5, 1); preys[2] = new TorusAgent(new TorusWorld(100, 100), 70, 60, 1); double[] dists = pred.distances(preys); assertEquals(dists[0], 90.0, doubleThreshold); assertEquals(dists[1], 10.0, doubleThreshold); assertEquals(dists[2], 70.0, doubleThreshold); dists = pred.distances(preds); assertEquals(dists[0], 20.0, doubleThreshold); assertEquals(dists[1], 40.0, doubleThreshold); assertEquals(dists[2], 60.0, doubleThreshold); dists = prey.distances(preds); assertEquals(dists[0], 20.0, doubleThreshold); assertEquals(dists[1], 40.0, doubleThreshold); assertEquals(dists[2], 60.0, doubleThreshold); dists = prey.distances(preds); assertEquals(dists[0], 20.0, doubleThreshold); assertEquals(dists[1], 40.0, doubleThreshold); assertEquals(dists[2], 60.0, doubleThreshold); dists = preds[0].distances(preys); assertEquals(dists[0], 70.0, doubleThreshold); assertEquals(dists[1], 20.0, doubleThreshold); assertEquals(dists[2], 90.0, doubleThreshold); dists = preds[1].distances(preds); assertEquals(dists[0], 20.0, doubleThreshold); assertEquals(dists[1], 0.0, doubleThreshold); assertEquals(dists[2], 20.0, doubleThreshold); dists = preys[0].distances(preds); assertEquals(dists[0], 70.0, doubleThreshold); assertEquals(dists[1], 50.0, doubleThreshold); assertEquals(dists[2], 30.0, doubleThreshold); dists = preys[1].distances(preys); assertEquals(dists[0], 90.0, doubleThreshold); assertEquals(dists[1], 00.0, doubleThreshold); assertEquals(dists[2], 70.0, doubleThreshold); }