Class Vector3D

Object
Point3D
Vector3D
All Implemented Interfaces:
Point

public class Vector3D extends Point3D
Utility class for 3D vector manipulations.
Author:
Christoph Hauert
  • Constructor Details

    • Vector3D

      public Vector3D()
      Create a new 3D vector (0,0,0).
    • Vector3D

      public Vector3D(Vector3D p)
      Create a new 3D vector from point p.
      Parameters:
      p - the point/vector to copy
    • Vector3D

      public Vector3D(double x, double y, double z)
      Create a new 3D vector with coordinates (x,y,z).
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      z - the z-coordinate
  • Method Details

    • set

      public Vector3D set(Point3D from, Point3D to)
      Set the 3D vector pointing from from to to.
      Parameters:
      from - the starting point
      to - the end point
      Returns:
      the new vector
    • add

      public Vector3D add(double dx, double dy, double dz)
      Shift the coordinates of this 3D vector by dx, dy and dz in the x-, y-, and z-coordinates, respectively.
      Parameters:
      dx - the shift in the x-coordinate
      dy - the shift in the y-coordinate
      dz - the shift in the z-coordinate
      Returns:
      the shifted vector (x+dx,y+dy,z+dz)
    • add

      public Vector3D add(Vector3D add)
      Add the 3D vector add to this vector.
      Parameters:
      add - the vector to add
      Returns:
      this new vector
    • add

      public Vector3D add(Point3D a, Point3D b)
      Add the 3D vectors a and b and store result in this vector.
      Parameters:
      a - the first vector
      b - the second vector
      Returns:
      the new vector a+b
    • sub

      public Vector3D sub(double dx, double dy, double dz)
      Subtract dx, dy and dz from the x-, y-, and z-coordinates, respectively. This yields the same result as add(-dx, -dy, -dz).
      Parameters:
      dx - the shift in the x-coordinate
      dy - the shift in the y-coordinate
      dz - the shift in the z-coordinate
      Returns:
      this new 3D vector (x-dx,y-dy,z-dz)
    • sub

      public Vector3D sub(Vector3D sub)
      Subtract the 3D vector sub from this vector.
      Parameters:
      sub - the vector to subtract
      Returns:
      this new vector
    • sub

      public Vector3D sub(Vector3D a, Vector3D b)
      Subtract 3D vectors b from a and store result in this 3D vector.
      Parameters:
      a - the first vector for subtraction
      b - the vector to subtract from a
      Returns:
      the vector a-b
    • dot

      public double dot(Vector3D d)
      Calculate the dot product of 3D vector d and this vector.
      Parameters:
      d - the vector to calculate the dot product with
      Returns:
      the dot product v.d
    • cross

      public Vector3D cross(Vector3D d)
      Calculate the cross product of the 3D vector d and this vector.
      Parameters:
      d - the vector for cross product
      Returns:
      the cross product v x d
    • cross

      public Vector3D cross(Vector3D a, Vector3D b)
      Calculate the cross product of two 3D vectors a, b and store result in this vector.
      Parameters:
      a - the first vector
      b - the second vector
      Returns:
      the cross product v=a x b
    • negate

      public Vector3D negate()
      Negates the coordinates of 3D vector v to obtain -v.
      Returns:
      the vector v=-v
    • normalize

      public Vector3D normalize()
      Normalizes the 3D vector v such that its length is |v|=1 while preserving the direction.
      Returns:
      the normalized vector
      See Also:
    • normalize

      public Vector3D normalize(double l)
      Normalizes the 3D vector v such that its length is l, i.e. |v|=l, while preserving the direction.
      Parameters:
      l - the new length of the vector
      Returns:
      the scaled vector
      See Also:
    • scaleAdd

      public Vector3D scaleAdd(double s, Vector3D a)
      Scales the 3D vector v by the scalar factor s and adds the 3D vector a, v=s*v+a. This is a shortcut for v.scale(s).add(a).
      Parameters:
      s - the scalar factor
      a - the vector to add
      Returns:
      the new vector s*v+a
    • length

      public double length()
      Calculate the length of the 3D vector.

      For computational efficiency the fairly expensive square-roots calculations should be avoided whenever possible.

      Returns:
      the length of vector |v|
      See Also:
    • length2

      public double length2()
      Calculate the length squared of the 3D vector, |v|2.
      Returns:
      the length squared
      See Also:
    • lengthSquared

      public double lengthSquared()
      Same as length2(). For (historical) compatibility with Vector3f from java3d.
      Returns:
      the length squared
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Point3D