Package sim.util

Class Int2D

All Implemented Interfaces:
Serializable

public final class Int2D extends Number2D
Int2D is more or less the same class as java.awt.Point, but it is immutable: once the x and y values are set, they cannot be changed (they're final). Why use this immutable class when you could just use Point? Because Point is broken with respect to hash tables. You use Point as a key in a hash table at your own peril. Try this: hash an object by a Point as key. Then change the x value of the original Point. Ta-da! The object is lost in the hash table.

One day in the far future, Int2D should also be HIGHLY efficient; since it is immutable, it can be passed by value rather than by pointer by a smart compiler. Not today, though. But it's not bad.

This class has an elaborate hash code generation that is much more random than Sun's standard generator, but takes more time. For very large numbers of objects, this is a good idea, but we may change it to a simpler version in the future.

Int2D.equals(...) can compare by value against other Int2Ds, MutableInt2Ds, Double2Ds, and MutableDouble2Ds.

See Also:
  • Field Details

    • x

      public final int x
    • y

      public final int y
  • Constructor Details

    • Int2D

      public Int2D()
    • Int2D

      public Int2D(Point p)
    • Int2D

      public Int2D(MutableInt2D p)
    • Int2D

      public Int2D(int x, int y)
    • Int2D

      public Int2D(int[] vals)
  • Method Details

    • numDimensions

      public int numDimensions()
      Description copied from class: NumberND
      Returns the number of dimensions of this NumberND (normally 2 or 3)
      Specified by:
      numDimensions in class NumberND
    • getVal

      public double getVal(int val)
      Description copied from class: NumberND
      Returns the value at position VAL in this NumberND (val should 0, 1, or sometimes 2)
      Specified by:
      getVal in class NumberND
    • toBytes

      public byte[] toBytes()
      Description copied from class: NumberND
      Flattens out the NumberND to an array of bytes, including the NumberND subtype.
      Specified by:
      toBytes in class NumberND
    • getX

      public final int getX()
    • getY

      public final int getY()
    • toPoint2D

      public Point2D.Double toPoint2D()
    • toPoint

      public Point toPoint()
    • toString

      public String toString()
      Description copied from class: NumberND
      Returns this NumberND as a nicely formatted String.
      Specified by:
      toString in class NumberND
    • toCoordinates

      public String toCoordinates()
      Description copied from class: NumberND
      Returns this NumberND in mathematical coordinates as a formatted String.
      Specified by:
      toCoordinates in class NumberND
    • toArray

      public int[] toArray()
    • toArrayAsDouble

      public double[] toArrayAsDouble()
      Specified by:
      toArrayAsDouble in class NumberND
    • hashCode

      public final int hashCode()
      Description copied from class: NumberND
      Provides a hashcode for this NumberND.
      Specified by:
      hashCode in class NumberND
    • equals

      public final boolean equals(Object obj)
      Description copied from class: NumberND
      Returns whether this NumberND is equivalent to some other NumberND.
      Specified by:
      equals in class NumberND
    • equals

      public boolean equals(Int2D val)
      Returns true I am equal to val
    • equals

      public boolean equals(Double2D val)
      Returns true I am equal to val
    • distance

      public double distance(double x, double y)
      Returns the distance FROM this Int2D TO the specified point
    • distance

      public double distance(Double2D p)
      Returns the distance FROM this Int2D TO the specified point.
    • distance

      public double distance(MutableInt2D p)
      Returns the distance FROM this Int2D TO the specified point.
    • distance

      public double distance(Int2D p)
      Returns the distance FROM this Int2D TO the specified point.
    • distance

      public double distance(Point2D p)
      Returns the distance FROM this Int2D TO the specified point.
    • distanceSq

      public double distanceSq(double x, double y)
      Returns the squared distance FROM this Int2D TO the specified point
    • distanceSq

      public double distanceSq(Double2D p)
      Returns the squared distance FROM this Int2D TO the specified point.
    • distanceSq

      public double distanceSq(Point2D p)
      Returns the squared distance FROM this Int2D TO the specified point
    • distanceSq

      public double distanceSq(MutableInt2D p)
      Returns the squared distance FROM this Int2D TO the specified point.
    • distanceSq

      public double distanceSq(Int2D p)
      Returns the squared distance FROM this Int2D TO the specified point.
    • manhattanDistance

      public long manhattanDistance(int x, int y)
      Returns the manhattan distance FROM this Int2D TO the specified point.
    • manhattanDistance

      public long manhattanDistance(MutableInt2D p)
      Returns the manhattan distance FROM this Int2D TO the specified point.
    • manhattanDistance

      public long manhattanDistance(Int2D p)
      Returns the manhattan distance FROM this Int2D TO the specified point.
    • manhattanDistance

      public long manhattanDistance(Point p)
      Returns the manhattan distance FROM this Int2D TO the specified point.
    • add

      public Double2D add(double dx, double dy)
      Specified by:
      add in class Number2D
    • add

      public Int2D add(int dx, int dy)
    • add

      public Double2D add(Double2D other)
      Specified by:
      add in class Number2D
    • subtract

      public Double2D subtract(Double2D other)
      Specified by:
      subtract in class Number2D
    • add

      public Int2D add(Int2D other)
      Specified by:
      add in class Number2D
    • subtract

      public Int2D subtract(Int2D other)
      Specified by:
      subtract in class Number2D
    • max

      public Int2D max(Int2D val)
    • min

      public Int2D min(Int2D val)