Class Line2D

Object
Line2D
Direct Known Subclasses:
Segment2D

public class Line2D extends Object
Utility class for deal with straight (infinite) lines in 2D.
Note: the drop-in replacement for java.awt.geom.Line2D.Double is not Line2D but rather Segment2D for straight lines of finite length.
Author:
Christoph Hauert
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    double
    The y-intercept of the straight line.
    double
    The slope of the straight line.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new 2D line with the slope and y-intercept of zero (corresponding to the x-axis).
    Line2D(double m, double b)
    Create a new 2D line with slope m and y-intercept b.
    Create a copy of the 2D line l.
    Create a new 2D line through points p1 and p2.
    Line2D(Point2D origin, Vector2D direction)
    Create a new 2D line with origin and direction.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    above(double x, double y)
    Check if point (x,y) lies above or below the line.
    boolean
    Check if point p lies above or below the line.
    double
    Calculate the distance between point p and the line.
    static double
    Calculate the squared distance between point p and the line l.
    double
    Calculate the squared distance between point p and the line.
    Return the intersection of two lines or Double.NaN if no intersection.
    Return the intersection of the line and the segment s or Double.NaN if there is no intersection.
    boolean
    Check if the line intersects the line l.
    boolean
    Check if the line intersects the rectangle r.
    boolean
    Check if the line intersects the line segment s.
    boolean
    Check if two lines are parallel.
    set(double m, double b)
    Set the slope m and y-intercept b of the line.
    set(double x1, double y1, double x2, double y2)
    Set the line to pass through points (x1,y1) and (x2,y2).
    set(Point2D p1, Point2D p2)
    Set the line to pass through points p1 and p2.
    set(Point2D point, Vector2D direction)
    Set the line to pass through point p in the direction of vector v.
    void
    Set the line to pass through the points p1 and p2.
    shift(double dx, double dy)
    Shift the line right by dx and up by dy.
     
    boolean
    Check if the line is vertical.
    double
    y(double x)
    Return the y-value, y = m*x+b, for given the x-value.

    Methods inherited from class Object

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

    • m

      public double m
      The slope of the straight line.

      Note: for vertical lines slope is Double.NaN

    • b

      public double b
      The y-intercept of the straight line.

      Note: for vertical lines this is the x-intercept.

  • Constructor Details

    • Line2D

      public Line2D()
      Create a new 2D line with the slope and y-intercept of zero (corresponding to the x-axis).
    • Line2D

      public Line2D(Line2D l)
      Create a copy of the 2D line l.
      Parameters:
      l - the 2D line to copy
    • Line2D

      public Line2D(double m, double b)
      Create a new 2D line with slope m and y-intercept b.
      Parameters:
      m - the slope
      b - the y-intercept
    • Line2D

      public Line2D(Point2D origin, Vector2D direction)
      Create a new 2D line with origin and direction.
      Parameters:
      origin - the starting point
      direction - the direction
    • Line2D

      public Line2D(Point2D p1, Point2D p2)
      Create a new 2D line through points p1 and p2.
      Parameters:
      p1 - the first point
      p2 - the second point
  • Method Details

    • set

      public Line2D set(Point2D point, Vector2D direction)
      Set the line to pass through point p in the direction of vector v.
      Parameters:
      point - the point on the line
      direction - the direction of the line
      Returns:
      the new line
    • set

      public Line2D set(double m, double b)
      Set the slope m and y-intercept b of the line.
      Parameters:
      m - the slope
      b - the y-intercept
      Returns:
      the new line
    • set

      public Line2D set(Point2D p1, Point2D p2)
      Set the line to pass through points p1 and p2.
      Parameters:
      p1 - the first point on the line
      p2 - the second point on the line
      Returns:
      the new line
    • setLine

      public void setLine(Point2D p1, Point2D p2)
      Set the line to pass through the points p1 and p2.

      Note: for compatibility with java.awt.geom.Line2D.Double

      Parameters:
      p1 - the first point on the line
      p2 - the second point on the line
    • set

      public Line2D set(double x1, double y1, double x2, double y2)
      Set the line to pass through points (x1,y1) and (x2,y2).
      Parameters:
      x1 - the x-coordinate of first point
      y1 - the y-coordinate of first point
      x2 - the x-coordinate of second point
      y2 - the y-coordinate of second point
      Returns:
      the new line
    • shift

      public Line2D shift(double dx, double dy)
      Shift the line right by dx and up by dy.
      Parameters:
      dx - the horizontal shift
      dy - the vertical shift
      Returns:
      the new line
    • y

      public double y(double x)
      Return the y-value, y = m*x+b, for given the x-value.
      Parameters:
      x - the x-value
      Returns:
      the y-value
    • above

      public boolean above(Point2D p)
      Check if point p lies above or below the line. Above means the y-coordinate of the point exceeds that of the line at the corresponding x-coordinate.
      Parameters:
      p - the point to check
      Returns:
      true if points lies above line.
    • above

      public boolean above(double x, double y)
      Check if point (x,y) lies above or below the line. Above means the y-coordinate of the point exceeds that of the line at the corresponding x-coordinate.
      Parameters:
      x - the x-coordinate of point
      y - the y-coordinate of point
      Returns:
      true if points lies above line.
    • distance

      public double distance(Point2D p)
      Calculate the distance between point p and the line.
      Parameters:
      p - the point to find distance from line
      Returns:
      the distance
    • distance2

      public double distance2(Point2D p)
      Calculate the squared distance between point p and the line.
      Parameters:
      p - the point to find squared distance from line
      Returns:
      the squared distance
    • distance2

      public static double distance2(Line2D l, Point2D p)
      Calculate the squared distance between point p and the line l.
      Parameters:
      l - the line to find distance from
      p - the point to find distance to line
      Returns:
      the squared distance
    • parallel

      public boolean parallel(Line2D l)
      Check if two lines are parallel.

      Note: Double.compare returns zero for two NaN's, i.e. for two vertical lines and hence works as expected.

      Parameters:
      l - the line to compare to
      Returns:
      true if the lines are parallel
    • vertical

      public boolean vertical()
      Check if the line is vertical.
      Returns:
      true if the line is vertical
    • intersects

      public boolean intersects(Line2D l)
      Check if the line intersects the line l.
      Parameters:
      l - the line to check
      Returns:
      true if the lines intersect
    • intersects

      public boolean intersects(Segment2D s)
      Check if the line intersects the line segment s.
      Parameters:
      s - the line segment to check
      Returns:
      true if the line segment intersects
    • intersects

      public boolean intersects(Rectangle2D r)
      Check if the line intersects the rectangle r.
      Parameters:
      r - the rectangle to check
      Returns:
      true if the line intersects the rectangle
    • intersection

      public Point2D intersection(Line2D l)
      Return the intersection of two lines or Double.NaN if no intersection.
      Parameters:
      l - the line to check
      Returns:
      the intersection point
    • intersection

      public Point2D intersection(Segment2D s)
      Return the intersection of the line and the segment s or Double.NaN if there is no intersection.
      Parameters:
      s - the line segment to check
      Returns:
      the intersection point.
    • toString

      public String toString()
      Overrides:
      toString in class Object