Class Vector3D

java.lang.Object
org.jlab.geom.prim.Vector3D
All Implemented Interfaces:
Transformable, Showable

public final class Vector3D extends Object implements Transformable, Showable
A 3D vector represented by three Cartesian coordinates (x, y, z).

Though internally represented in Cartesian coordinates, the position of a point can be set using spherical coordinates via setMagThetaPhi(double, double, double), and the spherical coordinates of any vector can be obtained via mag(), theta(), and phi().

In addition to containing functions for performing normal mathematical procedures involving vectors such as negate/inverse, addition, subtraction, scalar products, dot products, cross products, and normalization, Vector3D can be used to rotate a point or vector around an arbitrary axes that passes through the origin via rotate(Point3D, doubele) and rotate(Vector3D, double). The inverse of the rotation functions is provided via angle(Vector3D, Vector3D).

Author:
gavalian
  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Constructs a new empty Vector3D that is a null vector, ie (0,0,0).
    Vector3D(double[] xyz)
    Constructs a new Vector3D with the given xyz[0], xyz[1], xyz[2] components
    Vector3D(double x, double y, double z)
    Constructs a new Vector3D with the given x, y, and z components.
    Constructs a new Vector3D identical to the given vector.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    add(double a)
     
    add(Vector3D vector)
    Adds the given vector to this vector.
    double
    Angle between this and another vector
    double
    angle(Vector3D vector0, Vector3D vector1)
    Calculates the clockwise angle of rotation from the image of the first given vector to the image of the second given vector projected onto a plane who's normal is this vector and using this vector as an axis.
    Create unit vector from this
    Returns a new instance of this vector.
    double
    compare(Vector3D vector)
    Returns a value greater than or equal to zero proportional to the amount that the two vectors differ.
    void
    copy(Vector3D vector)
    Sets the components of this vector to be equal the components of the given vector.
    double
     
    cross(Vector3D vector)
    Constructs a new Vector3D containing the cross product of this vector and the given vector.
    divide(double a)
     
    double
    dot(Vector3D vector)
    Returns the dot product of this vector and the given vector.
    static Vector3D
    fromSpherical(double r, double phi, double theta)
    Creates a new Vector3D instance from spherical coordinates
    boolean
    Returns true if this vector is a null vector.
    double
    mag()
    Returns the magnitude of this vector.
    double
    Returns the square of the magnitude of this vector.
    static void
    main(String[] args)
     
    multiply(double a)
     
    void
    Negates this vector's components.
    double
    phi()
    Returns the angle between the x axis and the orthogonal projection of this vector onto the x-y plane.
    Projection of this vector onto another vector
    double
    r()
     
    double
    r2()
     
    double
    rho()
    Returns the length of the orthogonal projection of this vector onto the x-y plane.
    double
     
    void
    rotate(Point3D point, double angle)
    Rotates the given point clockwise around the axis produced by this vector by the given angle
    void
    rotate(Vector3D vector, double angle)
    Rotates the given vector clockwise around the axis produced by this vector by the given angle.
    void
    rotateX(double angle)
    Rotates this object clockwise around the x axis.
    void
    rotateY(double angle)
    Rotates this object clockwise around the y axis.
    void
    rotateZ(double angle)
    Rotates this object clockwise around the z axis.
    void
    scale(double factor)
    Scales each of this vectors components by the given scale factor.
    boolean
    setMag(double mag)
    Sets the magnitude of this vector to equal the given magnitude unless vector is a null vector.
    void
    setMagThetaPhi(double mag, double theta, double phi)
    Sets this vector based on the given spherical coordinates.
    void
    setX(double x)
    Sets the x component.
    void
    setXYZ(double x, double y, double z)
    Sets the x, y and z components of this vector.
    void
    setY(double y)
    Sets the y component.
    void
    setZ(double z)
    Sets the z component.
    void
    Invokes System.out.println(this).
    sub(Vector3D vector)
    Subtracts the given vector from this vector
    subtract(double a)
     
    double
    Returns the angle between this vector and the z axis.
    Constructs a new Point3D using this vector's x, y, and z components.
     
     
    toStringBrief(int ndigits)
     
    void
    translateXYZ(double x, double y, double z)
    Does nothing; vectors cannot be translated.
    boolean
    Sets the magnitude of this vector to equal one unless this vector is a null vector.
    double
    x()
    Returns the x component.
    double
    y()
    Returns the y component.
    double
    z()
    Returns the z component.

    Methods inherited from class java.lang.Object Link icon

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details Link icon

    • Vector3D Link icon

      public Vector3D()
      Constructs a new empty Vector3D that is a null vector, ie (0,0,0).
    • Vector3D Link icon

      public Vector3D(double x, double y, double z)
      Constructs a new Vector3D with the given x, y, and z components.
      Parameters:
      x - the x component
      y - the y component
      z - the z component
    • Vector3D Link icon

      public Vector3D(double[] xyz)
      Constructs a new Vector3D with the given xyz[0], xyz[1], xyz[2] components
      Parameters:
      xyz - where x/y/z-components are xyz[0]/[1]/[2]
    • Vector3D Link icon

      public Vector3D(Vector3D vector)
      Constructs a new Vector3D identical to the given vector.
      Parameters:
      vector - the vector to copy
  • Method Details Link icon

    • copy Link icon

      public void copy(Vector3D vector)
      Sets the components of this vector to be equal the components of the given vector.
      Parameters:
      vector - the vector to copy
    • clone Link icon

      public Vector3D clone()
      Returns a new instance of this vector.
      Overrides:
      clone in class Object
    • fromSpherical Link icon

      public static Vector3D fromSpherical(double r, double phi, double theta)
      Creates a new Vector3D instance from spherical coordinates
      Parameters:
      r - radial distance from the origin
      phi - azimuthal angle from the positive x-axis (right-handed)
      theta - polar angle from the positive z-axis
    • setXYZ Link icon

      public void setXYZ(double x, double y, double z)
      Sets the x, y and z components of this vector.
      Parameters:
      x - the x component
      y - the y component
      z - the z component
    • setX Link icon

      public void setX(double x)
      Sets the x component.
      Parameters:
      x - the x component
    • setY Link icon

      public void setY(double y)
      Sets the y component.
      Parameters:
      y - the y component
    • setZ Link icon

      public void setZ(double z)
      Sets the z component.
      Parameters:
      z - the z component
    • x Link icon

      public double x()
      Returns the x component.
      Returns:
      the x component
    • y Link icon

      public double y()
      Returns the y component.
      Returns:
      the y component
    • z Link icon

      public double z()
      Returns the z component.
      Returns:
      the z component
    • r2 Link icon

      public double r2()
    • r Link icon

      public double r()
    • phi Link icon

      public double phi()
      Returns the angle between the x axis and the orthogonal projection of this vector onto the x-y plane.
      Returns:
      the phi component of this vector's spherical coordinates
    • costheta Link icon

      public double costheta()
    • theta Link icon

      public double theta()
      Returns the angle between this vector and the z axis.
      Returns:
      the theta component of this vector's spherical coordinates
    • rho2 Link icon

      public double rho2()
    • rho Link icon

      public double rho()
      Returns the length of the orthogonal projection of this vector onto the x-y plane.
      Returns:
      sqrt(x*x + y*y)
    • isNull Link icon

      public boolean isNull()
      Returns true if this vector is a null vector.
      Returns:
      true if this vector is a null vector
    • mag2 Link icon

      public double mag2()
      Returns the square of the magnitude of this vector.
      Returns:
      the square of the magnitude of this vector
    • mag Link icon

      public double mag()
      Returns the magnitude of this vector.
      Returns:
      the magnitude of this vector
    • setMagThetaPhi Link icon

      public void setMagThetaPhi(double mag, double theta, double phi)
      Sets this vector based on the given spherical coordinates.
      Parameters:
      mag - the magnitude
      theta - the angle between the vector and the z axis
      phi - the angle between the x axis and the orthogonal projection of the vector onto the x-y plane
    • negative Link icon

      public void negative()
      Negates this vector's components.
    • add Link icon

      public Vector3D add(Vector3D vector)
      Adds the given vector to this vector.
      Parameters:
      vector - the vector to add to this vector
    • sub Link icon

      public Vector3D sub(Vector3D vector)
      Subtracts the given vector from this vector
      Parameters:
      vector - the vector to subtract from this vector
    • scale Link icon

      public void scale(double factor)
      Scales each of this vectors components by the given scale factor.
      Parameters:
      factor - the amount to scale the components of this vector
    • unit Link icon

      public boolean unit()
      Sets the magnitude of this vector to equal one unless this vector is a null vector.
      Returns:
      returns false if this vector is a null vector
    • asUnit Link icon

      public Vector3D asUnit()
      Create unit vector from this
      Returns:
      unit vector
    • setMag Link icon

      public boolean setMag(double mag)
      Sets the magnitude of this vector to equal the given magnitude unless vector is a null vector.
      Parameters:
      mag - the desired magnitude
      Returns:
      returns false if this vector is a null vector
    • add Link icon

      public Vector3D add(double a)
    • subtract Link icon

      public Vector3D subtract(double a)
    • multiply Link icon

      public Vector3D multiply(double a)
    • divide Link icon

      public Vector3D divide(double a)
    • dot Link icon

      public double dot(Vector3D vector)
      Returns the dot product of this vector and the given vector.
      Parameters:
      vector - the vector to dot product with this vector
      Returns:
      the dot product
    • cross Link icon

      public Vector3D cross(Vector3D vector)
      Constructs a new Vector3D containing the cross product of this vector and the given vector.
      Parameters:
      vector - the vector to cross product with this vector
      Returns:
      the cross product
    • toPoint3D Link icon

      public Point3D toPoint3D()
      Constructs a new Point3D using this vector's x, y, and z components.
      Returns:
      a point representation of this vector
    • compare Link icon

      public double compare(Vector3D vector)
      Returns a value greater than or equal to zero proportional to the amount that the two vectors differ. If the two vectors are equal then the returned value is 0.
      Parameters:
      vector - the vector to compare to
      Returns:
      the comparison result
    • rotate Link icon

      public void rotate(Point3D point, double angle)
      Rotates the given point clockwise around the axis produced by this vector by the given angle
      Parameters:
      point - the point to rotate
      angle - the angle of rotation
    • rotate Link icon

      public void rotate(Vector3D vector, double angle)
      Rotates the given vector clockwise around the axis produced by this vector by the given angle.
      Parameters:
      vector - the vector to rotate
      angle - the angle of rotation
    • angle Link icon

      public double angle(Vector3D vector0, Vector3D vector1)
      Calculates the clockwise angle of rotation from the image of the first given vector to the image of the second given vector projected onto a plane who's normal is this vector and using this vector as an axis.
      Parameters:
      vector0 - the first vector
      vector1 - the second vector
      Returns:
      the angle between the two vectors
    • projection Link icon

      public Vector3D projection(Vector3D v)
      Projection of this vector onto another vector
      Parameters:
      v - another vector
      Returns:
      projection of this vector onto v
    • angle Link icon

      public double angle(Vector3D v)
      Angle between this and another vector
      Parameters:
      v - another vector
      Returns:
      the angle between the two vectors
    • translateXYZ Link icon

      public void translateXYZ(double x, double y, double z)
      Does nothing; vectors cannot be translated.
      Specified by:
      translateXYZ in interface Transformable
      Parameters:
      x - ignored
      y - ignored
      z - ignored
    • rotateX Link icon

      public void rotateX(double angle)
      Description copied from interface: Transformable
      Rotates this object clockwise around the x axis.
      Specified by:
      rotateX in interface Transformable
      Parameters:
      angle - rotation angle in radians
    • rotateY Link icon

      public void rotateY(double angle)
      Description copied from interface: Transformable
      Rotates this object clockwise around the y axis.
      Specified by:
      rotateY in interface Transformable
      Parameters:
      angle - rotation angle in radians
    • rotateZ Link icon

      public void rotateZ(double angle)
      Description copied from interface: Transformable
      Rotates this object clockwise around the z axis.
      Specified by:
      rotateZ in interface Transformable
      Parameters:
      angle - rotation angle in radians
    • show Link icon

      public void show()
      Invokes System.out.println(this).
      Specified by:
      show in interface Showable
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object
    • toStringBrief Link icon

      public String toStringBrief(int ndigits)
    • toStringBrief Link icon

      public String toStringBrief()
    • main Link icon

      public static void main(String[] args)