Interface Zone
- All Superinterfaces:
Iterable<Coord>,Serializable
- All Known Subinterfaces:
ImmutableZone,MutableZone,Rectangle
- All Known Implementing Classes:
CoordPackerZone,GreasedRegion,ListZone,Rectangle.Impl,Zone.Skeleton
public interface Zone extends Serializable, Iterable<Coord>
Coord. This allows to use the short arrays
coming from CoordPacker, which are compressed for
better memory usage, regular lists of Coord, which are often the
simplest option, or GreasedRegions,
which are "greasy" in the fatty-food sense (they are heavier objects, and are
uncompressed) but also "greased" like greased lightning (they are very fast at
spatial transformations on their region).
Zones are Serializable, but serialization doesn't change the internal
representation (some would want to pack ListZone into
CoordPackerZones when serializing). I find that overzealous for a
simple interface. If you want your zones to be be packed when serialized,
create CoordPackerZone yourself. In squidlib-extra, GreasedRegions are
given slightly special treatment during that JSON-like serialization so they
avoid repeating certain information, but they are still going to be larger than
compressed short arrays from CoordPacker.
While CoordPacker produces short arrays that can be wrapped in CoordPackerZone
objects, and a List of Coord can be similarly wrapped in a ListZone object,
GreasedRegion extends Zone.Skeleton and so implements Zone itself.
Unlike CoordPackerZone, which is immutable in practice (changing the short
array reference is impossible and changing the elements rarely works as
planned), GreasedRegion is mutable for performance reasons, and may need copies
to be created if you want to keep around older GreasedRegions.
The correct method to implement a Zone efficiently is to first try
implementing the interface directly, looking at each method and thinking
whether you can do something smart for it. Once you've inspected all methods,
then extend Zone.Skeleton (instead of Object in the first place) so
that it'll fill for you the methods for which you cannot provide a smart
implementation.
- Author:
- smelC
- See Also:
CoordPacker,GreasedRegion
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classZone.Helperstatic classZone.SkeletonA convenience partial implementation. -
Method Summary
Modifier and Type Method Description booleancontains(int x, int y)booleancontains(Zone other)booleancontains(Coord c)Zoneextend()Gets a new Zone that contains all the Coords inthisplus all neighboring Coords, which can be orthogonally or diagonally adjacent to any Coord this has in it.List<Coord>getAll()CoordgetCenter()doublegetDiagonal()Collection<Coord>getExternalBorder()Gets a Collection of Coord values that are not in this Zone, but are adjacent to it, either orthogonally or diagonally.intgetHeight()Collection<Coord>getInternalBorder()intgetWidth()booleanintersectsWith(Zone other)booleanisEmpty()intsize()Zonetranslate(int x, int y)Zonetranslate(Coord c)intxBound(boolean smallestOrBiggest)intyBound(boolean smallestOrBiggest)
-
Method Details
-
isEmpty
boolean isEmpty()- Returns:
- Whether this zone is empty.
-
size
int size()- Returns:
- The number of cells that this zone contains (the size
getAll()).
-
contains
- Parameters:
x-y-- Returns:
- Whether this zone contains the coordinate (x,y).
-
contains
- Parameters:
c-- Returns:
- Whether this zone contains
c.
-
contains
- Parameters:
other-- Returns:
- true if all cells of
otherare inthis.
-
intersectsWith
- Parameters:
other-- Returns:
- true if
thisandotherhave a common cell.
-
getCenter
- Returns:
- The approximate center of this zone, or null if this zone is empty.
-
getWidth
int getWidth()- Returns:
- The distance between the leftmost cell and the rightmost cell, or
anything negative if
thiszone is empty; may be 0 if all cells are in one vertical line.
-
getHeight
int getHeight()- Returns:
- The distance between the topmost cell and the lowest cell, or
anything negative if
thiszone is empty; may be 0 if all cells are in one horizontal line.
-
getDiagonal
double getDiagonal()- Returns:
- The approximation of the zone's diagonal, using
getWidth()andgetHeight().
-
xBound
- Parameters:
smallestOrBiggest- if true, finds the smallest x-coordinate value; if false, finds the biggest.- Returns:
- The x-coordinate of the Coord within
thisthat has the smallest (or biggest) x-coordinate. Or -1 if the zone is empty.
-
yBound
- Parameters:
smallestOrBiggest- if true, finds the smallest y-coordinate value; if false, finds the biggest.- Returns:
- The y-coordinate of the Coord within
thisthat has the smallest (or biggest) y-coordinate. Or -1 if the zone is empty.
-
getAll
- Returns:
- All cells in this zone.
-
translate
- Returns:
thisshifted by(c.x,c.y)
-
translate
- Returns:
thisshifted by(x,y)
-
getInternalBorder
- Returns:
- Cells in
thisthat are adjacent to a cell not inthis
-
getExternalBorder
Gets a Collection of Coord values that are not in this Zone, but are adjacent to it, either orthogonally or diagonally. Related to the fringe() methods in CoordPacker and GreasedRegion, but guaranteed to use 8-way adjacency and to return a new Collection of Coord.- Returns:
- Cells adjacent to
this(orthogonally or diagonally) that aren't inthis
-
extend
Gets a new Zone that contains all the Coords inthisplus all neighboring Coords, which can be orthogonally or diagonally adjacent to any Coord this has in it. Related to the expand() methods in CoordPacker and GreasedRegion, but guaranteed to use 8-way adjacency and to return a new Zone.- Returns:
- A variant of
thiswhere cells adjacent tothis(orthogonally or diagonally) have been added (i.e. it'sthisplusgetExternalBorder()).
-