Class Line2D

java.lang.Object
com.irurueta.geometry.Line2D
All Implemented Interfaces:
Serializable

public class Line2D extends Object implements Serializable
Line2D in R2. Lines can be expressed using the following expression: A * x + B * y + C = 0 where A and B are different from zero. Changing this expression to a y = m * x + b format leads to the following definitions: m: slope of the line. Defined as m = -A/B b: interception point of this line. Defined as b = -c/B angle of the line with respect to the x-axis = Math.atan(slope).
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private double
    Parameter A of a line.
    private double
    Parameter B of a line.
    private double
    Parameter C of a line.
    static final double
    Defines the threshold used when comparing two values.
    static final double
    Positive threshold determine whether points lay inside (is locus) of a given line or not.
    private static final int
    Constant defining the size of vector that define the direction of a line.
    static final int
    Number of line parameters.
    static final double
    Minimum allowed threshold.
    private boolean
    Indicates if line is normalized or not.
    private static final double
    Machine precision.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
    Line2D(double[] array)
    Constructor.
    Line2D(double a, double b, double c)
    Constructor with parameters.
    Line2D(Point2D point, double[] vector)
    Constructor of a line from one point and its director vector.
    Line2D(Point2D pointA, Point2D pointB)
    Constructor.
    Line2D(Point2D pointA, Point2D pointB, boolean noThrow)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    double[]
    Returns parameters of this line as an array containing [a, b, c].
    void
    asArray(double[] array)
    Stores the parameters of this line in provided array as [a, b, c].
    void
    closestPoint(Point2D point, Point2D result)
    Computes the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line.
    void
    closestPoint(Point2D point, Point2D result, double threshold)
    Computes the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line.
    static Line2D
    Creates a new instance of a 2D line located at the canonical infinity.
    void
    directorVector(double[] directorVector)
    Computes director vector of this line and stores the result in provided array.
    double
    Computes the dot product between the parameters A, B,C of this line and the ones of provided line.
    boolean
    equals(Line2D line)
    Checks if the line described by this instance equals provided line up to default comparison threshold.
    boolean
    equals(Line2D line, double threshold)
    Checks if the line described by this instance equals provided line up to provided threshold.
    boolean
    Checks if provided object equals current line.
    double
    Returns parameter A of this line.
    double
    Returns the angle of this line in radians.
    double
    Returns parameter B of this line.
    double
    Returns parameter C of this line.
    Returns the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line.
    getClosestPoint(Point2D point, double threshold)
    Returns the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line.
    double[]
    Returns director vector of this line.
    Computes the intersection of this line with provided line.
    double
    Returns the slope of this line.
    double
    Returns the y-coordinate intercept point of this line.
    int
    Returns hash code value.
    void
    intersection(Line2D otherLine, Point2D result)
    Computes the intersection of this line with provided line.
    boolean
    Returns boolean indicating whether provided point lies within this line (at a maximum distance of DEFAULT_LOCUS_THRESHOLD).
    boolean
    isLocus(Point2D point, double threshold)
    Returns boolean indicating whether provided point lies within this line (at a maximum distance of provided threshold).
    boolean
    Returns boolean indicating whether this line has already been normalized.
    void
    Normalizes the parameters of this line to increase the accuracy of some computations.
    void
    setA(double a)
    Sets parameter A of this line.
    void
    setAngle(double angle)
    Sets angle of this line in radians.
    static void
    Sets provided 2D line into the canonical infinity.
    void
    setB(double b)
    Sets parameter B of this line.
    void
    setC(double c)
    Sets parameter C of this line.
    final void
    setParameters(double[] array)
    Sets parameters of this line.
    final void
    setParameters(double a, double b, double c)
    Sets parameters of this line.
    final void
    Sets parameters of this line from a pair of 2D points.
    final void
    setParametersFromPairOfPoints(Point2D pointA, Point2D pointB, boolean noThrow)
    Sets parameters of this line from a pair of 2D points.
    final void
    Sets parameters of a 2D line from one point and its director vector.
    void
    setSlope(double slope)
    Sets the slope of this line.
    void
    setYIntercept(double yIntercept)
    Sets the y-coordinate intercept point of this line.
    double
    Distance between a line and a point.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LINE_NUMBER_PARAMS

      public static final int LINE_NUMBER_PARAMS
      Number of line parameters.
      See Also:
    • DEFAULT_LOCUS_THRESHOLD

      public static final double DEFAULT_LOCUS_THRESHOLD
      Positive threshold determine whether points lay inside (is locus) of a given line or not.
      See Also:
    • MIN_THRESHOLD

      public static final double MIN_THRESHOLD
      Minimum allowed threshold.
      See Also:
    • DEFAULT_COMPARISON_THRESHOLD

      public static final double DEFAULT_COMPARISON_THRESHOLD
      Defines the threshold used when comparing two values.
      See Also:
    • INHOM_VECTOR_SIZE

      private static final int INHOM_VECTOR_SIZE
      Constant defining the size of vector that define the direction of a line.
      See Also:
    • PRECISION

      private static final double PRECISION
      Machine precision.
      See Also:
    • a

      private double a
      Parameter A of a line.
    • b

      private double b
      Parameter B of a line.
    • c

      private double c
      Parameter C of a line.
    • normalized

      private boolean normalized
      Indicates if line is normalized or not.
  • Constructor Details

    • Line2D

      public Line2D()
      Constructor.
    • Line2D

      public Line2D(double a, double b, double c)
      Constructor with parameters. Parameters of a line are provided in the following homogeneous format: A * x + B * y + C = 0
      Parameters:
      a - Parameter A of a line.
      b - Parameter B of a line.
      c - Parameter C of a line.
    • Line2D

      public Line2D(Point2D pointA, Point2D pointB, boolean noThrow) throws CoincidentPointsException
      Constructor. This constructor takes two 2D points to build a line passing through both of them.
      Parameters:
      pointA - First point used to compute the line.
      pointB - Second point used to compute the line.
      noThrow - If true no exception is thrown even if points are coincident.
      Throws:
      CoincidentPointsException - Raised if points are equal.
    • Line2D

      public Line2D(Point2D pointA, Point2D pointB)
      Constructor. This constructor takes two 2D points to build a line passing through both of them.
      Parameters:
      pointA - First point used to compute the line.
      pointB - Second point used to compute the line.
    • Line2D

      public Line2D(double[] array)
      Constructor.
      Parameters:
      array - Array containing the three parameters of a line (A, B, C).
      Throws:
      IllegalArgumentException - Raised if length of provided array is not three.
    • Line2D

      public Line2D(Point2D point, double[] vector)
      Constructor of a line from one point and its director vector.
      Parameters:
      point - point passing through the line.
      vector - director vector.
      Throws:
      IllegalArgumentException - raised if vector length is not 2.
  • Method Details

    • getA

      public double getA()
      Returns parameter A of this line.
      Returns:
      Parameter A of this line.
    • getB

      public double getB()
      Returns parameter B of this line.
      Returns:
      Parameter B of this line.
    • getC

      public double getC()
      Returns parameter C of this line.
      Returns:
      Parameter C of this line.
    • setParameters

      public final void setParameters(double a, double b, double c)
      Sets parameters of this line.
      Parameters:
      a - Parameter A of this line.
      b - Parameter B of this line.
      c - Parameter C of this line.
    • setParameters

      public final void setParameters(double[] array)
      Sets parameters of this line.
      Parameters:
      array - Array containing parameters of this line.
      Throws:
      IllegalArgumentException - Raised if provided array does not have length equal to 3.
    • setA

      public void setA(double a)
      Sets parameter A of this line.
      Parameters:
      a - Parameter A of this line.
    • setB

      public void setB(double b)
      Sets parameter B of this line.
      Parameters:
      b - Parameter B of this line.
    • setC

      public void setC(double c)
      Sets parameter C of this line.
      Parameters:
      c - Parameter C of this line.
    • getSlope

      public double getSlope()
      Returns the slope of this line.
      Returns:
      Slope of this line.
    • setSlope

      public void setSlope(double slope)
      Sets the slope of this line.
      Parameters:
      slope - Slope of this line.
    • getAngle

      public double getAngle()
      Returns the angle of this line in radians.
      Returns:
      Angle of this line in radians.
    • setAngle

      public void setAngle(double angle)
      Sets angle of this line in radians.
      Parameters:
      angle - Angle of this line in radians.
    • getYIntercept

      public double getYIntercept()
      Returns the y-coordinate intercept point of this line. For a line following the expression A * X + B * y + C = 0 this method evaluates y for x = 0.
      Returns:
      Vertical coordinate where the line intercepts with the Y axis.
    • setYIntercept

      public void setYIntercept(double yIntercept)
      Sets the y-coordinate intercept point of this line. For a line following the expression A * x + B * y + C = 0 this method recalculates the parameters that define the line.
      Parameters:
      yIntercept - Vertical coordinate where the line intercepts with the Y axis.
    • setParametersFromPairOfPoints

      public final void setParametersFromPairOfPoints(Point2D pointA, Point2D pointB, boolean noThrow) throws CoincidentPointsException
      Sets parameters of this line from a pair of 2D points. The line will pass through both points.
      Parameters:
      pointA - First point laying on this line.
      pointB - Second point laying on this line.
      noThrow - Enables/disables throwing exceptions.
      Throws:
      CoincidentPointsException - Raised if provided points are equal.
    • setParametersFromPairOfPoints

      public final void setParametersFromPairOfPoints(Point2D pointA, Point2D pointB)
      Sets parameters of this line from a pair of 2D points. The line will pass through both points.
      Parameters:
      pointA - First point laying on this line.
      pointB - Second point laying on this line.
    • setParametersFromPointAndDirectorVector

      public final void setParametersFromPointAndDirectorVector(Point2D point, double[] vector)
      Sets parameters of a 2D line from one point and its director vector.
      Parameters:
      point - point passing through the line.
      vector - director vector.
      Throws:
      IllegalArgumentException - raised if vector length is not 2.
    • isLocus

      public boolean isLocus(Point2D point, double threshold)
      Returns boolean indicating whether provided point lies within this line (at a maximum distance of provided threshold).
      Parameters:
      point - Point to be checked.
      threshold - Threshold to determine whether the point lies inside the line.
      Returns:
      True if point lies inside the line (is locus), false otherwise.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • isLocus

      public boolean isLocus(Point2D point)
      Returns boolean indicating whether provided point lies within this line (at a maximum distance of DEFAULT_LOCUS_THRESHOLD).
      Parameters:
      point - Point to be checked.
      Returns:
      True if point lies inside the line (is locus), false otherwise.
    • signedDistance

      public double signedDistance(Point2D point)
      Distance between a line and a point. Returned distance equals to the Euclidean distance between this line and provided point but having sign. Sign indicates whether point is at one side or the other of the line.
      Parameters:
      point - Point whose distance to this line will be computed.
      Returns:
      Distance between this line and provided point.
    • getClosestPoint

      public Point2D getClosestPoint(Point2D point)
      Returns the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line. If provided point belong to this line, then the same point will be returned as a result.
      Parameters:
      point - Point to be checked.
      Returns:
      Closest point.
    • getClosestPoint

      public Point2D getClosestPoint(Point2D point, double threshold)
      Returns the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line. If provided point belong to this line, then the same point will be returned as a result.
      Parameters:
      point - Point to be checked.
      threshold - Threshold to determine whether point is locus of line or not.
      Returns:
      Closest point.
      Throws:
      IllegalArgumentException - Raised if threshold is negative.
    • closestPoint

      public void closestPoint(Point2D point, Point2D result)
      Computes the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line. If provided point belongs to this line, then the same point will be returned as a result.
      Parameters:
      point - Point to be checked.
      result - Instance where the closest point will be stored.
    • closestPoint

      public void closestPoint(Point2D point, Point2D result, double threshold)
      Computes the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line. If provided point belongs to this line, then the same point will be returned as a result.
      Parameters:
      point - Point to be checked.
      result - Instance where the closest point will be stored.
      threshold - threshold to determine whether a point belongs to the line's locus.
      Throws:
      IllegalArgumentException - Raised if threshold is negative.
    • asArray

      public double[] asArray()
      Returns parameters of this line as an array containing [a, b, c].
      Returns:
      Array containing all the parameters that describe this line.
    • asArray

      public void asArray(double[] array)
      Stores the parameters of this line in provided array as [a, b, c].
      Parameters:
      array - Array where parameters of this line will be stored.
      Throws:
      IllegalArgumentException - Raised if provided array doesn't have length 3.
    • normalize

      public void normalize()
      Normalizes the parameters of this line to increase the accuracy of some computations.
    • isNormalized

      public boolean isNormalized()
      Returns boolean indicating whether this line has already been normalized.
      Returns:
      True if this line is normalized, false otherwise.
    • getDirectorVector

      public double[] getDirectorVector()
      Returns director vector of this line.
      Returns:
      Director vector of this line.
    • directorVector

      public void directorVector(double[] directorVector)
      Computes director vector of this line and stores the result in provided array.
      Parameters:
      directorVector - Array containing director vector.
      Throws:
      IllegalArgumentException - Raised if provided array does not have length 2.
    • getIntersection

      public Point2D getIntersection(Line2D otherLine) throws NoIntersectionException
      Computes the intersection of this line with provided line. Notice that parallel lines intersect at infinity.
      Parameters:
      otherLine - other line to be intersected with.
      Returns:
      A 2D point indicating containing the intersection.
      Throws:
      NoIntersectionException - if for numerical instabilities the intersection cannot be computed.
    • intersection

      public void intersection(Line2D otherLine, Point2D result) throws NoIntersectionException
      Computes the intersection of this line with provided line. Notice that parallel lines intersect at infinity.
      Parameters:
      otherLine - other line to be intersected with.
      result - 2D point where intersection will be stored. For greater accuracy it is recommended to use an HomogeneousPoint2D instance.
      Throws:
      NoIntersectionException - if for numerical instabilities the intersection cannot be computed.
    • dotProduct

      public double dotProduct(Line2D line)
      Computes the dot product between the parameters A, B,C of this line and the ones of provided line. This method normalizes both lines to compute dot product.
      Parameters:
      line - line to compute dot product with.
      Returns:
      dot product value.
    • equals

      public boolean equals(Line2D line, double threshold)
      Checks if the line described by this instance equals provided line up to provided threshold.
      Parameters:
      line - line to be compared to.
      threshold - threshold grade of tolerance to determine whether the lines are equal or not. It is used because due to machine precision, the values might not be exactly equal (if not provided DEFAULT_COMPARISON_THRESHOLD is used).
      Returns:
      true if current line and provided one are the same, false otherwise.
      Throws:
      IllegalArgumentException - if threshold is negative.
    • equals

      public boolean equals(Line2D line)
      Checks if the line described by this instance equals provided line up to default comparison threshold.
      Parameters:
      line - line to be compared to.
      Returns:
      true if current line and provided one are the same, false otherwise.
    • equals

      public boolean equals(Object obj)
      Checks if provided object equals current line.
      Overrides:
      equals in class Object
      Parameters:
      obj - object to compare.
      Returns:
      true if both objects are considered to be equal, false otherwise.
    • hashCode

      public int hashCode()
      Returns hash code value. This is only defined to keep the compiler happy. This method must be overridden in subclasses of this class.
      Overrides:
      hashCode in class Object
      Returns:
      Hash code
    • createCanonicalLineAtInfinity

      public static Line2D createCanonicalLineAtInfinity()
      Creates a new instance of a 2D line located at the canonical infinity. The canonical infinity corresponds to all 2D points located at infinity (i.e. m = (x,y,w = 0), hence l = (A = 0, B = 0, C = 1))
      Returns:
      a new instance of a 2D line located at the canonical infinity.
    • setAsCanonicalLineAtInfinity

      public static void setAsCanonicalLineAtInfinity(Line2D line)
      Sets provided 2D line into the canonical infinity. The canonical infinity corresponds to all 2D points located at infinity (i.e. m = (x,y,w = 0), hence l = (A = 0, B = 0, C = 1)).
      Parameters:
      line - 2D line to be set at infinity.