Class Segment


public class Segment extends AbstractGeometricPrimitive
Class representing a directed segment in 2D space. The segment is directed from its begin point to its end point. This class provides methods to determine segments' intersection, relative position of segments and relations between segments and points. Zero-length and infinite length segments are allowed although most of the methods will not work correctly with such segments.

This class uses the ideas of absolute offset and relative offset:

  • absolute offset - signed distance from the begin point of segment to some other point lying on the line of the segment
  • relative offset - absolute offset divided by the length of the segment. Thus, relative offset of segment's end point is always 1
  • Author:
    Andrey Malykhanov
    • Constructor Summary

      Constructors
      Constructor
      Description
      Segment(double beginPointX, double beginPointY, double endPointX, double endPointY)
      Creates a new instance of Segment class with the specified coordinates of begin and end points.
      Segment(Point beginPoint, Point endPoint)
      Creates a new instance of Segment class with specified begin and end points.
    • Method Summary

      Modifier and Type
      Method
      Description
      boolean
      Checks whether segment contains the specified point, i.e.
      boolean
      containsPoint(Point point, boolean includeEndPoint)
      Checks whether this segment contains the specified point, i.e.
      double
      Returns the shortest distance from segment to the specified point.
      boolean
      equals(Segment segment)
      Checks whether the other given segment is equal to this segment, i.e.
      boolean
       
      Returns the begin point of this segment.
      Returns the point on this segment closest to the specified point.
      Returns the shortest segment directed from the current segment to the specified segment, or null if the two segments intersect.
      Returns a copy of this segment.
      double
      Returns X-coordinate difference between end and begin points of segment.
      double
      Returns Y-coordinate difference between end and begin points of segment.
      Returns the end point of segment.
      Returns an IntersectionDescriptor instance which contains information (intersection point as well as absolute and relative offsets of intersected segments) about intersection with specified segment.
      getFirstIntersection(Segment segment, boolean includeEndPoints)
      Returns an IntersectionDescriptor instance which contains information (intersection point as well as absolute and relative offsets of intersected segments) about intersection of this segment with the specified segment.
      Returns first intersection point of segment and the specified another segment.
      getFirstIntersectionPoint(Segment segment, boolean includeEndPoints)
      Returns first intersection point of segment and the specified another segment.
      double
      Returns the heading of this segment.
      double
      Return the length of segment, i.e.
      getParallelSegment(double distance)
      Returns a segment which is parallel to this segment and has a specified distance to this segment.
      double
      Returns absolute offset of the specified point lying on the line of the segment, or Double.NaN if the point does not lie on the line of the segment.
      getPointOnLineByAbsoluteOffset(double absoluteOffset)
      Returns point on line of this segment by its absolute offset from the begin point of this segment.
      getPointOnLineByRelativeOffset(double relativeOffset)
      Returns point on line of this segment by its relative offset from the begin point of this segment.
      double
      Returns relative offset of the specified point lying on the line of the segment, or Double.NaN if the point does not lie on the line of the segment.
      Returns the segment with begin and end point swapped
      getRotated(double angle)
      Returns segment rotated to the specified angle around its begin point.
      getRotatedAround(double angle, Point rotationCenter)
      Returns segment rotated to the specified angle around the specified point (rotation center).
      getSegmentOnLineByAbsoluteOffsets(double beginPointAbsoluteOffset, double endPointAbsoluteOffset)
      Returns segment lying on the line of this segment located by the specified absolute offsets measured from the begin point of this segment.
      getSegmentOnLineByRelativeOffsets(double beginPointRelativeOffset, double endPointRelativeOffset)
      Returns segment lying on the line of this segment located by the specified relative offsets measured from the begin point of this segment.
      int
       
      boolean
      Checks whether this segment intersects with the specified segment.
      boolean
      intersectsWith(Segment segment, boolean includeEndPoints)
      Checks whether this segment intersects with the specified segment.
      minus(Point point)
      Returns a segment having begin and end points coordinate-wise subtracted the specified point.
      multiply(double factor)
      Returns a segment having all its coordinates multiplied by the specified factor
      plus(Point point)
      Returns a segment having begin and end points coordinate-wise added to the specified point.
      protected void
       
      protected void
       
      void
      setBeginPoint(Point beginPoint)
      Sets a new begin point to this segment.
      void
      setEndPoint(Point endPoint)
      Sets a new end point to this segment.
      Returns the string representing Java code that creates this primitive in its current state
       

      Methods inherited from class java.lang.Object

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

      • Segment

        public Segment(Point beginPoint, Point endPoint)
        Creates a new instance of Segment class with specified begin and end points.
        Parameters:
        beginPoint - - Begin point of segment.
        endPoint - - End point of segment.
      • Segment

        public Segment(double beginPointX, double beginPointY, double endPointX, double endPointY)
        Creates a new instance of Segment class with the specified coordinates of begin and end points.
        Parameters:
        beginPointX - - X-coordinate of begin point of segment
        beginPointY - - Y-coordinate of begin point of segment
        endPointX - - X-coordinate of end point of segment
        endPointY - - Y-coordinate of end point of segment
    • Method Details

      • getBeginPoint

        public Point getBeginPoint()
        Returns the begin point of this segment.
        Returns:
        begin point of segment.
      • setBeginPoint

        public void setBeginPoint(Point beginPoint)
        Sets a new begin point to this segment. Recalculates all internal information of segment instance.
        Parameters:
        beginPoint - new begin point of segment.
      • setEndPoint

        public void setEndPoint(Point endPoint)
        Sets a new end point to this segment. Recalculates all internal information of segment instance.
        Parameters:
        endPoint - - new end point of segment.
      • getEndPoint

        public Point getEndPoint()
        Returns the end point of segment.
        Returns:
        End point of segment.
      • getLength

        public double getLength()
        Return the length of segment, i.e. distance between begin and end points of the segment
        Returns:
        Length of segment.
      • getHeading

        public double getHeading()
        Returns the heading of this segment. The heading is calculated as angle between the base vector pointing downwards (in screen coordinate system) and the direction from begin to end point of this segment. Counter-clockwise direction from the base vector is considered positive heading, clockwise - negative heading. The heading always lies in range (-PI, PI].

        Returns:
        heading of this segment in radians.
        See Also:
      • getDX

        public double getDX()
        Returns X-coordinate difference between end and begin points of segment.
        Returns:
        X-coordinate difference between end and begin points of segment.
      • getDY

        public double getDY()
        Returns Y-coordinate difference between end and begin points of segment.
        Returns:
        Y-coordinate difference between end and begin points of segment.
      • recalculate

        protected void recalculate()
      • recalculateHeading

        protected void recalculateHeading()
      • containsPoint

        public boolean containsPoint(Point point, boolean includeEndPoint)
        Checks whether this segment contains the specified point, i.e. the specified point lies on segment. Allows to include or exclude end point of this segment.
        Parameters:
        point - - Specified point.
        includeEndPoint - - Include or exclude end point of segment.
        Returns:
        true if segment contains the specified point, false otherwise.
      • containsPoint

        public boolean containsPoint(Point point)
        Checks whether segment contains the specified point, i.e. the specified point lies on segment.
        Specified by:
        containsPoint in class AbstractGeometricPrimitive
        Parameters:
        point - - Specified point.
        Returns:
        true if segment contains the specified point, false otherwise.
      • intersectsWith

        public boolean intersectsWith(Segment segment)
        Checks whether this segment intersects with the specified segment.
        Parameters:
        segment - - Specified segment to check intersection with.
        Returns:
        true if segments intersect, false otherwise.
      • intersectsWith

        public boolean intersectsWith(Segment segment, boolean includeEndPoints)
        Checks whether this segment intersects with the specified segment. Allows to include or exclude end points of both segments when detecting intersection.
        Parameters:
        segment - - Specified segment to check intersection with.
        includeEndPoints - - Whether or not consider end points of segments when detecting intersection.
        Returns:
        true if segments intersect, false otherwise.
      • distanceToPoint

        public double distanceToPoint(Point point)
        Returns the shortest distance from segment to the specified point.
        Parameters:
        point - - Specified point.
        Returns:
        Shortest distance to the specified point.
      • getClosestPointTo

        public Point getClosestPointTo(Point point)
        Returns the point on this segment closest to the specified point.
        Parameters:
        point - - Specified point.
        Returns:
        Closest point on segment to the specified point.
      • getPointAbsoluteOffset

        public double getPointAbsoluteOffset(Point point)
        Returns absolute offset of the specified point lying on the line of the segment, or Double.NaN if the point does not lie on the line of the segment.
        Parameters:
        point - - Specified point.
        Returns:
        Absolute offset (can be positive, zero or negative) of the specified point, or Double.NaN if the point does not lie on the line of the segment.
      • getPointRelativeOffset

        public double getPointRelativeOffset(Point point)
        Returns relative offset of the specified point lying on the line of the segment, or Double.NaN if the point does not lie on the line of the segment.
        Parameters:
        point - - Specified point.
        Returns:
        Relative offset (can be positive, zero or negative) of the specified point, or Double.NaN if the point does not lie on the line of the segment.
      • getFirstIntersectionPoint

        public Point getFirstIntersectionPoint(Segment segment, boolean includeEndPoints)
        Returns first intersection point of segment and the specified another segment. Allows to include or exclude end points of segments when detecting intersection point. If segments do not intersect, returns null. If segments intersect at more than one point (segments lie on the same line and one segment is a part of another), returns point closest to the begin point of segment.
        Parameters:
        segment - - Specified segment.
        includeEndPoints - - Whether or not include end points of segments when detecting intersection point.
        Returns:
        Intersection point of segments or null if segments do not intersect.
      • getFirstIntersectionPoint

        public Point getFirstIntersectionPoint(Segment segment)
        Returns first intersection point of segment and the specified another segment. If segments do not intersect, returns null. If segments intersect at more than one point (segments lie on the same line and one segment is a part of another), returns point closest to the begin point of segment.
        Parameters:
        segment - - Specified segment.
        Returns:
        Intersection point of segments or null if segments do not intersect.
      • getFirstIntersection

        public IntersectionDescriptor getFirstIntersection(Segment segment, boolean includeEndPoints)
        Returns an IntersectionDescriptor instance which contains information (intersection point as well as absolute and relative offsets of intersected segments) about intersection of this segment with the specified segment. Allows to include or exclude end points of segments when detecting intersection. If segments do not intersect, returns null.
        Parameters:
        segment - - Specified segment.
        includeEndPoints - - - Whether or not include end points of segments when detecting intersection.
        Returns:
        IntersectionDescriptor with information about intersection or null if segments do not intersect
      • getFirstIntersection

        public IntersectionDescriptor getFirstIntersection(Segment segment)
        Returns an IntersectionDescriptor instance which contains information (intersection point as well as absolute and relative offsets of intersected segments) about intersection with specified segment. If segments do not intersect, returns null.
        Parameters:
        segment - - Specified segment.
        Returns:
        IntersectionDescriptor with information about intersection or null if segments do not intersect
      • getParallelSegment

        public Segment getParallelSegment(double distance)
        Returns a segment which is parallel to this segment and has a specified distance to this segment. If specified distance is positive, the returned segment will be located above and to the right of this one. Otherwise, if specified distance is negative, the returned segment will be located below and to the left of this one.
        Parameters:
        distance - - Distance of parallel segment.
        Returns:
        A parallel segment.
      • getReversed

        public Segment getReversed()
        Returns the segment with begin and end point swapped
        Returns:
        Reversed segment.
      • getRotated

        public Segment getRotated(double angle)
        Returns segment rotated to the specified angle around its begin point. In the resulting segment, begin point stays the same and end point is a result of rotation around the begin point to the specified angle.
        Parameters:
        angle - angle (in radians) to which the result segment will be rotated.
        Returns:
        rotated segment
        See Also:
      • getRotatedAround

        public Segment getRotatedAround(double angle, Point rotationCenter)
        Returns segment rotated to the specified angle around the specified point (rotation center).
        Parameters:
        angle - angle (in radians) to which the result segment will be rotated.
        rotationCenter - point specifying the center of rotation.
        Returns:
        rotated segment
        See Also:
      • getPointOnLineByRelativeOffset

        public Point getPointOnLineByRelativeOffset(double relativeOffset)
        Returns point on line of this segment by its relative offset from the begin point of this segment. The offset can be both positive and negative.
        Parameters:
        relativeOffset - specified relative offset of point.
        Returns:
        point on line of this segment at distance of |relativeOffset * length of the segment|.
      • getSegmentOnLineByRelativeOffsets

        public Segment getSegmentOnLineByRelativeOffsets(double beginPointRelativeOffset, double endPointRelativeOffset)
        Returns segment lying on the line of this segment located by the specified relative offsets measured from the begin point of this segment.

        If endPointRelativeOffset > beginPointRelativeOffset, the resulting segment will have the same heading as this segment. If endPointRelativeOffset < beginPointRelativeOffset, the resulting segment will have the opposite heading.

        Parameters:
        beginPointRelativeOffset - specified begin relative offset
        endPointRelativeOffset - specified end relative offset
        Returns:
        segment lying on the line of this segment located by the specified relative offsets
      • getPointOnLineByAbsoluteOffset

        public Point getPointOnLineByAbsoluteOffset(double absoluteOffset)
        Returns point on line of this segment by its absolute offset from the begin point of this segment. The offset can be both positive and negative.
        Parameters:
        absoluteOffset - specified absolute offset of point
        Returns:
        point on line of this segment at distance of |absoluteOffset|
      • getConnectingSegment

        public Segment getConnectingSegment(Segment otherSegment)
        Returns the shortest segment directed from the current segment to the specified segment, or null if the two segments intersect. The returned segment touches both current and the specified segments, its distance is equal to the shortest distance between the current and the specified segments
        Parameters:
        otherSegment - - Specified other segment
        Returns:
        Segment connecting the two segments, or null if the segments intersect
      • getSegmentOnLineByAbsoluteOffsets

        public Segment getSegmentOnLineByAbsoluteOffsets(double beginPointAbsoluteOffset, double endPointAbsoluteOffset)
        Returns segment lying on the line of this segment located by the specified absolute offsets measured from the begin point of this segment.

        If endPointAbsoluteOffset > beginPointAbsoluteOffset, the resulting segment will have the same heading as this segment. If endPointAbsoluteOffset < beginPointAbsoluteOffset, the resulting segment will have the opposite heading.

        Parameters:
        beginPointAbsoluteOffset - specified begin absolute offset
        endPointAbsoluteOffset - specified end absolute offset
        Returns:
        segment lying on the line of this segment located by the specified absolute offsets
      • plus

        public Segment plus(Point point)
        Returns a segment having begin and end points coordinate-wise added to the specified point. In other words, returns a segment shifted by the coordinates of the specified point
        Parameters:
        point - specified point
        Returns:
        segment shifted by the coordinates of the specified point
      • minus

        public Segment minus(Point point)
        Returns a segment having begin and end points coordinate-wise subtracted the specified point. In other words, returns a segment negatively shifted by the coordinates of the specified point
        Parameters:
        point - specified point
        Returns:
        segment negatively shifted by the coordinates of the specified point
      • multiply

        public Segment multiply(double factor)
        Returns a segment having all its coordinates multiplied by the specified factor
        Parameters:
        factor - specified factor
        Returns:
        segment having all its coordinates multiplied by the specified factor
      • toString

        public String toString()
        Overrides:
        toString in class Object
      • hashCode

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

        public boolean equals(Object obj)
        Overrides:
        equals in class Object
      • equals

        public boolean equals(Segment segment)
        Checks whether the other given segment is equal to this segment, i.e. if both begin and end points of the other segment equal correspondingly to begin and end points of this segment.

        The equality of the points is determined with respect to the default epsilon, that can be obtained by Compare.getEpsilon().

        Parameters:
        segment - other given segment
        Returns:
        true if the other given segment is equal to this segment, false otherwise
        See Also:
      • getCopy

        public Segment getCopy()
        Returns a copy of this segment.
        Returns:
        copy of this segment
      • toCodeString

        public String toCodeString()
        Description copied from class: AbstractGeometricPrimitive
        Returns the string representing Java code that creates this primitive in its current state
        Specified by:
        toCodeString in class AbstractGeometricPrimitive
        Returns:
        Java code required to create this primitive in its current state