Class Line2D

  • All Implemented Interfaces:
    java.lang.Cloneable, Shape
    Direct Known Subclasses:
    Line2D.Double, Line2D.Float

    public abstract class Line2D
    extends java.lang.Object
    implements Shape, java.lang.Cloneable
    An abstract class representing a line in two dimensional space
    Author:
    Lawrie Griffiths
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Line2D.Double
      A line in 2D space using float coordinates
      static class  Line2D.Float
      A line in 2D space using float coordinates
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Line2D()
      This is an abstract class that cannot be instantiated: use Line2D.Float or Line2D.Double.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object clone()  
      boolean contains​(double x, double y)
      Test if the shape contains the point (x,y)
      boolean contains​(double x, double y, double w, double h)
      Test if the shape contains the rectangle with top left at (x,y), width w and height h.
      boolean contains​(Point2D p)
      Test if the shape contains the Point2D
      boolean contains​(Rectangle2D r)
      Test if the shape contains the Rectangle2D
      RectangleInt32 getBounds()
      Get the bounding Rectangle for the shape
      abstract Point2D getP1()
      Get the start point of the line as a Point2D
      abstract Point2D getP2()
      Get the end point of the line as a Point2D
      abstract double getX1()
      Get the x coordinate of the start of the line
      abstract double getX2()
      Get the x coordinate of the end of the line
      abstract double getY1()
      Get the y coordinate of the start of the line
      abstract double getY2()
      Get the y coordinate of the end of the line
      boolean intersects​(double x, double y, double w, double h)
      Test if the shape intersects the rectangle with top left at (x,y), width w and height h.
      boolean intersects​(Rectangle2D r)
      Test if the shape intersects the Rectangle2D r
      boolean intersectsLine​(double x1, double y1, double x2, double y2)
      Test if this line intersects a given line
      boolean intersectsLine​(Line2D l)
      Tests if this line intersects a given line
      static boolean linesIntersect​(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
      Test if one line intersects another line
      double ptSegDist​(double px, double py)
      Measures the shortest distance from the reference point to a point on this line segment.
      static double ptSegDist​(double x1, double y1, double x2, double y2, double px, double py)
      Measures the shortest distance from the reference point to a point on the line segment.
      double ptSegDist​(Point2D p)
      Measures the shortest distance from the reference point to a point on this line segment.
      double ptSegDistSq​(double px, double py)
      Measures the square of the shortest distance from the reference point to a point on this line segment.
      static double ptSegDistSq​(double x1, double y1, double x2, double y2, double px, double py)
      Measures the square of the shortest distance from the reference point to a point on the line segment.
      double ptSegDistSq​(Point2D p)
      Measures the square of the shortest distance from the reference point to a point on this line segment.
      int relativeCCW​(double px, double py)
      Returns an indicator of where the specified point lies with respect to the line.
      static int relativeCCW​(double x1, double y1, double x2, double y2, double px, double py)
      Returns an indicator of where the specified point lies with respect to the line
      int relativeCCW​(Point2D p)
      Returns an indicator of where the specified point lies with respect to the line
      abstract void setLine​(double x1, double y1, double x2, double y2)
      Sets the end points of the line using double coordinates.
      void setLine​(Line2D line)
      Set the end points of a line to the same as a given line
      void setLine​(Point2D p1, Point2D p2)
      Sets the end points of the line from a given start and end point
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Line2D

        protected Line2D()
        This is an abstract class that cannot be instantiated: use Line2D.Float or Line2D.Double.
    • Method Detail

      • getX1

        public abstract double getX1()
        Get the x coordinate of the start of the line
        Returns:
        the x coordinate as a double
      • getY1

        public abstract double getY1()
        Get the y coordinate of the start of the line
        Returns:
        the y coordinate as a double
      • getP1

        public abstract Point2D getP1()
        Get the start point of the line as a Point2D
        Returns:
        the Point2D
      • getX2

        public abstract double getX2()
        Get the x coordinate of the end of the line
        Returns:
        the x coordinate as a double
      • getY2

        public abstract double getY2()
        Get the y coordinate of the end of the line
        Returns:
        the y coordinate as a double
      • getP2

        public abstract Point2D getP2()
        Get the end point of the line as a Point2D
        Returns:
        the Point2D
      • setLine

        public abstract void setLine​(double x1,
                                     double y1,
                                     double x2,
                                     double y2)
        Sets the end points of the line using double coordinates.
        Parameters:
        x1 - the x coordinate of the start point
        y1 - the y coordinate of the start point
        x2 - the x coordinate of the end point
        y2 - the y coordinate of the end point
      • setLine

        public void setLine​(Point2D p1,
                            Point2D p2)
        Sets the end points of the line from a given start and end point
        Parameters:
        p1 - the start point
        p2 - the end point
      • setLine

        public void setLine​(Line2D line)
        Set the end points of a line to the same as a given line
        Parameters:
        line - the given line
      • contains

        public boolean contains​(double x,
                                double y)
        Description copied from interface: Shape
        Test if the shape contains the point (x,y)
        Specified by:
        contains in interface Shape
        Parameters:
        x - the x co-ordinate of the point
        y - the y co-ordinate of the point
        Returns:
        true iff the shape contains the point
      • contains

        public boolean contains​(Point2D p)
        Description copied from interface: Shape
        Test if the shape contains the Point2D
        Specified by:
        contains in interface Shape
        Parameters:
        p - the Point2D
        Returns:
        true iff the shape contains the point
      • contains

        public boolean contains​(double x,
                                double y,
                                double w,
                                double h)
        Description copied from interface: Shape
        Test if the shape contains the rectangle with top left at (x,y), width w and height h.
        Specified by:
        contains in interface Shape
        Parameters:
        x - the x-coordinate of the top left point of the rectangle
        y - the y-coordinate of the top left point of the rectangle
        w - the width of the rectangle
        h - the height of the rectangle
        Returns:
        true iff the shape contains the rectangle
      • contains

        public boolean contains​(Rectangle2D r)
        Description copied from interface: Shape
        Test if the shape contains the Rectangle2D
        Specified by:
        contains in interface Shape
        Parameters:
        r - the Rectangle2D
        Returns:
        true iff the shape contains the Rectangle2D
      • intersects

        public boolean intersects​(double x,
                                  double y,
                                  double w,
                                  double h)
        Description copied from interface: Shape
        Test if the shape intersects the rectangle with top left at (x,y), width w and height h.
        Specified by:
        intersects in interface Shape
        Parameters:
        x - the x-coordinate of the top left point of the rectangle
        y - the y-coordinate of the top left point of the rectangle
        w - the width of the rectangle
        h - the height of the rectangle
        Returns:
        true iff the shape intersects the rectangle
      • intersectsLine

        public boolean intersectsLine​(double x1,
                                      double y1,
                                      double x2,
                                      double y2)
        Test if this line intersects a given line
        Parameters:
        x1 - the x coordinate of the start of the given line
        y1 - the y coordinate of the start of the given line
        x2 - the x coordinate of the end of the given line
        y2 - the y coordinate of the end of the given line
        Returns:
        true iff the lines intersect
      • intersectsLine

        public boolean intersectsLine​(Line2D l)
        Tests if this line intersects a given line
        Parameters:
        l - the given line
        Returns:
        true iff the lines intersect
      • linesIntersect

        public static boolean linesIntersect​(double x1,
                                             double y1,
                                             double x2,
                                             double y2,
                                             double x3,
                                             double y3,
                                             double x4,
                                             double y4)
        Test if one line intersects another line
        Parameters:
        x1 - the x coordinate of the start of the first line
        y1 - the y coordinate of the start of the first line
        x2 - the x coordinate of the end of the first line
        y2 - the y coordinate of the end of the first line
        x3 - the x coordinate of the start of the second line
        y3 - the y coordinate of the start of the second line
        x4 - the x coordinate of the end of the second line
        y4 - the y coordinate of the end of the second line
        Returns:
        true iff the lines intersect
      • getBounds

        public RectangleInt32 getBounds()
        Description copied from interface: Shape
        Get the bounding Rectangle for the shape
        Specified by:
        getBounds in interface Shape
        Returns:
        the bounding Rectangle
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.lang.Object
      • relativeCCW

        public static int relativeCCW​(double x1,
                                      double y1,
                                      double x2,
                                      double y2,
                                      double px,
                                      double py)
        Returns an indicator of where the specified point lies with respect to the line
        Parameters:
        x1 - the x coordinate of the start of the line
        y1 - the y coordinate of the start of the line
        x2 - the x coordinate of the end of the line
        y2 - the y coordinate of the end of the line
        px - the x coordinate of the specified point
        py - the y coordinate of the specified point
        Returns:
        0 iff the point is on the line else 1 or -1 depending on whether the point in to the left or ahead of the line, or to the right or behind the line segment
      • relativeCCW

        public int relativeCCW​(Point2D p)
        Returns an indicator of where the specified point lies with respect to the line
        Parameters:
        p - the specified point
        Returns:
        0 iff the point is on the line else 1 or -1 depending on whether the point in to the left or ahead of the line, or to the right or behind the line segment
      • relativeCCW

        public int relativeCCW​(double px,
                               double py)
        Returns an indicator of where the specified point lies with respect to the line.
        Parameters:
        px - the x coordinate of the specified point
        py - the y coordinate of the specified point
        Returns:
        0 iff the point is on the line else 1 or -1 depending on whether the point in to the left or ahead of the line, or to the right or behind the line segment
      • intersects

        public boolean intersects​(Rectangle2D r)
        Description copied from interface: Shape
        Test if the shape intersects the Rectangle2D r
        Specified by:
        intersects in interface Shape
        Parameters:
        r - the Recangle2D
        Returns:
        true iff the shape intersects the Rectangle2D
      • ptSegDistSq

        public static double ptSegDistSq​(double x1,
                                         double y1,
                                         double x2,
                                         double y2,
                                         double px,
                                         double py)
        Measures the square of the shortest distance from the reference point to a point on the line segment. If the point is on the segment, the result will be 0.
        Parameters:
        x1 - the first x coordinate of the segment
        y1 - the first y coordinate of the segment
        x2 - the second x coordinate of the segment
        y2 - the second y coordinate of the segment
        px - the x coordinate of the point
        py - the y coordinate of the point
        Returns:
        the square of the distance from the point to the segment
      • ptSegDist

        public static double ptSegDist​(double x1,
                                       double y1,
                                       double x2,
                                       double y2,
                                       double px,
                                       double py)
        Measures the shortest distance from the reference point to a point on the line segment. If the point is on the segment, the result will be 0.
        Parameters:
        x1 - the first x coordinate of the segment
        y1 - the first y coordinate of the segment
        x2 - the second x coordinate of the segment
        y2 - the second y coordinate of the segment
        px - the x coordinate of the point
        py - the y coordinate of the point
        Returns:
        the distance from the point to the segment
      • ptSegDistSq

        public double ptSegDistSq​(double px,
                                  double py)
        Measures the square of the shortest distance from the reference point to a point on this line segment. If the point is on the segment, the result will be 0.
        Parameters:
        px - the x coordinate of the point
        py - the y coordinate of the point
        Returns:
        the square of the distance from the point to the segment
        See Also:
        ptSegDistSq(double, double, double, double, double, double)
      • ptSegDistSq

        public double ptSegDistSq​(Point2D p)
        Measures the square of the shortest distance from the reference point to a point on this line segment. If the point is on the segment, the result will be 0.
        Parameters:
        p - the point
        Returns:
        the square of the distance from the point to the segment
        Throws:
        java.lang.NullPointerException - if p is null
        See Also:
        ptSegDistSq(double, double, double, double, double, double)
      • ptSegDist

        public double ptSegDist​(double px,
                                double py)
        Measures the shortest distance from the reference point to a point on this line segment. If the point is on the segment, the result will be 0.
        Parameters:
        px - the x coordinate of the point
        py - the y coordinate of the point
        Returns:
        the distance from the point to the segment
        See Also:
        ptSegDist(double, double, double, double, double, double)
      • ptSegDist

        public double ptSegDist​(Point2D p)
        Measures the shortest distance from the reference point to a point on this line segment. If the point is on the segment, the result will be 0.
        Parameters:
        p - the point
        Returns:
        the distance from the point to the segment
        Throws:
        java.lang.NullPointerException - if p is null
        See Also:
        ptSegDist(double, double, double, double, double, double)