Class LineSegment2d

    • Field Detail

      • startX

        public final double startX
        The start x-coordinate.
      • startY

        public final double startY
        The start y-coordinate.
      • endX

        public final double endX
        The end x-coordinate.
      • endY

        public final double endY
        The end y-coordinate.
    • Constructor Detail

      • LineSegment2d

        public LineSegment2d​(double startX,
                             double startY,
                             double endX,
                             double endY)
                      throws DrawRuntimeException
        Construct a new LineSegment2d start four coordinates.
        Parameters:
        startX - double; the x-coordinate of the start point
        startY - double; the y-coordinate of the start point
        endX - double; the x-coordinate of the end point
        endY - double; the y-coordinate of the end point
        Throws:
        DrawRuntimeException - when (startX,startY) is equals end (endX,endY)
      • LineSegment2d

        public LineSegment2d​(Point2d start,
                             double endX,
                             double endY)
                      throws NullPointerException,
                             DrawRuntimeException
        Construct a new LineSegment2d start a Point2d and two coordinates.
        Parameters:
        start - Point2d; the start point
        endX - double; the x-coordinate of the end point
        endY - double; the y-coordinate of the end point
        Throws:
        NullPointerException - when start is null
        DrawRuntimeException - when start has the exact coordinates endX, endY
      • LineSegment2d

        public LineSegment2d​(double startX,
                             double startY,
                             Point2d end)
                      throws NullPointerException,
                             DrawRuntimeException
        Construct a new LineSegment2d start two coordinates and a Point2d.
        Parameters:
        startX - double; the x-coordinate of the start point
        startY - double; the y-coordinate of the start point
        end - Point2d; the end point
        Throws:
        NullPointerException - when end is null
        DrawRuntimeException - when end has the exact coordinates startX, startY
    • Method Detail

      • getLength

        public double getLength()
        Get the length (distance from start point to end point) of this LineSegment.
        Specified by:
        getLength in interface LineSegment<Point2d,​Ray2d,​Space2d>
        Returns:
        double; (distance from start point to end point) of this LineSegment
      • getPoints

        public Iterator<? extends Point2d> getPoints()
        Retrieve, or generate all points that make up the object.
        Specified by:
        getPoints in interface Drawable<Point2d,​Space2d>
        Returns:
        Iterable<Point2d>; an iterator that generates all points that make up the object
      • size

        public int size()
        Retrieve the number of points that make up the object.
        Specified by:
        size in interface Drawable<Point2d,​Space2d>
        Returns:
        int; the number of points that make up the object
      • getBounds

        public Bounds2d getBounds()
        Retrieve the bounding rectangle of the object.
        Specified by:
        getBounds in interface Drawable2d
        Returns:
        Bounds2d; the bounding box of the object
      • closestPointOnSegment

        public Point2d closestPointOnSegment​(Point2d point)
        Project a Point on this LineSegment. If the the projected points lies outside the line segment, the nearest end point of the line segment is returned. Otherwise the returned point lies between the end points of the line segment.
        Adapted from example code provided by Paul Bourke.
        Specified by:
        closestPointOnSegment in interface LineSegment<Point2d,​Ray2d,​Space2d>
        Parameters:
        point - P; the point to project onto the segment
        Returns:
        P; either the start point, or the end point of the segment or a Point2d that lies somewhere in between those two.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object