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 slopemandy-interceptb.Create a copy of the 2D linel.Create a new 2D line through pointsp1andp2.Create a new 2D line withoriginanddirection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanabove(double x, double y) Check if point(x,y)lies above or below the line.booleanCheck if pointplies above or below the line.doubleCalculate the distance between pointpand the line.static doubleCalculate the squared distance between pointpand the linel.doubleCalculate the squared distance between pointpand the line.Return the intersection of two lines orDouble.NaNif no intersection.Return the intersection of the line and the segmentsorDouble.NaNif there is no intersection.booleanintersects(Line2D l) Check if the line intersects the linel.booleanCheck if the line intersects the rectangler.booleanCheck if the line intersects the line segments.booleanCheck if two lines are parallel.set(double m, double b) Set the slopemandy-interceptbof 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 pointsp1andp2.Set the line to pass through pointpin the direction of vectorv.voidSet the line to pass through the pointsp1andp2.shift(double dx, double dy) Shift the line right bydxand up bydy.toString()booleanvertical()Check if the line is vertical.doubley(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 slopemandy-interceptb.- Parameters:
m- the slopeb- they-intercept
-
Line2D
Create a new 2D line withoriginanddirection.- Parameters:
origin- the starting pointdirection- the direction
-
Line2D
Create a new 2D line through pointsp1andp2.- Parameters:
p1- the first pointp2- the second point
-
-
Method Details
-
set
Set the line to pass through pointpin the direction of vectorv.- Parameters:
point- the point on the linedirection- the direction of the line- Returns:
- the new line
-
set
Set the slopemandy-interceptbof the line.- Parameters:
m- the slopeb- they-intercept- Returns:
- the new line
-
set
Set the line to pass through pointsp1andp2.- 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 pointsp1andp2.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 bydxand 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 pointplies 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:
trueif 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:
trueif points lies above line.
-
distance
Calculate the distance between pointpand the line.- Parameters:
p- the point to find distance from line- Returns:
- the distance
-
distance2
Calculate the squared distance between pointpand the line.- Parameters:
p- the point to find squared distance from line- Returns:
- the squared distance
-
distance2
Calculate the squared distance between pointpand 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.comparereturns zero for twoNaN's, i.e. for two vertical lines and hence works as expected.- Parameters:
l- the line to compare to- Returns:
trueif the lines are parallel
-
vertical
public boolean vertical()Check if the line is vertical.- Returns:
trueif the line is vertical
-
intersects
Check if the line intersects the linel.- Parameters:
l- the line to check- Returns:
trueif the lines intersect
-
intersects
Check if the line intersects the line segments.- Parameters:
s- the line segment to check- Returns:
trueif the line segment intersects
-
intersects
Check if the line intersects the rectangler.- Parameters:
r- the rectangle to check- Returns:
trueif the line intersects the rectangle
-
intersection
Return the intersection of two lines orDouble.NaNif no intersection.- Parameters:
l- the line to check- Returns:
- the intersection point
-
intersection
Return the intersection of the line and the segmentsorDouble.NaNif there is no intersection.- Parameters:
s- the line segment to check- Returns:
- the intersection point.
-
toString
-