Class Line

  • All Implemented Interfaces:
    Embedding<Vector2D,​Vector1D>, EmbeddingHyperplane<Vector2D,​Vector1D>, Hyperplane<Vector2D>

    public final class Line
    extends AbstractHyperplane<Vector2D>
    implements EmbeddingHyperplane<Vector2D,​Vector1D>
    This class represents an oriented line in the 2D plane.

    An oriented line can be defined either by extending a line segment between two points past these points, by specifying a point and a direction, or by specifying a point and an angle relative to the x-axis.

    Since the line oriented, the two half planes on its sides are unambiguously identified as the left half plane and the right half plane. This can be used to identify the interior and the exterior in a simple way when a line is used to define a portion of a polygon boundary.

    A line can also be used to completely define a reference frame in the plane. It is sufficient to select one specific point in the line (the orthogonal projection of the original reference frame on the line) and to use the unit vector in the line direction (see getDirection() and the orthogonal vector oriented from the left half plane to the right half plane (see getOffsetDirection(). We define two coordinates by the process, the abscissa along the line, and the offset across the line. All points of the plane are uniquely identified by these two coordinates. The line is the set of points at zero offset, the left half plane is the set of points with negative offsets and the right half plane is the set of points with positive offsets.

    See Also:
    Lines
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Line.SubspaceTransform
      Class containing a transformed line instance along with a subspace (1D) transform.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Vector2D.Unit direction
      The direction of the line as a normalized vector.
      private double originOffset
      The distance between the origin and the line.
      (package private) static java.lang.String TO_STRING_FORMAT
      Format string for creating line string representations.
    • Constructor Summary

      Constructors 
      Constructor Description
      Line​(Vector2D.Unit direction, double originOffset, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
      Simple constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double abscissa​(Vector2D point)
      Get the abscissa of the given point on the line.
      double angle​(Line other)
      Compute the angle in radians between this instance's direction and the direction of the given line.
      boolean contains​(Line line)
      Check if this instance completely contains the other line.
      boolean contains​(Vector2D p)
      Check if the line contains a point.
      double distance​(Line line)
      Compute the shortest distance between this instance and the given line.
      double distance​(Vector2D p)
      Compute the distance between the instance and a point.
      boolean eq​(Line other, org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
      Return true if this instance should be considered equivalent to the argument, using the given precision context for comparison.
      boolean equals​(java.lang.Object obj)
      double getAngle()
      Get the angle of the line in radians with respect to the abscissa (+x) axis.
      Vector2D.Unit getDirection()
      Get the direction of the line.
      Vector2D getOffsetDirection()
      Get the offset direction of the line.
      Vector2D getOrigin()
      Get the line origin point.
      double getOriginOffset()
      Get the signed distance from the origin of the 2D space to the closest point on the line.
      int hashCode()
      Vector2D intersection​(Line other)
      Get the intersection point of the instance and another line.
      boolean isParallel​(Line line)
      Check if the instance is parallel to another line.
      double offset​(Line line)
      Get the offset (oriented distance) of the given line relative to this instance.
      double offset​(Vector2D point)
      Get the offset (oriented distance) of a point with respect to this instance.
      Vector2D pointAt​(double abscissa, double offset)
      Get one point from the plane, relative to the coordinate system of the line.
      Vector2D project​(Vector2D point)
      Project a point onto this instance.
      Ray rayFrom​(double startLocation)
      Create a new ray instance that starts at the given 1D location and continues in the direction of the line to infinity.
      Ray rayFrom​(Vector2D startPoint)
      Create a new ray instance that starts at the projection of the given point and continues in the direction of the line to infinity.
      Line reverse()
      Return a hyperplane that has the opposite orientation as this instance.
      ReverseRay reverseRayTo​(double endLocation)
      Create a new convex line subset that starts at infinity and continues along the line up to the given 1D location.
      ReverseRay reverseRayTo​(Vector2D endPoint)
      Create a new convex line subset that starts at infinity and continues along the line up to the projection of the given end point.
      Segment segment​(double a, double b)
      Create a new line segment from the given 1D interval.
      Segment segment​(Vector2D a, Vector2D b)
      Create a new line segment from two points.
      boolean similarOrientation​(Hyperplane<Vector2D> other)
      Return true if this instance has a similar orientation to the given hyperplane, meaning that they point in generally the same direction.
      LineConvexSubset span()
      Return a HyperplaneConvexSubset spanning this entire hyperplane.
      Line.SubspaceTransform subspaceTransform​(Transform<Vector2D> transform)
      Get an object containing the current line transformed by the argument along with a 1D transform that can be applied to subspace points.
      Vector2D toSpace​(double abscissa)
      Convert the given abscissa value (1D location on the line) into a 2D point.
      Vector2D toSpace​(Vector1D point)
      Transform a subspace point into a space point.
      java.lang.String toString()
      Vector1D toSubspace​(Vector2D point)
      Transform a space point into a subspace point.
      Line transform​(Transform<Vector2D> transform)
      Transform this instance using the given Transform.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.apache.commons.geometry.core.partitioning.Hyperplane

        classify
    • Field Detail

      • TO_STRING_FORMAT

        static final java.lang.String TO_STRING_FORMAT
        Format string for creating line string representations.
        See Also:
        Constant Field Values
      • direction

        private final Vector2D.Unit direction
        The direction of the line as a normalized vector.
      • originOffset

        private final double originOffset
        The distance between the origin and the line.
    • Constructor Detail

      • Line

        Line​(Vector2D.Unit direction,
             double originOffset,
             org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
        Simple constructor.
        Parameters:
        direction - The direction of the line.
        originOffset - The signed distance between the line and the origin.
        precision - Precision context used to compare floating point numbers.
    • Method Detail

      • getAngle

        public double getAngle()
        Get the angle of the line in radians with respect to the abscissa (+x) axis. The returned angle is in the range [0, 2pi).
        Returns:
        the angle of the line with respect to the abscissa (+x) axis in the range [0, 2pi)
      • getDirection

        public Vector2D.Unit getDirection()
        Get the direction of the line.
        Returns:
        the direction of the line
      • getOffsetDirection

        public Vector2D getOffsetDirection()
        Get the offset direction of the line. This vector is perpendicular to the line and points in the direction of positive offset values, meaning that it points from the left side of the line to the right when one is looking along the line direction.
        Returns:
        the offset direction of the line.
      • getOrigin

        public Vector2D getOrigin()
        Get the line origin point. This is the projection of the 2D origin onto the line and also serves as the origin for the 1D embedded subspace.
        Returns:
        the origin point of the line
      • getOriginOffset

        public double getOriginOffset()
        Get the signed distance from the origin of the 2D space to the closest point on the line.
        Returns:
        the signed distance from the origin to the line
      • reverse

        public Line reverse()
        Return a hyperplane that has the opposite orientation as this instance. That is, the plus side of this instance is the minus side of the returned instance and vice versa.
        Specified by:
        reverse in interface Hyperplane<Vector2D>
        Returns:
        a hyperplane with the opposite orientation
      • subspaceTransform

        public Line.SubspaceTransform subspaceTransform​(Transform<Vector2D> transform)
        Get an object containing the current line transformed by the argument along with a 1D transform that can be applied to subspace points. The subspace transform transforms subspace points such that their 2D location in the transformed line is the same as their 2D location in the original line after the 2D transform is applied. For example, consider the code below:
              SubspaceTransform st = line.subspaceTransform(transform);
        
              Vector1D subPt = Vector1D.of(1);
        
              Vector2D a = transform.apply(line.toSpace(subPt)); // transform in 2D space
              Vector2D b = st.getLine().toSpace(st.getTransform().apply(subPt)); // transform in 1D space
         
        At the end of execution, the points a (which was transformed using the original 2D transform) and b (which was transformed in 1D using the subspace transform) are equivalent.
        Parameters:
        transform - the transform to apply to this instance
        Returns:
        an object containing the transformed line along with a transform that can be applied to subspace points
        See Also:
        transform(Transform)
      • segment

        public Segment segment​(double a,
                               double b)
        Create a new line segment from the given 1D interval. The returned line segment consists of all points between the two locations, regardless of the order the arguments are given.
        Parameters:
        a - first 1D location for the interval
        b - second 1D location for the interval
        Returns:
        a new line segment on this line
        Throws:
        java.lang.IllegalArgumentException - if either of the locations is NaN or infinite
        See Also:
        Lines.segmentFromLocations(Line, double, double)
      • segment

        public Segment segment​(Vector2D a,
                               Vector2D b)
        Create a new line segment from two points. The returned segment represents all points on this line between the projected locations of a and b. The points may be given in any order.
        Parameters:
        a - first point
        b - second point
        Returns:
        a new line segment on this line
        Throws:
        java.lang.IllegalArgumentException - if either point contains NaN or infinite coordinate values
        See Also:
        Lines.segmentFromPoints(Line, Vector2D, Vector2D)
      • reverseRayTo

        public ReverseRay reverseRayTo​(Vector2D endPoint)
        Create a new convex line subset that starts at infinity and continues along the line up to the projection of the given end point.
        Parameters:
        endPoint - point defining the end point of the line subset; the end point is equal to the projection of this point onto the line
        Returns:
        a new, half-open line subset that ends at the given point
        Throws:
        java.lang.IllegalArgumentException - if any coordinate in endPoint is NaN or infinite
        See Also:
        Lines.reverseRayFromPoint(Line, Vector2D)
      • reverseRayTo

        public ReverseRay reverseRayTo​(double endLocation)
        Create a new convex line subset that starts at infinity and continues along the line up to the given 1D location.
        Parameters:
        endLocation - the 1D location of the end of the half-line
        Returns:
        a new, half-open line subset that ends at the given 1D location
        Throws:
        java.lang.IllegalArgumentException - if endLocation is NaN or infinite
        See Also:
        Lines.reverseRayFromLocation(Line, double)
      • rayFrom

        public Ray rayFrom​(Vector2D startPoint)
        Create a new ray instance that starts at the projection of the given point and continues in the direction of the line to infinity.
        Parameters:
        startPoint - point defining the start point of the ray; the start point is equal to the projection of this point onto the line
        Returns:
        a ray starting at the projected point and extending along this line to infinity
        Throws:
        java.lang.IllegalArgumentException - if any coordinate in startPoint is NaN or infinite
        See Also:
        Lines.rayFromPoint(Line, Vector2D)
      • rayFrom

        public Ray rayFrom​(double startLocation)
        Create a new ray instance that starts at the given 1D location and continues in the direction of the line to infinity.
        Parameters:
        startLocation - 1D location defining the start point of the ray
        Returns:
        a ray starting at the given 1D location and extending along this line to infinity
        Throws:
        java.lang.IllegalArgumentException - if startLocation is NaN or infinite
        See Also:
        Lines.rayFromLocation(Line, double)
      • abscissa

        public double abscissa​(Vector2D point)
        Get the abscissa of the given point on the line. The abscissa represents the distance the projection of the point on the line is from the line's origin point (the point on the line closest to the origin of the 2D space). Abscissa values increase in the direction of the line. This method is exactly equivalent to toSubspace(Vector2D) except that this method returns a double instead of a Vector1D.
        Parameters:
        point - point to compute the abscissa for
        Returns:
        abscissa value of the point
        See Also:
        toSubspace(Vector2D)
      • toSpace

        public Vector2D toSpace​(double abscissa)
        Convert the given abscissa value (1D location on the line) into a 2D point.
        Parameters:
        abscissa - value to convert
        Returns:
        2D point corresponding to the line abscissa value
      • intersection

        public Vector2D intersection​(Line other)
        Get the intersection point of the instance and another line.
        Parameters:
        other - other line
        Returns:
        intersection point of the instance and the other line or null if there is no unique intersection point (ie, the lines are parallel or coincident)
      • angle

        public double angle​(Line other)
        Compute the angle in radians between this instance's direction and the direction of the given line. The return value is in the range [-pi, +pi). This method always returns a value, even for parallel or coincident lines.
        Parameters:
        other - other line
        Returns:
        the angle required to rotate this line to point in the direction of the given line
      • project

        public Vector2D project​(Vector2D point)
        Project a point onto this instance.
        Specified by:
        project in interface Hyperplane<Vector2D>
        Parameters:
        point - the point to project
        Returns:
        the projection of the point onto this instance. The returned point lies on the hyperplane.
      • offset

        public double offset​(Vector2D point)
        Get the offset (oriented distance) of a point with respect to this instance. Points with an offset of zero lie on the hyperplane itself.
        Specified by:
        offset in interface Hyperplane<Vector2D>
        Parameters:
        point - the point to compute the offset for
        Returns:
        the offset of the point
      • offset

        public double offset​(Line line)
        Get the offset (oriented distance) of the given line relative to this instance. Since an infinite number of distances can be calculated between points on two different lines, this method returns the value closest to zero. For intersecting lines, this will simply be zero. For parallel lines, this will be the perpendicular distance between the two lines, as a signed value.

        The sign of the returned offset indicates the side of the line that the argument lies on. The offset is positive if the line lies on the right side of the instance and negative if the line lies on the left side of the instance.

        Parameters:
        line - line to check
        Returns:
        offset of the line
        See Also:
        distance(Line)
      • similarOrientation

        public boolean similarOrientation​(Hyperplane<Vector2D> other)
        Return true if this instance has a similar orientation to the given hyperplane, meaning that they point in generally the same direction. This method is not used to determine exact equality of hyperplanes, but rather to determine whether two hyperplanes that contain the same points are parallel (point in the same direction) or anti-parallel (point in opposite directions).
        Specified by:
        similarOrientation in interface Hyperplane<Vector2D>
        Parameters:
        other - the hyperplane to compare with
        Returns:
        true if the hyperplanes point in generally the same direction and could possibly be parallel
      • pointAt

        public Vector2D pointAt​(double abscissa,
                                double offset)
        Get one point from the plane, relative to the coordinate system of the line. Note that the direction of increasing offsets points to the right of the line. This means that if one pictures the line (abscissa) direction as equivalent to the +x-axis, the offset direction will point along the -y axis.
        Parameters:
        abscissa - desired abscissa (distance along the line) for the point
        offset - desired offset (distance perpendicular to the line) for the point
        Returns:
        one point in the plane, with given abscissa and offset relative to the line
      • contains

        public boolean contains​(Line line)
        Check if this instance completely contains the other line. This will be true if the two instances represent the same line, with perhaps different directions.
        Parameters:
        line - line to check
        Returns:
        true if this instance contains all points in the given line
      • distance

        public double distance​(Vector2D p)
        Compute the distance between the instance and a point.

        This is a shortcut for invoking Math.abs(getOffset(p)), and provides consistency with what is in the org.apache.commons.geometry.euclidean.threed.Line class.

        Parameters:
        p - to check
        Returns:
        distance between the instance and the point
      • distance

        public double distance​(Line line)
        Compute the shortest distance between this instance and the given line. This value will simply be zero for intersecting lines.
        Parameters:
        line - line to compute the closest distance to
        Returns:
        the shortest distance between this instance and the given line
        See Also:
        offset(Line)
      • isParallel

        public boolean isParallel​(Line line)
        Check if the instance is parallel to another line.
        Parameters:
        line - other line to check
        Returns:
        true if the instance is parallel to the other line (they can have either the same or opposite orientations)
      • eq

        public boolean eq​(Line other,
                          org.apache.commons.numbers.core.Precision.DoubleEquivalence precision)
        Return true if this instance should be considered equivalent to the argument, using the given precision context for comparison. Instances are considered equivalent if they have equivalent origin points and make similar angles with the x-axis.
        Parameters:
        other - the point to compare with
        precision - precision context to use for the comparison
        Returns:
        true if this instance should be considered equivalent to the argument
        See Also:
        Vector2D.eq(Vector2D, Precision.DoubleEquivalence)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object