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
Constructors -
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.static doublegetSlope(double x1, double y1, double x2, double y2) Calculate the slope of a line running through points(x1,y1)and(x2,y2).static doubleCalculate the slope of a line running through pointsp1andp2.static doublegetYIntercept(double x1, double y1, double x2, double y2) Calculate the slope of a line running through pointsp1andp2.static doublegetYIntercept(Point2D p1, Point2D p2) Calculate the slope of a line running through pointsp1andp2.protected static voidinitialize(Line2D line, double m, double b) Initialize the line with the given slope and y-intercept.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.voidset(double m, double b) Set the slopemandy-interceptbof the line.voidset(double x1, double y1, double x2, double y2) Set the slopemand y-interceptbof the line passing through points(x1,y1)and(x2,y2).voidSet the line to pass through pointsp1andp2.voidSet 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
double mThe slope of the straight line.Note: for vertical lines slope is
Double.POSITIVE_INFINITYorDouble.POSITIVE_INFINITY. -
b
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
-
Line2D
public Line2D(double m, double b) Create a new 2D line with slopemandy-interceptb.- Parameters:
m- the slopeb- they-intercept
-
Line2D
-
-
Method Details
-
initialize
Initialize the line with the given slope and y-intercept. Helper method to preventthis-escapewarnings.- Parameters:
line- the line to initializem- the slopeb- the y-intercept
-
getSlope
-
getSlope
public static double getSlope(double x1, double y1, double x2, double y2) Calculate the slope of a line running 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 slope of the line
-
getYIntercept
-
getYIntercept
public static double getYIntercept(double x1, double y1, double x2, double y2) Calculate the slope of a line running through pointsp1andp2.- Parameters:
x1- thex-coordinate of first pointy1- they-coordinate of first pointx2- thex-coordinate of second pointy2- they-coordinate of second point- Returns:
- the
y-intercept of the line
-
set
-
set
public void set(double m, double b) Set the slopemandy-interceptbof the line.- Parameters:
m- the slopeb- they-intercept
-
set
-
setLine
-
set
public void set(double x1, double y1, double x2, double y2) Set the slopemand y-interceptbof the line passing 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
-
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
-
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
-
intersection
-
toString
-