import java.util.*; import java.awt.*; public class Animal implements Agent { protected Environment world = null; protected ArrayList agents = null; protected int x = 0; protected int y = 0; // Neighbourhood variables. // Neighbourhood 'radius'. // At the moment the neighbourhood is a hexagon by default, so this is the longest // distance from the centre to the edge. private int neighbourhoodRadius = 5; // Trig result used for laying out the hexagonal neighbourhood. private int opp = (int)Math.abs((Math.sin(Math.toRadians(30.0)) * (double)neighbourhoodRadius)); // Trig result used for laying out the hexagonal neighbourhood. private int adj = (int)Math.abs((Math.cos(Math.toRadians(30.0)) * (double)neighbourhoodRadius)); public Animal (Environment world, ArrayList agents) { this.world = world; this.agents = agents; x = 50; y = 150; } public void run () { } public int getX() { return x; } public int getY() { return y; } // Polymorphic neighbourhood methods. // Gets the hexagonal neighbourhood around the current location. // The advantage with a Polygon is that you can use Polygon.contains(x,y) to see if // other agents etc. fall inside it. public Polygon getNeighbourhood() { return getNeighbourhood(this.x,this.y); } // Gets a hexagonal neighbourhood around an arbitrary location. // Can be used, for example, to check future neighbourhoods for suitability // before moving to them. // The advantage with a Polygon is that you can use Polygon.contains(x,y) to see if // other agents etc. fall inside it. public Polygon getNeighbourhood (int x, int y) { // Make an array of x coordinates. int[] xs = { x - adj, x, x + adj, x + adj, x, x - adj }; // Make an array of y coordinates. int[] ys = { y - opp, y - neighbourhoodRadius, y - opp, y + opp, y + neighbourhoodRadius, y + opp }; // Use these to make and return a Polygon. return new Polygon (xs,ys, xs.length); } }