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 pointp1
to pointp2
.Create a copy of the 2D linel
. -
Method Summary
Modifier and TypeMethodDescriptionprivate static boolean
For the three collinear pointsp,q,x
, check if the pointx
lies on the line segmentp-q
.double
Calculate the distance between pointp
and the line.double
Calculate the squared distance between pointp
and the line.static double
Find the square distance of the pointp
from the segment, specified by the two pointss1
ands2
.static double
Find the square distance of the pointp
from the segments
.boolean
intersects
(Line2D l) Check if the line intersects the linel
.boolean
Check if this segment and the segments
intersect.static int
orientation
(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 pointp1
to pointp2
.shift
(double dx, double dy) Shift the line right bydx
and 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 pointp1
to 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 pointp1
to pointp2
. -
set
Set the line segment from point(x1,y1)
to point(x2,y2)
. -
shift
Description copied from class:Line2D
Shift the line right bydx
and up bydy
. -
distance
Description copied from class:Line2D
Calculate the distance between pointp
and the line. -
distance2
Description copied from class:Line2D
Calculate the squared distance between pointp
and the line. -
distance2
Find the square distance of the pointp
from the segments
.- Parameters:
s
- the segmentp
- the point- Returns:
- the distance of the point from the segment
-
distance2
Find the square distance of the pointp
from the segment, specified by the two pointss1
ands2
.- 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:Line2D
Check if the line intersects the linel
.- Overrides:
intersects
in classLine2D
- Parameters:
l
- the line to check- Returns:
true
if the lines intersect
-
intersects
Check if this segment and the segments
intersect.Note: code inspired by https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
- Overrides:
intersects
in classLine2D
- Parameters:
s
- the other line segment- Returns:
true
if the line segments intersect
-
orientation
Find the orientation of the ordered triplet(p, q, r)
.Return value denotes orientation:
- 0:
p, q
andr
are 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 pointx
lies on the line segmentp-q
.- Parameters:
p
- the first pointq
- the third pointx
- the second point- Returns:
true
ifq
lies on segment
-
toString
-