Package sim.util

Class Double2D

All Implemented Interfaces:
Serializable

public final class Double2D extends Number2D
Double2D is more or less the same class as java.awt.geom.Point2D.Double, 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 Point2D? Because Point2D is broken with respect to hash tables. You use Point2D as a key in a hash table at your own peril. Try this: hash an object by a Point2D as key. Then change the x value of the original Point2D. Ta-da! The object is lost in the hash table.

One day in the far future, Double3D 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.

Double2D.equals(...) can compare by value against other Int2Ds and Double2Ds.

See Also:
  • Field Details

    • x

      public final double x
    • y

      public final double y
  • Constructor Details

    • Double2D

      public Double2D()
    • Double2D

      public Double2D(Int2D p)
    • Double2D

      public Double2D(MutableInt2D p)
    • Double2D

      public Double2D(MutableDouble2D p)
    • Double2D

      public Double2D(Point p)
    • Double2D

      public Double2D(Point2D.Double p)
    • Double2D

      public Double2D(Point2D.Float p)
    • Double2D

      public Double2D(Point2D p)
      Only included for completeness' sakes, in case a new Point2D subclass is created in the future.
    • Double2D

      public Double2D(double x, double y)
    • Double2D

      public Double2D(double[] 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 double getX()
    • getY

      public final double getY()
    • 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
    • toPoint2D

      public Point2D.Double toPoint2D()
    • toArray

      public double[] 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
    • distance

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

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

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

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

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

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

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

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

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

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

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

      public double manhattanDistance(Double2D p)
      Returns the manhtattan distance FROM this Double2D TO the specified point
    • manhattanDistance

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

      public double manhattanDistance(MutableDouble2D p)
      Returns the manhtattan distance FROM this Double2D TO the specified point
    • manhattanDistance

      public double manhattanDistance(MutableInt2D p)
      Returns the manhtattan distance FROM this Double2D TO the specified point
    • manhattanDistance

      public double manhattanDistance(Point2D p)
      Returns the manhtattan distance FROM this Double2D TO the specified point
    • add

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

      public final Double2D subtract(Double2D other)
      Subtracts Double2D "other" from current Double2D using vector subtraction
      Specified by:
      subtract in class Number2D
    • add

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

      public final Double2D subtract(Int2D other)
      Subtracts Int2D "other" from current Double2D using vector subtraction
      Specified by:
      subtract in class Number2D
    • length

      public final double length()
      Returns the vector length of the Double2D
    • angle

      public final double angle()
      Returns the length of the vector between -Pi and Pi.
    • lengthSq

      public final double lengthSq()
      Returns the vector length of the Double2D
    • multiply

      public final Double2D multiply(double val)
      Multiplies each element by scalar "val"
    • resize

      public final Double2D resize(double dist)
      Scales the vector to length "dist". dist must be a finite value. If the vector has NaN, zero, or infinite values, then the vector cannot be resized to any length except for 0: other lengths will throw an exception in this case.
    • normalize

      public final Double2D normalize()
      Normalizes the vector (sets its length to 1). If the vector has NaN or infinite values, or has all zero values, then an exception will be thrown.
    • dot

      public final double dot(Double2D other)
      Takes the dot product this Double2D with another
    • perpDot

      public double perpDot(Double2D other)
      2D version of the cross product. Rotates current Vector2D 90 degrees and takes the dot product of the result and Double2D "other"
    • negate

      public final Double2D negate()
      Returns the negation of this Double2D.
    • rotate

      public final Double2D rotate(double theta)
      Rotates the Double2D by theta radians
    • lt

      public boolean lt(Double2D val)
      Returns true I am less than val in every dimension
    • gt

      public boolean gt(Double2D val)
      Returns true I am greater than val in every dimension
    • leq

      public boolean leq(Double2D val)
      Returns true I am less than or equal to than val in every dimension
    • geq

      public boolean geq(Double2D val)
      Returns true I am greater than or equal to than val in every dimension
    • lt

      public boolean lt(Int2D val)
      Returns true I am less than val in every dimension
    • gt

      public boolean gt(Int2D val)
      Returns true I am greater than val in every dimension
    • leq

      public boolean leq(Int2D val)
      Returns true I am less than or equal to than val in every dimension
    • geq

      public boolean geq(Int2D val)
      Returns true I am greater than or equal to than val in every dimension
    • equals

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

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

      public Double2D max(Double2D val)
    • min

      public Double2D min(Double2D val)
    • add

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

      public Double2D add(int dx, int dy)