/**
* --Copyright notice--
*
* Copyright (c) School of Geography, University of Leeds.
* http://www.geog.leeds.ac.uk/
* This software is licensed under 'The Artistic License' which can be found at
* the Open Source Initiative website at...
* http://www.opensource.org/licenses/artistic-license.php
* Please note that the optional Clause 8 does not apply to this code.
*
* The Standard Version source code, and associated documentation can be found at...
* [online] http://mass.leeds.ac.uk/
*
*
* --End of Copyright notice--
*
*/
import java.util.*;
import java.awt.*;
/**
* Implements basic spatial elements of an agent.
* The expectation is that this would be extended, but it is left non-abstract
* incase wanted for early model development.
* Each animal has an x and y coordinate, plus a neighbourhood which could, for example,
* represent a perceived area.
* @version 1.0
* @author Andy Evans
*/
public class Animal implements Agent {
// Note use of protected keyword to allow extension access.
/** Reference to the environment set up in Model. */
protected Environment world = null;
/** Reference to the agent list set up in Model. */
protected ArrayList agents = null;
/** Spatial x coordinate. */
protected int x = 0;
/** Spatial y coordinate. */
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));
/**
* Constructor. Just defaults the x and y to 50 and 50.
* @param worldIn environment for the model
* @param agentsIn list of all the agents in the model
*/
public Animal (Environment worldIn, ArrayList agentsIn) {
world = worldIn;
agents = agentsIn;
x = 50;
y = 150;
}
/**
* Empty run method.
*/
public void run () {
}
/**
* Gets the x coordinate.
* @return spatial location x coordinate
*/
public int getX() {
return x;
}
/**
* Gets the y coordinate.
* @return spatial location y coordinate
*/
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.
* @return neighbourhood as a polygon in the global coordinate system
*/
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.
* @param x the x location in the global coordinate system
* @param y the y location in the global coordinate system
* @return neighbourhood around x,y as a polygon in the global coordinate system
*/
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);
}
}