Package sim.util
Class Double2D
java.lang.Object
sim.util.NumberND
sim.util.Number2D
sim.util.Double2D
- All Implemented Interfaces:
Serializable
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 Summary
FieldsFields inherited from class sim.util.NumberND
TYPE_DOUBLE_2D, TYPE_DOUBLE_3D, TYPE_INT_2D, TYPE_INT_3D, TYPE_MUTABLE_DOUBLE_2D, TYPE_MUTABLE_DOUBLE_3D, TYPE_MUTABLE_INT_2D, TYPE_MUTABLE_INT_3D -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(double dx, double dy) add(int dx, int dy) final Double2Dfinal Double2Dfinal doubleangle()Returns the length of the vector between -Pi and Pi.doubledistance(double x, double y) Returns the distance FROM this Double2D TO the specified pointdoubleReturns the distance FROM this Double2D TO the specified point.doubleReturns the distance FROM this Double2D TO the specified point.doubleReturns the distance FROM this Double2D TO the specified point.doubleReturns the distance FROM this Double2D TO the specified point.doubledistanceSq(double x, double y) Returns the distance FROM this Double2D TO the specified pointdoubleReturns the distance FROM this Double2D TO the specified pointdoubleReturns the distance FROM this Double2D TO the specified point.doubledistanceSq(Int2D p) Returns the distance FROM this Double2D TO the specified point.doubleReturns the distance FROM this Double2D TO the specified point.final doubleTakes the dot product this Double2D with anotherfinal booleanReturns whether this NumberND is equivalent to some other NumberND.final booleanReturns true I am equal to valfinal booleanReturns true I am equal to valbooleanReturns true I am greater than or equal to than val in every dimensionbooleanReturns true I am greater than or equal to than val in every dimensiondoublegetVal(int val) Returns the value at position VAL in this NumberND (val should 0, 1, or sometimes 2)final doublegetX()final doublegetY()booleanReturns true I am greater than val in every dimensionbooleanReturns true I am greater than val in every dimensionfinal inthashCode()Provides a hashcode for this NumberND.final doublelength()Returns the vector length of the Double2Dfinal doublelengthSq()Returns the vector length of the Double2DbooleanReturns true I am less than or equal to than val in every dimensionbooleanReturns true I am less than or equal to than val in every dimensionbooleanReturns true I am less than val in every dimensionbooleanReturns true I am less than val in every dimensiondoublemanhattanDistance(double x, double y) Returns the manhtattan distance FROM this Double2D TO the specified pointdoubleReturns the manhtattan distance FROM this Double2D TO the specified pointdoubleReturns the manhtattan distance FROM this Double2D TO the specified pointdoubleReturns the manhtattan distance FROM this Double2D TO the specified pointdoubleReturns the manhtattan distance FROM this Double2D TO the specified pointdoubleReturns the manhtattan distance FROM this Double2D TO the specified pointfinal Double2Dmultiply(double val) Multiplies each element by scalar "val"final Double2Dnegate()Returns the negation of this Double2D.final Double2DNormalizes the vector (sets its length to 1).intReturns the number of dimensions of this NumberND (normally 2 or 3)double2D version of the cross product.final Double2Dresize(double dist) Scales the vector to length "dist".final Double2Drotate(double theta) Rotates the Double2D by theta radiansfinal Double2DSubtracts Double2D "other" from current Double2D using vector subtractionfinal Double2DSubtracts Int2D "other" from current Double2D using vector subtractiondouble[]toArray()double[]byte[]toBytes()Flattens out the NumberND to an array of bytes, including the NumberND subtype.Returns this NumberND in mathematical coordinates as a formatted String.toString()Returns this NumberND as a nicely formatted String.Methods inherited from class sim.util.NumberND
doubleFromBytes, doubleToBytes, fromBytes, getDistanceSq, getOffsets, intFromBytes, intToBytes, main, mutable
-
Field Details
-
x
public final double x -
y
public final double y
-
-
Constructor Details
-
Double2D
public Double2D() -
Double2D
-
Double2D
-
Double2D
-
Double2D
-
Double2D
-
Double2D
-
Double2D
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:NumberNDReturns the number of dimensions of this NumberND (normally 2 or 3)- Specified by:
numDimensionsin classNumberND
-
getVal
public double getVal(int val) Description copied from class:NumberNDReturns the value at position VAL in this NumberND (val should 0, 1, or sometimes 2) -
toBytes
public byte[] toBytes()Description copied from class:NumberNDFlattens out the NumberND to an array of bytes, including the NumberND subtype. -
getX
public final double getX() -
getY
public final double getY() -
toString
Description copied from class:NumberNDReturns this NumberND as a nicely formatted String. -
toCoordinates
Description copied from class:NumberNDReturns this NumberND in mathematical coordinates as a formatted String.- Specified by:
toCoordinatesin classNumberND
-
toPoint2D
-
toArray
public double[] toArray() -
toArrayAsDouble
public double[] toArrayAsDouble()- Specified by:
toArrayAsDoublein classNumberND
-
hashCode
public final int hashCode()Description copied from class:NumberNDProvides a hashcode for this NumberND. -
equals
Description copied from class:NumberNDReturns whether this NumberND is equivalent to some other NumberND. -
distance
public double distance(double x, double y) Returns the distance FROM this Double2D TO the specified point -
distance
Returns the distance FROM this Double2D TO the specified point. -
distance
Returns the distance FROM this Double2D TO the specified point. -
distance
Returns the distance FROM this Double2D TO the specified point. -
distance
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
Returns the distance FROM this Double2D TO the specified point. -
distanceSq
Returns the distance FROM this Double2D TO the specified point. -
distanceSq
Returns the distance FROM this Double2D TO the specified point. -
distanceSq
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
Returns the manhtattan distance FROM this Double2D TO the specified point -
manhattanDistance
Returns the manhtattan distance FROM this Double2D TO the specified point -
manhattanDistance
Returns the manhtattan distance FROM this Double2D TO the specified point -
manhattanDistance
Returns the manhtattan distance FROM this Double2D TO the specified point -
manhattanDistance
Returns the manhtattan distance FROM this Double2D TO the specified point -
add
-
subtract
Subtracts Double2D "other" from current Double2D using vector subtraction -
add
-
subtract
Subtracts Int2D "other" from current Double2D using vector subtraction -
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
Multiplies each element by scalar "val" -
resize
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
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
Takes the dot product this Double2D with another -
perpDot
2D version of the cross product. Rotates current Vector2D 90 degrees and takes the dot product of the result and Double2D "other" -
negate
Returns the negation of this Double2D. -
rotate
Rotates the Double2D by theta radians -
lt
Returns true I am less than val in every dimension -
gt
Returns true I am greater than val in every dimension -
leq
Returns true I am less than or equal to than val in every dimension -
geq
Returns true I am greater than or equal to than val in every dimension -
lt
Returns true I am less than val in every dimension -
gt
Returns true I am greater than val in every dimension -
leq
Returns true I am less than or equal to than val in every dimension -
geq
Returns true I am greater than or equal to than val in every dimension -
equals
Returns true I am equal to val -
equals
Returns true I am equal to val -
max
-
min
-
add
-
add
-