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
CoordPackerZone
s 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 class
Zone.Helper
static class
Zone.Skeleton
A convenience partial implementation. -
Method Summary
Modifier and Type Method Description boolean
contains(int x, int y)
boolean
contains(Zone other)
boolean
contains(Coord c)
Zone
extend()
Gets a new Zone that contains all the Coords inthis
plus all neighboring Coords, which can be orthogonally or diagonally adjacent to any Coord this has in it.List<Coord>
getAll()
Coord
getCenter()
double
getDiagonal()
Collection<Coord>
getExternalBorder()
Gets a Collection of Coord values that are not in this Zone, but are adjacent to it, either orthogonally or diagonally.int
getHeight()
Collection<Coord>
getInternalBorder()
int
getWidth()
boolean
intersectsWith(Zone other)
boolean
isEmpty()
int
size()
Zone
translate(int x, int y)
Zone
translate(Coord c)
int
xBound(boolean smallestOrBiggest)
int
yBound(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
other
are inthis
.
-
intersectsWith
- Parameters:
other
-- Returns:
- true if
this
andother
have 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
this
zone 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
this
zone 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
this
that 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
this
that has the smallest (or biggest) y-coordinate. Or -1 if the zone is empty.
-
getAll
- Returns:
- All cells in this zone.
-
translate
- Returns:
this
shifted by(c.x,c.y)
-
translate
- Returns:
this
shifted by(x,y)
-
getInternalBorder
- Returns:
- Cells in
this
that 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 inthis
plus 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
this
where cells adjacent tothis
(orthogonally or diagonally) have been added (i.e. it'sthis
plusgetExternalBorder()
).
-