org.apache.commons.math3.geometry.euclidean.twod

## Class Vector2D

- java.lang.Object
- - org.apache.commons.math3.geometry.euclidean.twod.Vector2D

- All Implemented Interfaces:
    java.io.Serializable, Vector<Euclidean2D>

      public class Vector2D
      extends java.lang.Object
      implements Vector<Euclidean2D>

  This class represents a 2D vector.

  Instances of this class are guaranteed to be immutable.

  Since:
    3.0
  Version:
    $Id: Vector2D.java 1416643 2012-12-03 19:37:14Z tn $
  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static Vector2D` | `NaN` A vector with all coordinates set to NaN.
    `static Vector2D` | `NEGATIVE_INFINITY` A vector with all coordinates set to negative infinity.
    `static Vector2D` | `POSITIVE_INFINITY` A vector with all coordinates set to positive infinity.
    `private static long` | `serialVersionUID` Serializable UID.
    `private double` | `x` Abscissa.
    `private double` | `y` Ordinate.
    `static Vector2D` | `ZERO` Origin (coordinates: 0, 0).

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Vector2D(double[] v)` Simple constructor.
    `Vector2D(double x, double y)` Simple constructor.
    `Vector2D(double a, Vector2D u)` Multiplicative constructor Build a vector from another one and a scale factor.
    `Vector2D(double a1, Vector2D u1, double a2, Vector2D u2)` Linear constructor Build a vector from two other ones and corresponding scale factors.
    `Vector2D(double a1, Vector2D u1, double a2, Vector2D u2, double a3, Vector2D u3)` Linear constructor Build a vector from three other ones and corresponding scale factors.
    `Vector2D(double a1, Vector2D u1, double a2, Vector2D u2, double a3, Vector2D u3, double a4, Vector2D u4)` Linear constructor Build a vector from four other ones and corresponding scale factors.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `Vector2D` | `add(double factor, Vector<Euclidean2D> v)` Add a scaled vector to the instance.
    `Vector2D` | `add(Vector<Euclidean2D> v)` Add a vector to the instance.
    `double` | `distance(Vector<Euclidean2D> p)` Compute the distance between the instance and another vector according to the L2 norm.
    `static double` | `distance(Vector2D p1, Vector2D p2)` Compute the distance between two vectors according to the L2 norm.
    `double` | `distance1(Vector<Euclidean2D> p)` Compute the distance between the instance and another vector according to the L1 norm.
    `double` | `distanceInf(Vector<Euclidean2D> p)` Compute the distance between the instance and another vector according to the L∞ norm.
    `static double` | `distanceInf(Vector2D p1, Vector2D p2)` Compute the distance between two vectors according to the L∞ norm.
    `double` | `distanceSq(Vector<Euclidean2D> p)` Compute the square of the distance between the instance and another vector.
    `static double` | `distanceSq(Vector2D p1, Vector2D p2)` Compute the square of the distance between two vectors.
    `double` | `dotProduct(Vector<Euclidean2D> v)` Compute the dot-product of the instance and another vector.
    `boolean` | `equals(java.lang.Object other)` Test for the equality of two 2D vectors.
    `double` | `getNorm()` Get the L2 norm for the vector.
    `double` | `getNorm1()` Get the L1 norm for the vector.
    `double` | `getNormInf()` Get the L∞ norm for the vector.
    `double` | `getNormSq()` Get the square of the norm for the vector.
    `Space` | `getSpace()` Get the space to which the vector belongs.
    `double` | `getX()` Get the abscissa of the vector.
    `double` | `getY()` Get the ordinate of the vector.
    `Vector2D` | `getZero()` Get the null vector of the vectorial space or origin point of the affine space.
    `int` | `hashCode()` Get a hashCode for the 2D vector.
    `boolean` | `isInfinite()` Returns true if any coordinate of this vector is infinite and none are NaN; false otherwise
    `boolean` | `isNaN()` Returns true if any coordinate of this vector is NaN; false otherwise
    `Vector2D` | `negate()` Get the opposite of the instance.
    `Vector2D` | `normalize()` Get a normalized vector aligned with the instance.
    `Vector2D` | `scalarMultiply(double a)` Multiply the instance by a scalar.
    `Vector2D` | `subtract(double factor, Vector<Euclidean2D> v)` Subtract a scaled vector from the instance.
    `Vector2D` | `subtract(Vector<Euclidean2D> p)` Subtract a vector from the instance.
    `double[]` | `toArray()` Get the vector coordinates as a dimension 2 array.
    `java.lang.String` | `toString()` Get a string representation of this vector.
    `java.lang.String` | `toString(java.text.NumberFormat format)` Get a string representation of this vector.

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### ZERO

          public static final Vector2D ZERO

      Origin (coordinates: 0, 0).

    - #### NaN

          public static final Vector2D NaN

      A vector with all coordinates set to NaN.

    - #### POSITIVE_INFINITY

          public static final Vector2D POSITIVE_INFINITY

      A vector with all coordinates set to positive infinity.

    - #### NEGATIVE_INFINITY

          public static final Vector2D NEGATIVE_INFINITY

      A vector with all coordinates set to negative infinity.

    - #### serialVersionUID

          private static final long serialVersionUID

      Serializable UID.
      See Also:
        Constant Field Values

    - #### x

          private final double x

      Abscissa.

    - #### y

          private final double y

      Ordinate.

  - ### Constructor Detail

    - #### Vector2D

          public Vector2D(double x,
                  double y)

      Simple constructor. Build a vector from its coordinates
      Parameters:
        `x` - abscissa
        `y` - ordinate
      See Also:
        `getX()`, `getY()`

    - #### Vector2D

          public Vector2D(double[] v)
                   throws DimensionMismatchException

      Simple constructor. Build a vector from its coordinates
      Parameters:
        `v` - coordinates array
      Throws:
        `DimensionMismatchException` - if array does not have 2 elements
      See Also:
        `toArray()`

    - #### Vector2D

          public Vector2D(double a,
                  Vector2D u)

      Multiplicative constructor Build a vector from another one and a scale factor. The vector built will be a * u
      Parameters:
        `a` - scale factor
        `u` - base (unscaled) vector

    - #### Vector2D

          public Vector2D(double a1,
                  Vector2D u1,
                  double a2,
                  Vector2D u2)

      Linear constructor Build a vector from two other ones and corresponding scale factors. The vector built will be a1 * u1 + a2 * u2
      Parameters:
        `a1` - first scale factor
        `u1` - first base (unscaled) vector
        `a2` - second scale factor
        `u2` - second base (unscaled) vector

    - #### Vector2D

          public Vector2D(double a1,
                  Vector2D u1,
                  double a2,
                  Vector2D u2,
                  double a3,
                  Vector2D u3)

      Linear constructor Build a vector from three other ones and corresponding scale factors. The vector built will be a1 * u1 + a2 * u2 + a3 * u3
      Parameters:
        `a1` - first scale factor
        `u1` - first base (unscaled) vector
        `a2` - second scale factor
        `u2` - second base (unscaled) vector
        `a3` - third scale factor
        `u3` - third base (unscaled) vector

    - #### Vector2D

          public Vector2D(double a1,
                  Vector2D u1,
                  double a2,
                  Vector2D u2,
                  double a3,
                  Vector2D u3,
                  double a4,
                  Vector2D u4)

      Linear constructor Build a vector from four other ones and corresponding scale factors. The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4
      Parameters:
        `a1` - first scale factor
        `u1` - first base (unscaled) vector
        `a2` - second scale factor
        `u2` - second base (unscaled) vector
        `a3` - third scale factor
        `u3` - third base (unscaled) vector
        `a4` - fourth scale factor
        `u4` - fourth base (unscaled) vector

  - ### Method Detail

    - #### getX

          public double getX()

      Get the abscissa of the vector.
      Returns:
        abscissa of the vector
      See Also:
        `Vector2D(double, double)`

    - #### getY

          public double getY()

      Get the ordinate of the vector.
      Returns:
        ordinate of the vector
      See Also:
        `Vector2D(double, double)`

    - #### toArray

          public double[] toArray()

      Get the vector coordinates as a dimension 2 array.
      Returns:
        vector coordinates
      See Also:
        `Vector2D(double[])`

    - #### getSpace

          public Space getSpace()

      Get the space to which the vector belongs.
      Specified by:
        `getSpace` in interface `Vector<Euclidean2D>`
      Returns:
        containing space

    - #### getZero

          public Vector2D getZero()

      Get the null vector of the vectorial space or origin point of the affine space.
      Specified by:
        `getZero` in interface `Vector<Euclidean2D>`
      Returns:
        null vector of the vectorial space or origin point of the affine space

    - #### getNorm1

          public double getNorm1()

      Get the L1 norm for the vector.
      Specified by:
        `getNorm1` in interface `Vector<Euclidean2D>`
      Returns:
        L1 norm for the vector

    - #### getNorm

          public double getNorm()

      Get the L2 norm for the vector.
      Specified by:
        `getNorm` in interface `Vector<Euclidean2D>`
      Returns:
        Euclidean norm for the vector

    - #### getNormSq

          public double getNormSq()

      Get the square of the norm for the vector.
      Specified by:
        `getNormSq` in interface `Vector<Euclidean2D>`
      Returns:
        square of the Euclidean norm for the vector

    - #### getNormInf

          public double getNormInf()

      Get the L∞ norm for the vector.
      Specified by:
        `getNormInf` in interface `Vector<Euclidean2D>`
      Returns:
        L∞ norm for the vector

    - #### add

          public Vector2D add(Vector<Euclidean2D> v)

      Add a vector to the instance.
      Specified by:
        `add` in interface `Vector<Euclidean2D>`
      Parameters:
        `v` - vector to add
      Returns:
        a new vector

    - #### add

          public Vector2D add(double factor,
                     Vector<Euclidean2D> v)

      Add a scaled vector to the instance.
      Specified by:
        `add` in interface `Vector<Euclidean2D>`
      Parameters:
        `factor` - scale factor to apply to v before adding it
        `v` - vector to add
      Returns:
        a new vector

    - #### subtract

          public Vector2D subtract(Vector<Euclidean2D> p)

      Subtract a vector from the instance.
      Specified by:
        `subtract` in interface `Vector<Euclidean2D>`
      Parameters:
        `p` - vector to subtract
      Returns:
        a new vector

    - #### subtract

          public Vector2D subtract(double factor,
                          Vector<Euclidean2D> v)

      Subtract a scaled vector from the instance.
      Specified by:
        `subtract` in interface `Vector<Euclidean2D>`
      Parameters:
        `factor` - scale factor to apply to v before subtracting it
        `v` - vector to subtract
      Returns:
        a new vector

    - #### normalize

          public Vector2D normalize()
                             throws MathArithmeticException

      Get a normalized vector aligned with the instance.
      Specified by:
        `normalize` in interface `Vector<Euclidean2D>`
      Returns:
        a new normalized vector
      Throws:
        `MathArithmeticException` - if the norm is zero

    - #### negate

          public Vector2D negate()

      Get the opposite of the instance.
      Specified by:
        `negate` in interface `Vector<Euclidean2D>`
      Returns:
        a new vector which is opposite to the instance

    - #### scalarMultiply

          public Vector2D scalarMultiply(double a)

      Multiply the instance by a scalar.
      Specified by:
        `scalarMultiply` in interface `Vector<Euclidean2D>`
      Parameters:
        `a` - scalar
      Returns:
        a new vector

    - #### isNaN

          public boolean isNaN()

      Returns true if any coordinate of this vector is NaN; false otherwise
      Specified by:
        `isNaN` in interface `Vector<Euclidean2D>`
      Returns:
        true if any coordinate of this vector is NaN; false otherwise

    - #### isInfinite

          public boolean isInfinite()

      Returns true if any coordinate of this vector is infinite and none are NaN; false otherwise
      Specified by:
        `isInfinite` in interface `Vector<Euclidean2D>`
      Returns:
        true if any coordinate of this vector is infinite and none are NaN; false otherwise

    - #### distance1

          public double distance1(Vector<Euclidean2D> p)

      Compute the distance between the instance and another vector according to the L1 norm.

      Calling this method is equivalent to calling: `q.subtract(p).getNorm1()` except that no intermediate vector is built

      Specified by:
        `distance1` in interface `Vector<Euclidean2D>`
      Parameters:
        `p` - second vector
      Returns:
        the distance between the instance and p according to the L1 norm

    - #### distance

          public double distance(Vector<Euclidean2D> p)

      Compute the distance between the instance and another vector according to the L2 norm.

      Calling this method is equivalent to calling: `q.subtract(p).getNorm()` except that no intermediate vector is built

      Specified by:
        `distance` in interface `Vector<Euclidean2D>`
      Parameters:
        `p` - second vector
      Returns:
        the distance between the instance and p according to the L2 norm

    - #### distanceInf

          public double distanceInf(Vector<Euclidean2D> p)

      Compute the distance between the instance and another vector according to the L∞ norm.

      Calling this method is equivalent to calling: `q.subtract(p).getNormInf()` except that no intermediate vector is built

      Specified by:
        `distanceInf` in interface `Vector<Euclidean2D>`
      Parameters:
        `p` - second vector
      Returns:
        the distance between the instance and p according to the L∞ norm

    - #### distanceSq

          public double distanceSq(Vector<Euclidean2D> p)

      Compute the square of the distance between the instance and another vector.

      Calling this method is equivalent to calling: `q.subtract(p).getNormSq()` except that no intermediate vector is built

      Specified by:
        `distanceSq` in interface `Vector<Euclidean2D>`
      Parameters:
        `p` - second vector
      Returns:
        the square of the distance between the instance and p

    - #### dotProduct

          public double dotProduct(Vector<Euclidean2D> v)

      Compute the dot-product of the instance and another vector.
      Specified by:
        `dotProduct` in interface `Vector<Euclidean2D>`
      Parameters:
        `v` - second vector
      Returns:
        the dot product this.v

    - #### distance

          public static double distance(Vector2D p1,
                        Vector2D p2)

      Compute the distance between two vectors according to the L2 norm.

      Calling this method is equivalent to calling: `p1.subtract(p2).getNorm()` except that no intermediate vector is built

      Parameters:
        `p1` - first vector
        `p2` - second vector
      Returns:
        the distance between p1 and p2 according to the L2 norm

    - #### distanceInf

          public static double distanceInf(Vector2D p1,
                           Vector2D p2)

      Compute the distance between two vectors according to the L∞ norm.

      Calling this method is equivalent to calling: `p1.subtract(p2).getNormInf()` except that no intermediate vector is built

      Parameters:
        `p1` - first vector
        `p2` - second vector
      Returns:
        the distance between p1 and p2 according to the L∞ norm

    - #### distanceSq

          public static double distanceSq(Vector2D p1,
                          Vector2D p2)

      Compute the square of the distance between two vectors.

      Calling this method is equivalent to calling: `p1.subtract(p2).getNormSq()` except that no intermediate vector is built

      Parameters:
        `p1` - first vector
        `p2` - second vector
      Returns:
        the square of the distance between p1 and p2

    - #### equals

          public boolean equals(java.lang.Object other)

      Test for the equality of two 2D vectors.

      If all coordinates of two 2D vectors are exactly the same, and none are `Double.NaN`, the two 2D vectors are considered to be equal.

      `NaN` coordinates are considered to affect globally the vector and be equals to each other - i.e, if either (or all) coordinates of the 2D vector are equal to `Double.NaN`, the 2D vector is equal to `NaN`.

      Overrides:
        `equals` in class `java.lang.Object`
      Parameters:
        `other` - Object to test for equality to this
      Returns:
        true if two 2D vector objects are equal, false if object is null, not an instance of Vector2D, or not equal to this Vector2D instance

    - #### hashCode

          public int hashCode()

      Get a hashCode for the 2D vector.

      All NaN values have the same hash code.

      Overrides:
        `hashCode` in class `java.lang.Object`
      Returns:
        a hash code value for this object

    - #### toString

          public java.lang.String toString()

      Get a string representation of this vector.
      Overrides:
        `toString` in class `java.lang.Object`
      Returns:
        a string representation of this vector

    - #### toString

          public java.lang.String toString(java.text.NumberFormat format)

      Get a string representation of this vector.
      Specified by:
        `toString` in interface `Vector<Euclidean2D>`
      Parameters:
        `format` - the custom format for components
      Returns:
        a string representation of this vector

Copyright (c) 2003-2013 Apache Software Foundation