Class Segment2D

Object
Line2D
Segment2D

public class Segment2D extends Line2D
Utility class for deal with (finite) line segments in 2D. Note: this implementation is intended as a drop-in replacement for java.awt.geom.Line2D.Double. Methods are implemented only on an as-needed basis.
Author:
Christoph Hauert
  • Field Details

    • p1

      public Point2D p1
      The first point of the line segment.
    • p2

      public Point2D p2
      The second point of the line segment.
  • Constructor Details

    • Segment2D

      public Segment2D()
      Create a new empty 2D line with coordinates (0,0) and zero length.
    • Segment2D

      public Segment2D(Segment2D l)
      Create a copy of the 2D line l.
      Parameters:
      l - the 2D line to copy
    • Segment2D

      public Segment2D(Point2D p1, Point2D p2)
      Create a new 2D line from point p1 to point p2.
      Parameters:
      p1 - the starting point
      p2 - the end point
    • Segment2D

      public Segment2D(double x1, double y1, double x2, double y2)
      Create a new 2D line from point (x1,y1) to point (x2,y2).
      Parameters:
      x1 - the x-coordinate of starting point
      y1 - the y-coordinate of starting point
      x2 - the x-coordinate of end point
      y2 - the y-coordinate of end point
  • Method Details

    • set

      public Segment2D set(Point2D p1, Point2D p2)
      Set the line segment from point p1 to point p2.
      Overrides:
      set in class Line2D
      Parameters:
      p1 - the first point
      p2 - the second point
      Returns:
      the new line segment
    • set

      public Segment2D set(double x1, double y1, double x2, double y2)
      Set the line segment from point (x1,y1) to point (x2,y2).
      Overrides:
      set in class Line2D
      Parameters:
      x1 - the x-coordinate of starting point
      y1 - the y-coordinate of starting point
      x2 - the x-coordinate of end point
      y2 - the y-coordinate of end point
      Returns:
      the new line segment
    • shift

      public Segment2D shift(double dx, double dy)
      Description copied from class: Line2D
      Shift the line right by dx and up by dy.
      Overrides:
      shift in class Line2D
      Parameters:
      dx - the horizontal shift
      dy - the vertical shift
      Returns:
      the new line
    • distance

      public double distance(Point2D p)
      Description copied from class: Line2D
      Calculate the distance between point p and the line.
      Overrides:
      distance in class Line2D
      Parameters:
      p - the point to find distance from line
      Returns:
      the distance
    • distance2

      public double distance2(Point2D p)
      Description copied from class: Line2D
      Calculate the squared distance between point p and the line.
      Overrides:
      distance2 in class Line2D
      Parameters:
      p - the point to find squared distance from line
      Returns:
      the squared distance
    • distance2

      public static double distance2(Segment2D s, Point2D p)
      Find the square distance of the point p from the segment s.
      Parameters:
      s - the segment
      p - the point
      Returns:
      the distance of the point from the segment
    • distance2

      public static double distance2(Point2D p1, Point2D p2, Point2D p)
      Find the square distance of the point p from the segment, specified by the two points s1 and s2.
      Parameters:
      p1 - the start of the line segment
      p2 - the end of the line segment
      p - the point
      Returns:
      the distance of point from segment
    • intersects

      public boolean intersects(Line2D l)
      Description copied from class: Line2D
      Check if the line intersects the line l.
      Overrides:
      intersects in class Line2D
      Parameters:
      l - the line to check
      Returns:
      true if the lines intersect
    • intersects

      public boolean intersects(Segment2D s)
      Check if this segment and the segment s intersect.

      Note: code inspired by https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/

      Overrides:
      intersects in class Line2D
      Parameters:
      s - the other line segment
      Returns:
      true if the line segments intersect
    • orientation

      public static int orientation(Point2D p, Point2D q, Point2D r)
      Find the orientation of the ordered triplet (p, q, r).

      Return value denotes orientation:

      • 0: p, q and r are collinear.
      • 1: clockwise
      • -1: anti-clockwise
      Parameters:
      p - the first point
      q - the second point
      r - the third point
      Returns:
      the orientation
    • contains

      private static boolean contains(Point2D p, Point2D q, Point2D x)
      For the three collinear points p,q,x, check if the point x lies on the line segment p-q.
      Parameters:
      p - the first point
      q - the third point
      x - the second point
      Returns:
      true if q lies on segment
    • toString

      public String toString()
      Overrides:
      toString in class Line2D