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>
Abstraction over a list of 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
  • 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

      boolean contains​(int x, int y)
      Parameters:
      x -
      y -
      Returns:
      Whether this zone contains the coordinate (x,y).
    • contains

      boolean contains​(Coord c)
      Parameters:
      c -
      Returns:
      Whether this zone contains c.
    • contains

      boolean contains​(Zone other)
      Parameters:
      other -
      Returns:
      true if all cells of other are in this.
    • intersectsWith

      boolean intersectsWith​(Zone other)
      Parameters:
      other -
      Returns:
      true if this and other 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() and getHeight().
    • xBound

      int xBound​(boolean smallestOrBiggest)
      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

      int yBound​(boolean smallestOrBiggest)
      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

      Zone translate​(int x, int y)
      Returns:
      this shifted by (x,y)
    • getInternalBorder

      Returns:
      Cells in this that are adjacent to a cell not in this
    • 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 in this
    • 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. 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 to this (orthogonally or diagonally) have been added (i.e. it's this plus getExternalBorder()).