Class Segment2D
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new empty 2D line with coordinates(0,0)and zero length.Segment2D(double x1, double y1, double x2, double y2) Create a new 2D line from point(x1,y1)to point(x2,y2).Create a new 2D line from pointp1to pointp2.Create a copy of the 2D linel. -
Method Summary
Modifier and TypeMethodDescriptionprivate static booleanFor the three collinear pointsp,q,x, check if the pointxlies on the line segmentp-q.doubleCalculate the distance between pointpand the line.doubleCalculate the squared distance between pointpand the line.static doubleFind the square distance of the pointpfrom the segment, specified by the two pointss1ands2.static doubleFind the square distance of the pointpfrom the segments.booleanintersects(Line2D l) Check if the line intersects the linel.booleanCheck if this segment and the segmentsintersect.static intorientation(Point2D p, Point2D q, Point2D r) Find the orientation of the ordered triplet(p, q, r).voidset(double x1, double y1, double x2, double y2) Set the line segment from point(x1,y1)to point(x2,y2).voidSet the line segment from pointp1to pointp2.shift(double dx, double dy) Shift the line right bydxand up bydy.toString()Methods inherited from class Line2D
above, above, distance2, getSlope, getSlope, getYIntercept, getYIntercept, initialize, intersection, intersection, intersects, parallel, set, set, setLine, vertical, y
-
Field Details
-
Constructor Details
-
Segment2D
public Segment2D()Create a new empty 2D line with coordinates(0,0)and zero length. -
Segment2D
-
Segment2D
-
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- thex-coordinate of starting pointy1- they-coordinate of starting pointx2- thex-coordinate of end pointy2- they-coordinate of end point
-
-
Method Details
-
set
-
set
public void set(double x1, double y1, double x2, double y2) Set the line segment from point(x1,y1)to point(x2,y2). -
shift
-
distance
-
distance2
-
distance2
-
distance2
Find the square distance of the pointpfrom the segment, specified by the two pointss1ands2.- Parameters:
p1- the start of the line segmentp2- the end of the line segmentp- the point- Returns:
- the distance of point from segment
-
intersects
Description copied from class:Line2DCheck if the line intersects the linel.- Overrides:
intersectsin classLine2D- Parameters:
l- the line to check- Returns:
trueif the lines intersect
-
intersects
Check if this segment and the segmentsintersect.Note: code inspired by https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
- Overrides:
intersectsin classLine2D- Parameters:
s- the other line segment- Returns:
trueif the line segments intersect
-
orientation
Find the orientation of the ordered triplet(p, q, r).Return value denotes orientation:
- 0:
p, qandrare collinear. - 1: clockwise
- -1: anti-clockwise
- Parameters:
p- the first pointq- the second pointr- the third point- Returns:
- the orientation
- 0:
-
contains
-
toString
-