public interface Zone extends java.io.Serializable, java.lang.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.
CoordPacker
,
GreasedRegion
Modifier and Type | Interface and Description |
---|---|
static class |
Zone.Helper |
static class |
Zone.Skeleton
A convenience partial implementation.
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(Coord c) |
boolean |
contains(int x,
int y) |
boolean |
contains(Zone other) |
Zone |
extend()
Gets a new Zone that contains all the Coords in
this plus all
neighboring Coords, which can be orthogonally or diagonally adjacent
to any Coord this has in it. |
java.util.List<Coord> |
getAll() |
Coord |
getCenter() |
double |
getDiagonal() |
java.util.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() |
java.util.Collection<Coord> |
getInternalBorder() |
int |
getWidth() |
boolean |
intersectsWith(Zone other) |
boolean |
isEmpty() |
int |
size() |
Zone |
translate(Coord c) |
Zone |
translate(int x,
int y) |
int |
xBound(boolean smallestOrBiggest) |
int |
yBound(boolean smallestOrBiggest) |
boolean isEmpty()
int size()
getAll()
).boolean contains(int x, int y)
x
- y
- boolean contains(Coord c)
c
- c
.boolean contains(Zone other)
other
- other
are in this
.boolean intersectsWith(Zone other)
other
- this
and other
have a common cell.Coord getCenter()
int getWidth()
this
zone is empty; may be 0 if all cells
are in one vertical line.int getHeight()
this
zone is empty; may be 0 if all cells
are in one horizontal line.double getDiagonal()
getWidth()
and getHeight()
.int xBound(boolean smallestOrBiggest)
smallestOrBiggest
- if true, finds the smallest x-coordinate value;
if false, finds the biggest.this
that has the
smallest (or biggest) x-coordinate. Or -1 if the zone is empty.int yBound(boolean smallestOrBiggest)
smallestOrBiggest
- if true, finds the smallest y-coordinate value;
if false, finds the biggest.this
that has the
smallest (or biggest) y-coordinate. Or -1 if the zone is empty.java.util.List<Coord> getAll()
Zone translate(int x, int y)
this
shifted by (x,y)
java.util.Collection<Coord> getInternalBorder()
this
that are adjacent to a cell not in
this
java.util.Collection<Coord> getExternalBorder()
this
(orthogonally or diagonally) that
aren't in this
Zone extend()
this
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.this
where cells adjacent to this
(orthogonally or diagonally) have been added (i.e. it's this
plus getExternalBorder()
).Copyright © Eben Howard 2012–2022. All rights reserved.