Class Vector2D
- java.lang.Object
-
- edu.rit.vector.Vector2D
-
- All Implemented Interfaces:
- java.io.Externalizable, java.io.Serializable
public class Vector2D extends java.lang.Object implements java.io.ExternalizableClass Vector2D provides a two-dimensional mathematical vector of type double. The vector's components are stored in the fields x and y.Call one of the following static factory methods to create a buffer for sending and receiving Vector2D items in a message. To reduce the number of bytes in the message, thereby improving performance, each of these methods returns a buffer which treats a vector as a pair of doubles (rather than an object). This buffer does not support reduction. The buffer's getReductionBuf() method throws an UnsupportedOperationException.
- doubleBuffer (Vector2D[])
- doubleSliceBuffer (Vector2D[], Range)
- doubleSliceBuffers (Vector2D[], Range[])
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description doublexX component.doubleyY component.
-
Constructor Summary
Constructors Constructor and Description Vector2D()Construct a new vector.Vector2D(double x, double y)Construct a new vector with the given X and Y components.Vector2D(Vector2D theVector)Construct a new vector that is a copy of the given vector.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description Vector2Dadd(Vector2D theVector)Add the given vector to this vector.doublearg()Determine the argument of this vector.Vector2Dassign(double x, double y)Assign the given X and Y components to this vector.Vector2Dassign(Vector2D theVector)Assign (copy) the given vector to this vector.Vector2Dclear()Clear this vector.doubledist(Vector2D theVector)Determine the distance (magnitude of the difference) between this vector and the given vector.Vector2Ddiv(double a)Divide this vector by the given scalar.doubledot(Vector2D theVector)Determine the dot product of this vector and the given vector.static DoubleBufdoubleBuffer(Vector2D[] theArray)Create a buffer for the entire given vector array.static DoubleBufdoubleSliceBuffer(Vector2D[] theArray, Range theRange)Create a buffer for one slice of the given vector array.static DoubleBuf[]doubleSliceBuffers(Vector2D[] theArray, Range[] theRanges)Create an array of buffers for multiple slices of the given vector array.doublemag()Determine the magnitude of this vector.Vector2Dmul(double a)Multiply this vector by the given scalar.Vector2Dneg()Negate this vector.Vector2Dnorm()Normalize this vector.voidreadExternal(java.io.ObjectInput in)Read this vector from the given object input stream.Vector2Drotate180()Rotate this vector 180 degrees.Vector2Drotate270()Rotate this vector 270 degrees counterclockwise (90 degrees clockwise).Vector2Drotate90()Rotate this vector 90 degrees counterclockwise.doublesqrDist(Vector2D theVector)Determine the squared distance (squared magnitude of the difference) between this vector and the given vector.doublesqrMag()Determine the squared magnitude of this vector.Vector2Dsub(Vector2D theVector)Subtract the given vector from this vector.java.lang.StringtoString()Returns a string version of this vector.voidwriteExternal(java.io.ObjectOutput out)Write this vector to the given object output stream.
-
-
-
Constructor Detail
-
Vector2D
public Vector2D()
Construct a new vector. The X and Y components are initialized to 0.
-
Vector2D
public Vector2D(double x, double y)Construct a new vector with the given X and Y components.- Parameters:
x- Initial X component.y- Initial Y component.
-
Vector2D
public Vector2D(Vector2D theVector)
Construct a new vector that is a copy of the given vector.- Parameters:
theVector- Vector to copy.
-
-
Method Detail
-
clear
public Vector2D clear()
Clear this vector. The X and Y components are set to 0.- Returns:
- This vector, set to (0,0).
-
assign
public Vector2D assign(double x, double y)
Assign the given X and Y components to this vector.- Parameters:
x- X component.y- Y component.- Returns:
- This vector, set to (x,y).
-
assign
public Vector2D assign(Vector2D theVector)
Assign (copy) the given vector to this vector.- Parameters:
theVector- Vector to copy.- Returns:
- This vector, set to theVector.
-
add
public Vector2D add(Vector2D theVector)
Add the given vector to this vector.- Parameters:
theVector- Vector to add.- Returns:
- This vector, set to this + theVector.
-
sub
public Vector2D sub(Vector2D theVector)
Subtract the given vector from this vector.- Parameters:
theVector- Vector to subtract.- Returns:
- This vector, set to this - theVector.
-
mul
public Vector2D mul(double a)
Multiply this vector by the given scalar.- Parameters:
a- Scalar.- Returns:
- This vector, set to this * a.
-
div
public Vector2D div(double a)
Divide this vector by the given scalar.- Parameters:
a- Scalar.- Returns:
- This vector, set to this / a.
-
neg
public Vector2D neg()
Negate this vector.- Returns:
- This vector, set to -this.
-
rotate90
public Vector2D rotate90()
Rotate this vector 90 degrees counterclockwise.- Returns:
- This vector, rotated.
-
rotate180
public Vector2D rotate180()
Rotate this vector 180 degrees.- Returns:
- This vector, rotated.
-
rotate270
public Vector2D rotate270()
Rotate this vector 270 degrees counterclockwise (90 degrees clockwise).- Returns:
- This vector, rotated.
-
norm
public Vector2D norm()
Normalize this vector. This vector is set to a unit vector pointing in the same direction.- Returns:
- This vector, set to this / this.mag().
-
dot
public double dot(Vector2D theVector)
Determine the dot product of this vector and the given vector.- Parameters:
theVector- Vector.- Returns:
- Dot product.
-
mag
public double mag()
Determine the magnitude of this vector. The magnitude of the vector (x,y) is (x2 + y2)1/2.- Returns:
- Magnitude.
-
sqrMag
public double sqrMag()
Determine the squared magnitude of this vector. The squared magnitude of the vector (x,y) is (x2 + y2).- Returns:
- Squared magnitude.
-
arg
public double arg()
Determine the argument of this vector. The argument of the vector (x,y) is tan-1 (y/x).- Returns:
- Argument.
-
dist
public double dist(Vector2D theVector)
Determine the distance (magnitude of the difference) between this vector and the given vector.- Parameters:
theVector- Vector.- Returns:
- Distance between this vector and theVector.
-
sqrDist
public double sqrDist(Vector2D theVector)
Determine the squared distance (squared magnitude of the difference) between this vector and the given vector.- Parameters:
theVector- Vector.- Returns:
- Squared distance between this vector and theVector.
-
writeExternal
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOExceptionWrite this vector to the given object output stream.- Specified by:
writeExternalin interfacejava.io.Externalizable- Parameters:
out- Object output stream.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
readExternal
public void readExternal(java.io.ObjectInput in) throws java.io.IOExceptionRead this vector from the given object input stream.- Specified by:
readExternalin interfacejava.io.Externalizable- Parameters:
in- Object input stream.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
toString
public java.lang.String toString()
Returns a string version of this vector. The string is of the form "(x,y)".- Overrides:
toStringin classjava.lang.Object- Returns:
- String version.
-
doubleBuffer
public static DoubleBuf doubleBuffer(Vector2D[] theArray)
Create a buffer for the entire given vector array. The returned buffer encompasses all the elements in theArray.The returned buffer treats a vector as a pair of doubles (rather than an object). This buffer does not support reduction. The buffer's getReductionBuf() method throws an UnsupportedOperationException.
- Parameters:
theArray- Array.- Returns:
- Buffer.
- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theArray is null.
-
doubleSliceBuffer
public static DoubleBuf doubleSliceBuffer(Vector2D[] theArray, Range theRange)
Create a buffer for one slice of the given vector array. The returned buffer encompasses theRange of elements in theArray. The range's stride may be 1 or greater than 1.The returned buffer treats a vector as a pair of doubles (rather than an object). This buffer does not support reduction. The buffer's getReductionBuf() method throws an UnsupportedOperationException.
- Parameters:
theArray- Array.theRange- Range of elements to include.- Returns:
- Buffer.
- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theArray is null or theRange is null.java.lang.IndexOutOfBoundsException- (unchecked exception) Thrown if theArray does not include all the indexes in theRange.
-
doubleSliceBuffers
public static DoubleBuf[] doubleSliceBuffers(Vector2D[] theArray, Range[] theRanges)
Create an array of buffers for multiple slices of the given vector array. The returned buffer array has the same length as theRanges. Each element [i] of the returned buffer array encompasses the elements of theArray specified by theRanges[i]. Each range's stride may be 1 or greater than 1.Each buffer in the returned buffer array treats a vector as a pair of doubles (rather than an object). This buffer does not support reduction. The buffer's getReductionBuf() method throws an UnsupportedOperationException.
- Parameters:
theArray- Array.theRanges- Array of ranges of elements to include.- Returns:
- Array of buffers.
- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theArray is null or theRanges or any element thereof is null.java.lang.IndexOutOfBoundsException- (unchecked exception) Thrown if theArray's allocation does not include any element of theRanges.
-
-
DMelt 3.0 © DataMelt by jWork.ORG