Package org.evoludo.geom
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
FieldsModifier and TypeFieldDescriptionThe first point of the line segment.The second point of the line segment. -
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).set(double x1, double y1, double x2, double y2) Set the line segment from point(x1,y1)to point(x2,y2).Set 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, intersection, intersection, intersects, parallel, set, set, setLine, vertical, y
-
Field Details
-
p1
The first point of the line segment. -
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
Create a copy of the 2D linel.- Parameters:
l- the 2D line to copy
-
Segment2D
Create a new 2D line from pointp1to pointp2.- Parameters:
p1- the starting pointp2- 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- thex-coordinate of starting pointy1- they-coordinate of starting pointx2- thex-coordinate of end pointy2- they-coordinate of end point
-
-
Method Details
-
set
Set the line segment from pointp1to pointp2. -
set
Set the line segment from point(x1,y1)to point(x2,y2). -
shift
Description copied from class:Line2DShift the line right bydxand up bydy. -
distance
Description copied from class:Line2DCalculate the distance between pointpand the line. -
distance2
Description copied from class:Line2DCalculate the squared distance between pointpand the line. -
distance2
Find the square distance of the pointpfrom the segments.- Parameters:
s- the segmentp- the point- Returns:
- the distance of the point from the segment
-
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
For the three collinear pointsp,q,x, check if the pointxlies on the line segmentp-q.- Parameters:
p- the first pointq- the third pointx- the second point- Returns:
trueifqlies on segment
-
toString
-