com.vividsolutions.jts.algorithm
Class VectorMath

java.lang.Object
  extended by com.vividsolutions.jts.algorithm.VectorMath

public class VectorMath
extends java.lang.Object

Functions for performing mathematics on vectors in the 2D plane. Vectors are represented as single Coordinates; the implied vector is assumed to start at the origin.

Vector arithmetic is useful in computing various geometric constructions.

Version:
1.0
Author:
Martin Davis

Constructor Summary
VectorMath()
           
 
Method Summary
static Coordinate average(Coordinate v0, Coordinate v1)
          Computes the average of two vectors.
static Coordinate difference(Coordinate v0, Coordinate v1)
          Computes the difference of two vectors [v0 - v1].
static double dotProduct(Coordinate v1, Coordinate v2)
          Computes the dot-product of two vectors
static Coordinate multiply(double s, Coordinate v)
          Computes the product of a scalar and a vector.
static Coordinate normalize(Coordinate v)
          Normalizes a vector to have magnitude 1
static void normalizeSelf(Coordinate v)
          Normalizes a vector to have magnitude 1 in-place
static Coordinate pointAlong(Coordinate v0, Coordinate v1, double lengthFraction)
          Computes a point a given fraction of length along the line joining the endpoints of two vectors.
static Coordinate rotateByQuarterCircle(Coordinate v, int numQuarters)
          Rotates a vector by a given number of quarter-circles (i.e.
static Coordinate sum(Coordinate v0, Coordinate v1)
          Computes the sum of two vectors.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VectorMath

public VectorMath()
Method Detail

normalize

public static Coordinate normalize(Coordinate v)
Normalizes a vector to have magnitude 1

Parameters:
v - the vector to normalize
Returns:
the normalized vector

normalizeSelf

public static void normalizeSelf(Coordinate v)
Normalizes a vector to have magnitude 1 in-place

Parameters:
v - the vector to normalize

dotProduct

public static double dotProduct(Coordinate v1,
                                Coordinate v2)
Computes the dot-product of two vectors

Parameters:
v1 - a vector
v2 - a vector
Returns:
the dot product of the vectors

average

public static Coordinate average(Coordinate v0,
                                 Coordinate v1)
Computes the average of two vectors. This is equivalent to computing the midpoint of the line segment joining the vector endpoints.

Parameters:
v0 - a vector
v1 - a vector
Returns:
the average of the vectors

multiply

public static Coordinate multiply(double s,
                                  Coordinate v)
Computes the product of a scalar and a vector.

Parameters:
s - a scalar value
v - a vector
Returns:
the product of the scalar and the vector

pointAlong

public static Coordinate pointAlong(Coordinate v0,
                                    Coordinate v1,
                                    double lengthFraction)
Computes a point a given fraction of length along the line joining the endpoints of two vectors.

This is equivalent to computing the weighted sum of the two vectors:

   ws = frac * v0 + (1 - frac) * v1
 

Parameters:
v0 - a vector
v1 - a vector
lengthFraction - the fraction of the length
Returns:
the vector to the computed point

sum

public static Coordinate sum(Coordinate v0,
                             Coordinate v1)
Computes the sum of two vectors.

Parameters:
v0 - a vector
v1 - a vector
Returns:
the sum of the vectors

difference

public static Coordinate difference(Coordinate v0,
                                    Coordinate v1)
Computes the difference of two vectors [v0 - v1].

Parameters:
v0 - a vector
v1 - a vector
Returns:
the sum of the vectors

rotateByQuarterCircle

public static Coordinate rotateByQuarterCircle(Coordinate v,
                                               int numQuarters)
Rotates a vector by a given number of quarter-circles (i.e. multiples of 90 degrees or Pi/2 radians). A positive number rotates counter-clockwise, a negative number rotates clockwise. Under this operation the magnitude of the vector and the absolute values of the ordinates do not change, only their sign and ordinate index.

Parameters:
v - the vector to rotate.
numQuarters - the number of quarter-circles to rotate by
Returns:
the rotated vector.