edu.rit.vector
Class Vector2D

java.lang.Object
  extended by edu.rit.vector.Vector2D
All Implemented Interfaces:
Externalizable, Serializable

public class Vector2D
extends Object
implements Externalizable

Class 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.

See Also:
Serialized Form

Field Summary
 double x
          X component.
 double y
          Y component.
 
Constructor Summary
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
 Vector2D add(Vector2D theVector)
          Add the given vector to this vector.
 double arg()
          Determine the argument of this vector.
 Vector2D assign(double x, double y)
          Assign the given X and Y components to this vector.
 Vector2D assign(Vector2D theVector)
          Assign (copy) the given vector to this vector.
 Vector2D clear()
          Clear this vector.
 double dist(Vector2D theVector)
          Determine the distance (magnitude of the difference) between this vector and the given vector.
 Vector2D div(double a)
          Divide this vector by the given scalar.
 double dot(Vector2D theVector)
          Determine the dot product of this vector and the given vector.
static DoubleBuf doubleBuffer(Vector2D[] theArray)
          Create a buffer for the entire given vector array.
static DoubleBuf doubleSliceBuffer(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.
 double mag()
          Determine the magnitude of this vector.
 Vector2D mul(double a)
          Multiply this vector by the given scalar.
 Vector2D neg()
          Negate this vector.
 Vector2D norm()
          Normalize this vector.
 void readExternal(ObjectInput in)
          Read this vector from the given object input stream.
 Vector2D rotate180()
          Rotate this vector 180 degrees.
 Vector2D rotate270()
          Rotate this vector 270 degrees counterclockwise (90 degrees clockwise).
 Vector2D rotate90()
          Rotate this vector 90 degrees counterclockwise.
 double sqrDist(Vector2D theVector)
          Determine the squared distance (squared magnitude of the difference) between this vector and the given vector.
 double sqrMag()
          Determine the squared magnitude of this vector.
 Vector2D sub(Vector2D theVector)
          Subtract the given vector from this vector.
 String toString()
          Returns a string version of this vector.
 void writeExternal(ObjectOutput out)
          Write this vector to the given object output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public double x
X component.


y

public double y
Y component.

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(ObjectOutput out)
                   throws IOException
Write this vector to the given object output stream.

Specified by:
writeExternal in interface Externalizable
Parameters:
out - Object output stream.
Throws:
IOException - Thrown if an I/O error occurred.

readExternal

public void readExternal(ObjectInput in)
                  throws IOException
Read this vector from the given object input stream.

Specified by:
readExternal in interface Externalizable
Parameters:
in - Object input stream.
Throws:
IOException - Thrown if an I/O error occurred.

toString

public String toString()
Returns a string version of this vector. The string is of the form "(x,y)".

Overrides:
toString in class 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:
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:
NullPointerException - (unchecked exception) Thrown if theArray is null or theRange is null.
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:
NullPointerException - (unchecked exception) Thrown if theArray is null or theRanges or any element thereof is null.
IndexOutOfBoundsException - (unchecked exception) Thrown if theArray's allocation does not include any element of theRanges.


Copyright © 2005-2012 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.