Class Vector

java.lang.Object
edu.cnu.mdi.geometry.Point
edu.cnu.mdi.geometry.Vector

public class Vector extends Point
A mutable three-dimensional Cartesian vector.

This class extends Point so that existing MDI code can use the same public x, y, and z fields for points and vectors. The distinction is semantic: a Point represents a location, while a Vector represents a displacement or direction.

Author:
heddle
  • Constructor Details

    • Vector

      public Vector()
      Creates the zero vector.
    • Vector

      public Vector(Point p)
      Creates a vector from a point-like tuple.
      Parameters:
      p - the tuple whose components will be copied
      Throws:
      NullPointerException - if p is null
    • Vector

      public Vector(double x, double y, double z)
      Creates a vector from its Cartesian components.
      Parameters:
      x - the x component
      y - the y component
      z - the z component
  • Method Details

    • lengthSquared

      public double lengthSquared()
      Computes the square of this vector's Euclidean length.
      Returns:
      x*x + y*y + z*z
    • length

      public double length()
      Computes this vector's Euclidean length.
      Returns:
      the vector length
    • cross

      public static Vector cross(Vector a, Vector b)
      Computes the cross product a x b.
      Parameters:
      a - the first vector
      b - the second vector
      Returns:
      a new vector containing a x b
      Throws:
      NullPointerException - if either argument is null
    • cross

      public static void cross(Vector a, Vector b, Vector c)
      Computes the cross product a x b and stores it in an existing vector.

      All three result components are computed into temporaries before any are written, so the output vector may safely be the same object as either input.

      Parameters:
      a - the first vector
      b - the second vector
      c - upon return, contains a x b
      Throws:
      NullPointerException - if any argument is null
    • unitVector

      public Vector unitVector()
      Computes a unit vector in the same direction as this vector.
      Returns:
      a new unit vector, or null if this vector is too close to zero to normalize