Package org.evoludo.geom
Class Line2D
Object
Line2D
- Direct Known Subclasses:
Segment2D
Utility class for deal with straight (infinite) lines in 2D.
Note: the drop-in replacement for java.awt.geom.Line2D.Double is not Line2D but rather Segment2D for straight lines of finite length.
Note: the drop-in replacement for java.awt.geom.Line2D.Double is not Line2D but rather Segment2D for straight lines of finite length.
- Author:
- Christoph Hauert
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionLine2D()
Create a new 2D line with the slope andy
-intercept of zero (corresponding to thex
-axis).Line2D
(double m, double b) Create a new 2D line with slopem
andy
-interceptb
.Create a copy of the 2D linel
.Create a new 2D line through pointsp1
andp2
.Create a new 2D line withorigin
anddirection
. -
Method Summary
Modifier and TypeMethodDescriptionboolean
above
(double x, double y) Check if point(x,y)
lies above or below the line.boolean
Check if pointp
lies above or below the line.double
Calculate the distance between pointp
and the line.static double
Calculate the squared distance between pointp
and the linel
.double
Calculate the squared distance between pointp
and the line.Return the intersection of two lines orDouble.NaN
if no intersection.Return the intersection of the line and the segments
orDouble.NaN
if there is no intersection.boolean
intersects
(Line2D l) Check if the line intersects the linel
.boolean
Check if the line intersects the rectangler
.boolean
Check if the line intersects the line segments
.boolean
Check if two lines are parallel.set
(double m, double b) Set the slopem
andy
-interceptb
of the line.set
(double x1, double y1, double x2, double y2) Set the line to pass through points(x1,y1)
and(x2,y2)
.Set the line to pass through pointsp1
andp2
.Set the line to pass through pointp
in the direction of vectorv
.void
Set the line to pass through the pointsp1
andp2
.shift
(double dx, double dy) Shift the line right bydx
and up bydy
.toString()
boolean
vertical()
Check if the line is vertical.double
y
(double x) Return they
-value,y = m*x+b
, for given thex
-value.
-
Field Details
-
m
public double mThe slope of the straight line.Note: for vertical lines slope is
Double.NaN
-
b
public double bThey
-intercept of the straight line.Note: for vertical lines this is the
x
-intercept.
-
-
Constructor Details
-
Line2D
public Line2D()Create a new 2D line with the slope andy
-intercept of zero (corresponding to thex
-axis). -
Line2D
Create a copy of the 2D linel
.- Parameters:
l
- the 2D line to copy
-
Line2D
public Line2D(double m, double b) Create a new 2D line with slopem
andy
-interceptb
.- Parameters:
m
- the slopeb
- they
-intercept
-
Line2D
Create a new 2D line withorigin
anddirection
.- Parameters:
origin
- the starting pointdirection
- the direction
-
Line2D
Create a new 2D line through pointsp1
andp2
.- Parameters:
p1
- the first pointp2
- the second point
-
-
Method Details
-
set
Set the line to pass through pointp
in the direction of vectorv
.- Parameters:
point
- the point on the linedirection
- the direction of the line- Returns:
- the new line
-
set
Set the slopem
andy
-interceptb
of the line.- Parameters:
m
- the slopeb
- they
-intercept- Returns:
- the new line
-
set
Set the line to pass through pointsp1
andp2
.- Parameters:
p1
- the first point on the linep2
- the second point on the line- Returns:
- the new line
-
setLine
Set the line to pass through the pointsp1
andp2
.Note: for compatibility with
java.awt.geom.Line2D.Double
- Parameters:
p1
- the first point on the linep2
- the second point on the line
-
set
Set the line to pass through points(x1,y1)
and(x2,y2)
.- Parameters:
x1
- thex
-coordinate of first pointy1
- they
-coordinate of first pointx2
- thex
-coordinate of second pointy2
- they
-coordinate of second point- Returns:
- the new line
-
shift
Shift the line right bydx
and up bydy
.- Parameters:
dx
- the horizontal shiftdy
- the vertical shift- Returns:
- the new line
-
y
public double y(double x) Return they
-value,y = m*x+b
, for given thex
-value.- Parameters:
x
- thex
-value- Returns:
- the
y
-value
-
above
Check if pointp
lies above or below the line. Above means they
-coordinate of the point exceeds that of the line at the correspondingx
-coordinate.- Parameters:
p
- the point to check- Returns:
true
if points lies above line.
-
above
public boolean above(double x, double y) Check if point(x,y)
lies above or below the line. Above means they
-coordinate of the point exceeds that of the line at the correspondingx
-coordinate.- Parameters:
x
- thex
-coordinate of pointy
- they
-coordinate of point- Returns:
true
if points lies above line.
-
distance
Calculate the distance between pointp
and the line.- Parameters:
p
- the point to find distance from line- Returns:
- the distance
-
distance2
Calculate the squared distance between pointp
and the line.- Parameters:
p
- the point to find squared distance from line- Returns:
- the squared distance
-
distance2
Calculate the squared distance between pointp
and the linel
.- Parameters:
l
- the line to find distance fromp
- the point to find distance to line- Returns:
- the squared distance
-
parallel
Check if two lines are parallel.Note:
Double.compare
returns zero for twoNaN
's, i.e. for two vertical lines and hence works as expected.- Parameters:
l
- the line to compare to- Returns:
true
if the lines are parallel
-
vertical
public boolean vertical()Check if the line is vertical.- Returns:
true
if the line is vertical
-
intersects
Check if the line intersects the linel
.- Parameters:
l
- the line to check- Returns:
true
if the lines intersect
-
intersects
Check if the line intersects the line segments
.- Parameters:
s
- the line segment to check- Returns:
true
if the line segment intersects
-
intersects
Check if the line intersects the rectangler
.- Parameters:
r
- the rectangle to check- Returns:
true
if the line intersects the rectangle
-
intersection
Return the intersection of two lines orDouble.NaN
if no intersection.- Parameters:
l
- the line to check- Returns:
- the intersection point
-
intersection
Return the intersection of the line and the segments
orDouble.NaN
if there is no intersection.- Parameters:
s
- the line segment to check- Returns:
- the intersection point.
-
toString
-