uchicago.src.sim.space
Class Object2DHexagonalGrid

java.lang.Object
  extended by uchicago.src.sim.space.Object2DGrid
      extended by uchicago.src.sim.space.Object2DHexagonalGrid
All Implemented Interfaces:
Discrete2DSpace
Direct Known Subclasses:
Object2DHexagonalTorus

public class Object2DHexagonalGrid
extends Object2DGrid

A discrete hexagonal grid of objects accessed by x and y coordinates. The hexagonal cells are referenced by x, y coordinates as follows:

       _
   _ / 1 \ _
 / 0 \ _ / 2 \
 \ _ / 4 \ _ /
 / 3 \ _ / 5 \
 \ _ / 7 \ _ /
 / 6 \ _ / 8 \
 \ _ /   \ _ /

 
Here we have a 3 x 3 hexagonal grid. The first row of cells is 0, 1, 2 such that 0,0 refers to cell 0, and 0,2 refers to cell 2. The next row of cells is 3, 4, 5, so 1,0 refers to cell 3 and so on. The last row of cells is 6, 7, and 8, so 2, 0 refers to cell 6. The ring of neighbors with radius one that surrounds cell 4 is composed of 1, 2, 5, 7, 3, and 0. The grid wraps as a toriod such that cell -1, 0 refers to cell 2 and cell 0, -1 is cell 6.


Field Summary
 
Fields inherited from class uchicago.src.sim.space.Object2DGrid
matrix, Mneigh, PGM_ASCII, RASTER_ASCII, VNneigh, xSize, ySize
 
Fields inherited from interface uchicago.src.sim.space.Discrete2DSpace
MOORE, VON_NEUMANN
 
Constructor Summary
Object2DHexagonalGrid(int xSize, int ySize)
          Constructs a grid with the specified size.
Object2DHexagonalGrid(java.lang.String fileName, int type)
          Constructs a grid from a file.
 
Method Summary
 java.util.Vector findMaximum(int x, int y, int range, boolean includeOrigin)
          Finds the maximum grid cell occupant within a specified range from the specified origin coordinate.
 java.util.Vector findMinimum(int x, int y, int range, boolean includeOrigin)
          Finds the minimum grid cell occupant within a specified range from the specified origin coordinate.
 java.util.Vector getMooreNeighbors(int x, int y, boolean returnNulls)
          The notion of a Moore neighborhood is incoherent for a hexagonal grid.
 java.util.Vector getMooreNeighbors(int x, int y, int xExtent, int yExtent, boolean returnNulls)
          The notion of a Moore neighborhood is incoherent for a hexagonal grid.
 java.util.Vector getNeighbors(int x, int y, boolean returnNulls)
          Gets the neighbors of the object at x, y.
 java.util.Vector getNeighbors(int x, int y, int extent, boolean returnNulls)
          Gets the rings of neighbors of the object at x, y.
 java.util.Vector getVonNeumannNeighbors(int x, int y, boolean returnNulls)
          The notion of a vonNeumann neighborhood is incoherent for a hexagonal grid.
 java.util.Vector getVonNeumannNeighbors(int x, int y, int xExtent, int yExtent, boolean returnNulls)
          The notion of a vonNeumann neighborhood is incoherent for a hexagonal grid.
 
Methods inherited from class uchicago.src.sim.space.Object2DGrid
findMaximum, findMinimum, getMatrix, getObjectAt, getSize, getSizeX, getSizeY, getValueAt, putObjectAt, putValueAt, rangeCheck, setComparator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Object2DHexagonalGrid

public Object2DHexagonalGrid(int xSize,
                             int ySize)
Constructs a grid with the specified size.

Parameters:
xSize - the size of the lattice in the x dimension.
ySize - the size of the lattice in the y dimension.

Object2DHexagonalGrid

public Object2DHexagonalGrid(java.lang.String fileName,
                             int type)
Constructs a grid from a file. Only ASCII PGM files are supported at this time. Code adapted from Nelson Minar's implementation of SugarScape with Swarm.

Method Detail

getNeighbors

public java.util.Vector getNeighbors(int x,
                                     int y,
                                     boolean returnNulls)
Gets the neighbors of the object at x, y. Objects are returned in clockwise order starting at 12 o'clock. The object at x, y is not returned.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
returnNulls - whether nulls (nothing at x,y) should be returned
Returns:
a vector of objects (and possibly nulls) in in clockwise order starting at 12 o'clock.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getNeighbors

public java.util.Vector getNeighbors(int x,
                                     int y,
                                     int extent,
                                     boolean returnNulls)
Gets the rings of neighbors of the object at x, y. The extent parameters specfies the radius of the rings to get and thus the number of neighbor rings. That is, an extent of 2 will return the ring radius 2 away from the specified point as well as the innermost ring of radius 1. Objects are returned in clockwise order starting with the object at 12 o'clock and at the outermost ring.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
extent - the extension of the neighborhood
returnNulls - whether nulls should be returned
Returns:
a vector of objects (and possibly nulls) in clockwise order starting with the object at 12 o'clock and at the outermost ring.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

getVonNeumannNeighbors

public java.util.Vector getVonNeumannNeighbors(int x,
                                               int y,
                                               boolean returnNulls)
                                        throws java.lang.UnsupportedOperationException
The notion of a vonNeumann neighborhood is incoherent for a hexagonal grid. Consequently this throws an UnsupportedOperation if called. To get the neighbors in a hexagonal grid, use the getNeighbors method.

Overrides:
getVonNeumannNeighbors in class Object2DGrid
Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
returnNulls - whether nulls (nothing at x,y) should be returned
Returns:
a vector of objects (and possibly nulls) in west, east, north, south order
Throws:
java.lang.UnsupportedOperationException

getVonNeumannNeighbors

public java.util.Vector getVonNeumannNeighbors(int x,
                                               int y,
                                               int xExtent,
                                               int yExtent,
                                               boolean returnNulls)
                                        throws java.lang.UnsupportedOperationException
The notion of a vonNeumann neighborhood is incoherent for a hexagonal grid. Consequently this throws an UnsupportedOperation if called. To get the neighbors in a hexagonal grid, use the getNeighbors method.

Overrides:
getVonNeumannNeighbors in class Object2DGrid
Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
xExtent - the extension of the neighborhood in the x direction
yExtent - the extension of the neighborhood in the y direction
returnNulls - whether nulls should be returned
Returns:
a vector of objects (and possibly nulls) in west, east, north, south order with the most distant object first.
Throws:
java.lang.UnsupportedOperationException

getMooreNeighbors

public java.util.Vector getMooreNeighbors(int x,
                                          int y,
                                          boolean returnNulls)
                                   throws java.lang.UnsupportedOperationException
The notion of a Moore neighborhood is incoherent for a hexagonal grid. Consequently this throws an UnsupportedOperation if called. To get the neighbors in a hexagonal grid, use the getNeighbors method.

Overrides:
getMooreNeighbors in class Object2DGrid
Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
returnNulls - should the returned Vector contain null objects
Returns:
a vector of objects (and possibly nulls) ordered by row starting with the "NW corner" and ending with the "SE corner."
Throws:
java.lang.UnsupportedOperationException

getMooreNeighbors

public java.util.Vector getMooreNeighbors(int x,
                                          int y,
                                          int xExtent,
                                          int yExtent,
                                          boolean returnNulls)
                                   throws java.lang.UnsupportedOperationException
The notion of a Moore neighborhood is incoherent for a hexagonal grid. Consequently this throws an UnsupportedOperation if called. To get the neighbors in a hexagonal grid, use the getNeighbors method.

Overrides:
getMooreNeighbors in class Object2DGrid
Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
xExtent - the extension of the neighborhood in the x direction
yExtent - the extension of the neighborhood in the y direction
returnNulls - should the returned Vector contain null objects
Returns:
a vector of objects (and possibly nulls) ordered by row starting with the "NW corner" and ending with the "SE corner."
Throws:
java.lang.UnsupportedOperationException

findMaximum

public java.util.Vector findMaximum(int x,
                                    int y,
                                    int range,
                                    boolean includeOrigin)
Finds the maximum grid cell occupant within a specified range from the specified origin coordinate. Maximum is determined by the default or user supplied comparator class. The default comparator compares objects using the >, <, and = operators on the hashcode of the objects.

Parameters:
x - the x origin coordinate
y - the y origin coordinate
range - the range out from the coordinate to search
includeOrigin - include object at origin in search
Returns:
the Objects determined to be the maximum.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)

findMinimum

public java.util.Vector findMinimum(int x,
                                    int y,
                                    int range,
                                    boolean includeOrigin)
Finds the minimum grid cell occupant within a specified range from the specified origin coordinate. Minimum is determined by the default or user supplied comparator class. The default comparator compares objects using the >, <, and = operators on the hashcode of the objects.

Parameters:
x - the x origin coordinate
y - the y origin coordinate
range - the range out from the coordinate to search
includeOrigin - include object at origin in search
Returns:
the Objects determined to be the maximum.
Throws:
java.lang.IndexOutOfBoundsException - if the given coordinates are out of range (x < 0 || x >= xSize || y < 0 || y >= ySize)