Class Line

java.lang.Object
edu.cnu.mdi.geometry.Line

public class Line extends Object
A three-dimensional parametric line, represented as p(t) = p0 + t * dp.

The same representation can be used as an infinite line or as a directed line segment. For an infinite line, t may be any real value. For the directed segment from p0 to p1, the segment corresponds to 0 <= t <= 1.

Author:
heddle
  • Constructor Summary

    Constructors
    Constructor
    Description
    Line(double[] p1, double[] p2)
    Creates a line from two arrays containing x, y, and z values.
    Line(Line line)
    Creates a copy of another line.
    Line(Point po, Point p1)
    Creates a line from two points.
    Creates a line through the origin in the direction of a vector.
  • Method Summary

    Modifier and Type
    Method
    Description
    Finds the point on this infinite line closest to a given point.
    double
    Computes the shortest distance from this infinite line to a point.
    Gets the midpoint of the directed segment from p0 to p1.
    Gets the line direction vector dp = p1 - p0.
    getP(double t)
    Gets a point on the parametric line.
    void
    getP(double t, Point p)
    Gets a point on the parametric line and stores it in an existing point.
    Gets the line's base point.
    Gets the endpoint corresponding to t = 1.
    double
    Computes the line parameter of the point on this infinite line closest to a given point.
    Returns a string representation of this line.

    Methods inherited from class java.lang.Object

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

    • Line

      public Line(Point po, Point p1)
      Creates a line from two points.

      If the line is interpreted as a directed segment, it runs from po to p1.

      Parameters:
      po - one point on the line
      p1 - another point on the line
      Throws:
      NullPointerException - if either point is null
      IllegalArgumentException - if the two points are coincident, non-finite, or too close to define a stable direction
    • Line

      public Line(double[] p1, double[] p2)
      Creates a line from two arrays containing x, y, and z values.
      Parameters:
      p1 - one point as an xyz array
      p2 - another point as an xyz array
      Throws:
      NullPointerException - if either array is null
      IndexOutOfBoundsException - if either array has fewer than three values
      IllegalArgumentException - if the two points are coincident or too close to define a stable direction
    • Line

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

      public Line(Vector v)
      Creates a line through the origin in the direction of a vector.
      Parameters:
      v - the direction vector
      Throws:
      NullPointerException - if v is null
      IllegalArgumentException - if v is the zero vector or too close to zero to define a stable direction
  • Method Details

    • getP0

      public Point getP0()
      Gets the line's base point.

      This is an arbitrary point on an infinite line and the start point if this object is interpreted as a directed segment.

      Returns:
      a copy of the base point
    • getDelP

      public Vector getDelP()
      Gets the line direction vector dp = p1 - p0.
      Returns:
      a copy of the direction vector
    • getP1

      public Point getP1()
      Gets the endpoint corresponding to t = 1.

      This is an arbitrary second point on an infinite line and the end point if this object is interpreted as a directed segment.

      Returns:
      the point p0 + dp
    • getP

      public Point getP(double t)
      Gets a point on the parametric line.
      Parameters:
      t - the line parameter; use 0 <= t <= 1 for the finite directed segment
      Returns:
      the point p0 + t*dp
    • getP

      public void getP(double t, Point p)
      Gets a point on the parametric line and stores it in an existing point.
      Parameters:
      t - the line parameter; use 0 <= t <= 1 for the finite directed segment
      p - upon return, contains p0 + t*dp
      Throws:
      NullPointerException - if p is null
    • distance

      public double distance(Point p)
      Computes the shortest distance from this infinite line to a point.

      This method ignores segment endpoints. To use the finite segment distance, clamp the parameter returned by parameterOfClosestPoint(Point) to the interval [0, 1].

      Parameters:
      p - the point
      Returns:
      the perpendicular distance from p to the infinite line
      Throws:
      NullPointerException - if p is null
    • parameterOfClosestPoint

      public double parameterOfClosestPoint(Point p)
      Computes the line parameter of the point on this infinite line closest to a given point.
      Parameters:
      p - the point
      Returns:
      the parameter t for the closest point p0 + t*dp
      Throws:
      NullPointerException - if p is null
    • closestPointOnLine

      public Point closestPointOnLine(Point p)
      Finds the point on this infinite line closest to a given point.
      Parameters:
      p - the given point
      Returns:
      the closest point on the infinite line
      Throws:
      NullPointerException - if p is null
    • toString

      public String toString()
      Returns a string representation of this line.
      Overrides:
      toString in class Object
      Returns:
      a string containing the two defining points
    • getCenter

      public Point getCenter()
      Gets the midpoint of the directed segment from p0 to p1.
      Returns:
      the point corresponding to t = 0.5