Class Point

java.lang.Object
edu.cnu.mdi.geometry.Point
Direct Known Subclasses:
Vector

public class Point extends Object
A mutable point in three-dimensional Cartesian space.

The coordinates are public for lightweight geometry use and for compatibility with existing MDI code. The same class is also used in places where a tuple is naturally treated as a vector, for example in dot products, subtraction, and scaling. For operations that should explicitly represent a direction rather than a location, use Vector.

Author:
heddle
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    double
    The x coordinate.
    double
    The y coordinate.
    double
    The z coordinate.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a point at the origin.
    Point(double[] p)
    Creates a point from an array containing x, y, and z, in that order.
    Point(double x, double y, double z)
    Creates a point from its Cartesian coordinates.
    Creates a copy of another point.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Point other)
    Adds another point or vector to this point, coordinate by coordinate.
    static Point
    Computes the coordinate-wise difference a - b.
    static void
    Computes the coordinate-wise difference a - b and stores it in an existing point.
    double
    distance(double x, double y, double z)
    Computes the Euclidean distance from this point to another set of coordinates.
    double
    Computes the Euclidean distance from this point to another point.
    static double
    distance(Point p1, Point p2)
    Computes the Euclidean distance between two points.
    double
    Computes the dot product of this tuple with another tuple.
    static double
    dot(Point a, Point b)
    Computes the dot product of two tuples.
    scale(double scalar)
    Multiplies this point's coordinates by a scalar.
    void
    set(double x, double y, double z)
    Sets this point's coordinates.
    void
    Sets this point's coordinates from another point.
    subtract(Point other)
    Subtracts another point from this point.
    Returns a formatted string representation of this point.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • x

      public double x
      The x coordinate.
    • y

      public double y
      The y coordinate.
    • z

      public double z
      The z coordinate.
  • Constructor Details

    • Point

      public Point()
      Creates a point at the origin.
    • Point

      public Point(Point p)
      Creates a copy of another point.
      Parameters:
      p - the point to copy
      Throws:
      NullPointerException - if p is null
    • Point

      public Point(double x, double y, double z)
      Creates a point from its Cartesian coordinates.
      Parameters:
      x - the x coordinate
      y - the y coordinate
      z - the z coordinate
    • Point

      public Point(double[] p)
      Creates a point from an array containing x, y, and z, in that order.
      Parameters:
      p - an array containing at least three values
      Throws:
      NullPointerException - if p is null
      IndexOutOfBoundsException - if p.length < 3
  • Method Details

    • set

      public void set(double x, double y, double z)
      Sets this point's coordinates.
      Parameters:
      x - the new x coordinate
      y - the new y coordinate
      z - the new z coordinate
    • set

      public void set(Point p)
      Sets this point's coordinates from another point.
      Parameters:
      p - the point whose coordinates will be copied
      Throws:
      NullPointerException - if p is null
    • difference

      public static Point difference(Point a, Point b)
      Computes the coordinate-wise difference a - b.
      Parameters:
      a - the minuend point
      b - the subtrahend point
      Returns:
      a new point containing a - b
      Throws:
      NullPointerException - if either argument is null
    • subtract

      public Point subtract(Point other)
      Subtracts another point from this point.
      Parameters:
      other - the point to subtract
      Returns:
      a new point containing this - other
      Throws:
      NullPointerException - if other is null
    • difference

      public static void difference(Point a, Point b, Point c)
      Computes the coordinate-wise difference a - b and stores it in an existing point.
      Parameters:
      a - the minuend point
      b - the subtrahend point
      c - upon return, contains a - b
      Throws:
      NullPointerException - if any argument is null
    • dot

      public double dot(Point v)
      Computes the dot product of this tuple with another tuple.
      Parameters:
      v - the other point or vector
      Returns:
      this.x*v.x + this.y*v.y + this.z*v.z
      Throws:
      NullPointerException - if v is null
    • dot

      public static double dot(Point a, Point b)
      Computes the dot product of two tuples.
      Parameters:
      a - one point or vector
      b - the other point or vector
      Returns:
      a.x*b.x + a.y*b.y + a.z*b.z
      Throws:
      NullPointerException - if either argument is null
    • toString

      public String toString()
      Returns a formatted string representation of this point.
      Overrides:
      toString in class Object
      Returns:
      a string containing the three coordinates
    • distance

      public double distance(double x, double y, double z)
      Computes the Euclidean distance from this point to another set of coordinates.
      Parameters:
      x - the x coordinate of the other point
      y - the y coordinate of the other point
      z - the z coordinate of the other point
      Returns:
      the Euclidean distance between the two points
    • distance

      public double distance(Point p)
      Computes the Euclidean distance from this point to another point.
      Parameters:
      p - the other point
      Returns:
      the Euclidean distance between the points
      Throws:
      NullPointerException - if p is null
    • distance

      public static double distance(Point p1, Point p2)
      Computes the Euclidean distance between two points.
      Parameters:
      p1 - one point
      p2 - the other point
      Returns:
      the Euclidean distance between the two points
      Throws:
      NullPointerException - if either argument is null
    • add

      public Point add(Point other)
      Adds another point or vector to this point, coordinate by coordinate.
      Parameters:
      other - the point or vector to add
      Returns:
      a new point containing this + other
      Throws:
      NullPointerException - if other is null
    • scale

      public Point scale(double scalar)
      Multiplies this point's coordinates by a scalar.
      Parameters:
      scalar - the scalar multiplier
      Returns:
      a new point containing scalar * this