maspack.matrix
Class Vector2d

java.lang.Object
  extended bymaspack.matrix.VectorObject
      extended bymaspack.matrix.Vector2d
Direct Known Subclasses:
Point2d

public class Vector2d
extends VectorObject

Implements a 2 element vector, along with its most commonly used operations.

The size of these vectors is fixed.


Field Summary
 double x
          First element
 double y
          Second element
 
Constructor Summary
Vector2d()
          Creates a 2-vector and initializes its elements to 0.
Vector2d(double x, double y)
          Creates a 2-vector with the supplied element values.
Vector2d(Vector2d v)
          Creates a 2-vector by copying an existing one.
 
Method Summary
 void absolute()
          Sets the elements of this vector to their absolute values.
 void absolute(Vector2d v1)
          Sets the elements of this vector to the absolute value of v1.
 void add(Vector2d v1)
          Adds this vector to v1 and places the result in this vector.
 void add(Vector2d v1, Vector2d v2)
          Adds vector v1 to v2 and places the result in this vector.
 double angle(Vector2d v1)
          Returns the angle between this vector and v1.
 void combine(double s1, Vector2d v1, double s2, Vector2d v2)
          Computes s1 v1 + s2 v2 and places the result in this vector.
 double cross(Vector2d v2)
          Computes the cross product of this vector and v1.
 double cross(Vector2d v1, Vector2d v2)
          Computes the cross product of v1 and v2.
 double distance(Vector2d v)
          Returns the Euclidean distance between this vector and vector v.
 double distanceSquared(Vector2d v)
          Returns the squared of the Euclidean distance between this vector and vector v.
 double dot(Vector2d v1)
          Returns the dot product of this vector and v1.
 boolean epsilonEquals(Vector2d v1, double eps)
          Returns true if the elements of this vector equal those of vector v1within a prescribed tolerance epsilon.
 boolean equals(Vector2d v1)
          Returns true if the elements of this vector exactly equal those of vector v1.
 void get(double[] values)
          Copies the elements of this vector into an array of doubles.
 double get(int i)
          Gets a single element of this vector.
 double infinityNorm()
          Returns the infinity norm of this vector.
 void interpolate(double s, Vector2d v1)
          Computes the interpolation (1-s) this + s v1 and places the result in this vector.
 void interpolate(Vector2d v1, double s, Vector2d v2)
          Computes the interpolation (1-s) v1 + s v2 and places the result in this vector.
 void inverseTransform(AffineTransform2dObject X)
          Applies an inverse affine transformation to this vector, in place.
 void inverseTransform(AffineTransform2dObject X, Vector2d v1)
          Applies an inverse affine transformation to the vector v1, and places the result in this vector.
 void inverseTransform(RotationMatrix2d R)
          Applies an inverse rotational transformation to this vector, in place.
 void inverseTransform(RotationMatrix2d R, Vector2d v1)
          Applies an inverse rotational transformation to the vector v1, and stores the result in this vector.
 double length()
          Returns the 2 norm of this vector.
 double lengthSquared()
          Returns the square of the 2 norm of this vector.
 int maxAbsIndex()
          Returns the index (0 or 1) of the element of v with the largest absolute value.
 double maxElement()
          Returns the maximum element value of this vector.
 int minAbsIndex()
          Returns the index (0 or 1) of the element of v with the smallest absolute value.
 double minElement()
          Returns the minimum element value of this vector.
 void negate()
          Negates this vector in place.
 void negate(Vector2d v1)
          Sets this vector to the negative of v1.
 void normalize()
          Normalizes this vector in place.
 void normalize(Vector2d v1)
          Computes a unit vector in the direction of v1 and places the result in this vector.
 double oneNorm()
          Returns the 1 norm of this vector.
 void perpendicular(Vector2d v1)
          Sets this vector to one which is perpendicular to v1.
 void scale(double s)
          Scales the elements of this vector by s.
 void scale(double s, Vector2d v1)
          Scales the elements of vector v1 by s and places the results in this vector.
 void scaleAdd(double s, Vector2d v1)
          Computes s this + v1 and places the result in this vector.
 void scaleAdd(double s, Vector2d v1, Vector2d v2)
          Computes s v1 + v2 and places the result in this vector.
 void set(double[] values)
          Sets the elements of this vector from an array of doubles.
 void set(double x, double y)
          Sets the elements of this vector to the prescribed values.
 void set(int i, double value)
          Sets a single element of this vector.
 void set(Vector2d v1)
          Sets the values of this vector to those of v1.
 void setRandom()
          Sets the elements of this vector to uniformly distributed random values in the range -0.5 (inclusive) to 0.5 (exclusive).
 void setRandom(double lower, double upper)
          Sets the elements of this vector to uniformly distributed random values in a specified range.
 void setRandom(double lower, double upper, java.util.Random generator)
          Sets the elements of this vector to uniformly distributed random values in a specified range, using a supplied random number generator.
 void setZero()
          Sets the elements of this vector to zero.
 int size()
          Returns the size of this vector (which is always 2)
 void sort()
          Sorts the contents of this vector by element value, with x being set to the largest value and z being set to the smallest value.
 void sort(Vector2d v1)
          Sorts the contents of vector v1 by element value, with x being set to the largest value and y being set to the smallest value, and places the results in this vector.
 void sortAbsolute()
          Sorts the contents of this vector by absolute element value, with x being set to the largest value and y being set to the smallest value.
 void sub(Vector2d v1)
          Subtracts v1 from this vector and places the result in this vector.
 void sub(Vector2d v1, Vector2d v2)
          Subtracts vector v1 from v2 and places the result in this vector.
 void transform(AffineTransform2dObject X)
          Applies a affine transformation to this vector, in place.
 void transform(AffineTransform2dObject X, Vector2d v1)
          Applies a affine transformation to the vector v1, and places the result in this vector.
 void transform(RotationMatrix2d R)
          Applies a rotational transformation to this vector, in place.
 void transform(RotationMatrix2d R, Vector2d v1)
          Applies a rotational transformation to the vector v1 and stores the result in this vector.
 
Methods inherited from class maspack.matrix.VectorObject
dot, epsilonEquals, equals, equals, get, isFixedSize, norm, normSquared, scan, set, setSize, toString, toString, toString
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public double x
First element


y

public double y
Second element

Constructor Detail

Vector2d

public Vector2d()
Creates a 2-vector and initializes its elements to 0.


Vector2d

public Vector2d(Vector2d v)
Creates a 2-vector by copying an existing one.

Parameters:
v - vector to be copied

Vector2d

public Vector2d(double x,
                double y)
Creates a 2-vector with the supplied element values.

Parameters:
x - first element
y - second element
Method Detail

size

public int size()
Returns the size of this vector (which is always 2)

Specified by:
size in class VectorObject
Returns:
2

get

public double get(int i)
Gets a single element of this vector. Elements 0 and 1 correspond to x and y.

Specified by:
get in class VectorObject
Parameters:
i - element index
Returns:
element value throws ArrayIndexOutOfBoundsException if i is not in the range 0 to 1.

get

public void get(double[] values)
Copies the elements of this vector into an array of doubles.

Overrides:
get in class VectorObject
Parameters:
values - array into which values are copied

set

public void set(int i,
                double value)
Sets a single element of this vector. Elements 0 and 1 correspond to x and y.

Specified by:
set in class VectorObject
Parameters:
i - element index
value - element value
Returns:
element value throws ArrayIndexOutOfBoundsException if i is not in the range 0 to 1.

set

public void set(double[] values)
Sets the elements of this vector from an array of doubles.

Overrides:
set in class VectorObject
Parameters:
values - array from which values are copied

set

public void set(Vector2d v1)
Sets the values of this vector to those of v1.

Parameters:
v1 - vector whose values are copied

add

public void add(Vector2d v1,
                Vector2d v2)
Adds vector v1 to v2 and places the result in this vector.

Parameters:
v1 - left-hand vector
v2 - right-hand vector

add

public void add(Vector2d v1)
Adds this vector to v1 and places the result in this vector.

Parameters:
v1 - right-hand vector

sub

public void sub(Vector2d v1,
                Vector2d v2)
Subtracts vector v1 from v2 and places the result in this vector.

Parameters:
v1 - left-hand vector
v2 - right-hand vector

sub

public void sub(Vector2d v1)
Subtracts v1 from this vector and places the result in this vector.

Parameters:
v1 - right-hand vector

negate

public void negate(Vector2d v1)
Sets this vector to the negative of v1.

Parameters:
v1 - vector to negate

negate

public void negate()
Negates this vector in place.


scale

public void scale(double s)
Scales the elements of this vector by s.

Parameters:
s - scaling factor

scale

public void scale(double s,
                  Vector2d v1)
Scales the elements of vector v1 by s and places the results in this vector.

Parameters:
s - scaling factor
v1 - vector to be scaled

interpolate

public void interpolate(Vector2d v1,
                        double s,
                        Vector2d v2)
Computes the interpolation (1-s) v1 + s v2 and places the result in this vector.

Parameters:
v1 - left-hand vector
s - interpolation factor
v2 - right-hand vector

interpolate

public void interpolate(double s,
                        Vector2d v1)
Computes the interpolation (1-s) this + s v1 and places the result in this vector.

Parameters:
s - interpolation factor
v1 - right-hand vector

scaleAdd

public void scaleAdd(double s,
                     Vector2d v1)
Computes s this + v1 and places the result in this vector.

Parameters:
s - scaling factor
v1 - vector to be added

scaleAdd

public void scaleAdd(double s,
                     Vector2d v1,
                     Vector2d v2)
Computes s v1 + v2 and places the result in this vector.

Parameters:
s - scaling factor
v1 - vector to be scaled
v2 - vector to be added

combine

public void combine(double s1,
                    Vector2d v1,
                    double s2,
                    Vector2d v2)
Computes s1 v1 + s2 v2 and places the result in this vector.

Parameters:
s1 - left-hand scaling factor
v1 - left-hand vector
s2 - right-hand scaling factor
v2 - right-hand vector

length

public double length()
Returns the 2 norm of this vector. This is the square root of the sum of the squares of the elements.

Returns:
vector 2 norm

lengthSquared

public double lengthSquared()
Returns the square of the 2 norm of this vector. This is the sum of the squares of the elements.

Returns:
square of the 2 norm

distance

public double distance(Vector2d v)
Returns the Euclidean distance between this vector and vector v.

Returns:
distance between this vector and v

distanceSquared

public double distanceSquared(Vector2d v)
Returns the squared of the Euclidean distance between this vector and vector v.

Returns:
squared distance between this vector and v

maxElement

public double maxElement()
Returns the maximum element value of this vector.

Overrides:
maxElement in class VectorObject
Returns:
maximal element

minElement

public double minElement()
Returns the minimum element value of this vector.

Overrides:
minElement in class VectorObject
Returns:
minimal element

infinityNorm

public double infinityNorm()
Returns the infinity norm of this vector. This is the maximum absolute value over all elements.

Overrides:
infinityNorm in class VectorObject
Returns:
vector infinity norm

oneNorm

public double oneNorm()
Returns the 1 norm of this vector. This is the sum of the absolute values of the elements.

Overrides:
oneNorm in class VectorObject
Returns:
vector 1 norm

dot

public double dot(Vector2d v1)
Returns the dot product of this vector and v1.

Parameters:
v1 - right-hand vector
Returns:
dot product

angle

public double angle(Vector2d v1)
Returns the angle between this vector and v1. The angle is defined as acos(c), where c is the dot product of unit vectors parallel to this vector and v1.

Parameters:
v1 - right-hand vector
Returns:
angle between vectors, in radians

normalize

public void normalize()
Normalizes this vector in place.


normalize

public void normalize(Vector2d v1)
Computes a unit vector in the direction of v1 and places the result in this vector.

Parameters:
v1 - vector to normalize

perpendicular

public void perpendicular(Vector2d v1)
Sets this vector to one which is perpendicular to v1. The resulting vector will not be normalized.

Parameters:
v1 - perpendicular reference vector

epsilonEquals

public boolean epsilonEquals(Vector2d v1,
                             double eps)
Returns true if the elements of this vector equal those of vector v1within a prescribed tolerance epsilon.

Parameters:
v1 - vector to compare with
eps - comparison tolerance
Returns:
false if the vectors are not equal within the specified tolerance

equals

public boolean equals(Vector2d v1)
Returns true if the elements of this vector exactly equal those of vector v1.

Parameters:
v1 - vector to compare with
Returns:
false if the vectors are not equal

setZero

public void setZero()
Sets the elements of this vector to zero.


set

public void set(double x,
                double y)
Sets the elements of this vector to the prescribed values.

Parameters:
x - value for first element
y - value for second element

absolute

public void absolute()
Sets the elements of this vector to their absolute values.


absolute

public void absolute(Vector2d v1)
Sets the elements of this vector to the absolute value of v1.

Parameters:
v1 - vector to take the absolute value of

sortAbsolute

public void sortAbsolute()
Sorts the contents of this vector by absolute element value, with x being set to the largest value and y being set to the smallest value.


maxAbsIndex

public int maxAbsIndex()
Returns the index (0 or 1) of the element of v with the largest absolute value.

Returns:
index of largest absolute value

minAbsIndex

public int minAbsIndex()
Returns the index (0 or 1) of the element of v with the smallest absolute value.

Returns:
index of smallest absolute value

sort

public void sort(Vector2d v1)
Sorts the contents of vector v1 by element value, with x being set to the largest value and y being set to the smallest value, and places the results in this vector.

Parameters:
v1 - vector to sort

sort

public void sort()
Sorts the contents of this vector by element value, with x being set to the largest value and z being set to the smallest value.


cross

public double cross(Vector2d v1,
                    Vector2d v2)
Computes the cross product of v1 and v2.

Parameters:
v1 - first vector
v2 - second vector
Returns:
cross product

cross

public double cross(Vector2d v2)
Computes the cross product of this vector and v1.

Parameters:
v2 - second vector
Returns:
cross product

transform

public void transform(RotationMatrix2d R)
Applies a rotational transformation to this vector, in place. This is equivalent to multiplying the rotation matrix by this vector.

Parameters:
R - rotational transformation matrix

transform

public void transform(RotationMatrix2d R,
                      Vector2d v1)
Applies a rotational transformation to the vector v1 and stores the result in this vector. This is equivalent to multiplying the rotation matrix by v1.

Parameters:
R - rotational transformation matrix
v1 - vector to transform

inverseTransform

public void inverseTransform(RotationMatrix2d R)
Applies an inverse rotational transformation to this vector, in place. This is equivalent to multiplying the transpose (or inverse) of the rotation matrix by this vector.

Parameters:
R - rotational transformation matrix

inverseTransform

public void inverseTransform(RotationMatrix2d R,
                             Vector2d v1)
Applies an inverse rotational transformation to the vector v1, and stores the result in this vector. This is equivalent to multiplying the transpose (or inverse) of the rotation matrix by this v1.

Parameters:
R - rotational transformation matrix
v1 - vector to transform

transform

public void transform(AffineTransform2dObject X)
Applies a affine transformation to this vector, in place.

Parameters:
X - affine transformation

transform

public void transform(AffineTransform2dObject X,
                      Vector2d v1)
Applies a affine transformation to the vector v1, and places the result in this vector.

Parameters:
X - affine transformation
v1 - vector to be transformed

inverseTransform

public void inverseTransform(AffineTransform2dObject X)
Applies an inverse affine transformation to this vector, in place.

Parameters:
X - affine transformation

inverseTransform

public void inverseTransform(AffineTransform2dObject X,
                             Vector2d v1)
Applies an inverse affine transformation to the vector v1, and places the result in this vector.

Parameters:
X - affine transformation
v1 - vector to be transformed

setRandom

public void setRandom()
Sets the elements of this vector to uniformly distributed random values in the range -0.5 (inclusive) to 0.5 (exclusive).


setRandom

public void setRandom(double lower,
                      double upper)
Sets the elements of this vector to uniformly distributed random values in a specified range.

Parameters:
lower - lower random value (inclusive)
upper - upper random value (exclusive)

setRandom

public void setRandom(double lower,
                      double upper,
                      java.util.Random generator)
Sets the elements of this vector to uniformly distributed random values in a specified range, using a supplied random number generator.

Parameters:
lower - lower random value (inclusive)
upper - upper random value (exclusive)
generator - random number generator