Class DirectionalLine
java.lang.Object
java.awt.geom.Line2D
java.awt.geom.Line2D.Double
nl.tudelft.simulation.language.d2.DirectionalLine
- All Implemented Interfaces:
Shape
,Serializable
,Cloneable
A directional line with normal vector. Based on the BSPLine-example from the book Developing games in Java from David
Brackeen.
Copyright (c) 2003-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can be found at https://https://simulation.tudelft.nl/dsol/docs/latest/license.html.
- Author:
- Roy Chin
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
different values for the side a point can be at w.r.t. the line.Nested classes/interfaces inherited from class java.awt.geom.Line2D
Line2D.Double, Line2D.Float
-
Field Summary
Fields inherited from class java.awt.geom.Line2D.Double
x1, x2, y1, y2
-
Constructor Summary
ConstructorDescriptionDirectionalLine
(double x1, double y1, double x2, double y2) Creates a new DirectionalLine based on the specified coordinates.DirectionalLine
(float x1, float y1, float x2, float y2) Creates a new DirectionalLine based on the specified (float) coordinates. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Calculates the normal to this line.boolean
Returns true if the endpoints of this line match the endpoints of the specified line.boolean
Returns true if the endpoints of this line match the endpoints of the specified line, ignoring endpoint order (if the first point of this line is equal to the second point of the specified line, and vice versa, returns true).void
flip()
Flips this line so that the end points are reversed (in other words, (x1,y1) becomes (x2,y2) and vice versa) and the normal is changed to point the opposite direction.double
getIntersection
(Line2D.Double line) Returns the fraction of intersection along this line.Returns the intersection point of this line with the specified line.double
Gets the thickness of the line.double
double
getSide
(Line2D.Double line) Gets the side of this line that the specified line segment is on.getSideThick
(double x, double y) Gets the side of this line the specified point is on.getSideThin
(double x, double y) Gets the side of this line the specified point is on.void
Normalizes the normal of this line (make the normal's length 1).void
setLine
(double x1, double y1, double x2, double y2) void
setLine
(float x1, float y1, float x2, float y2) Set the line using floats.void
setLineThickness
(double lineThickness) Sets the thickness of the line.toString()
Methods inherited from class java.awt.geom.Line2D.Double
getBounds2D, getP1, getP2, getX1, getX2, getY1, getY2
Methods inherited from class java.awt.geom.Line2D
clone, contains, contains, contains, contains, getBounds, getPathIterator, getPathIterator, intersects, intersects, intersectsLine, intersectsLine, linesIntersect, ptLineDist, ptLineDist, ptLineDist, ptLineDistSq, ptLineDistSq, ptLineDistSq, ptSegDist, ptSegDist, ptSegDist, ptSegDistSq, ptSegDistSq, ptSegDistSq, relativeCCW, relativeCCW, relativeCCW, setLine, setLine
-
Constructor Details
-
DirectionalLine
public DirectionalLine(double x1, double y1, double x2, double y2) Creates a new DirectionalLine based on the specified coordinates.- Parameters:
x1
- double; Coordinate x1y1
- double; Coordinate y1x2
- double; Coordinate x2y2
- double; Coordinate y2
-
DirectionalLine
public DirectionalLine(float x1, float y1, float x2, float y2) Creates a new DirectionalLine based on the specified (float) coordinates.- Parameters:
x1
- float; Coordinate x1y1
- float; Coordinate y1x2
- float; Coordinate x2y2
- float; Coordinate y2
-
-
Method Details
-
calcNormal
public void calcNormal()Calculates the normal to this line. The normal of (a, b) is (-b, a). -
normalize
public void normalize()Normalizes the normal of this line (make the normal's length 1). -
setLine
public void setLine(float x1, float y1, float x2, float y2) Set the line using floats.- Parameters:
x1
- float; x1 coordinatey1
- float; y1 coordinatex2
- float; x2 coordinatey2
- float; y2 coordinate
-
setLine
public void setLine(double x1, double y1, double x2, double y2) - Overrides:
setLine
in classLine2D.Double
-
flip
public void flip()Flips this line so that the end points are reversed (in other words, (x1,y1) becomes (x2,y2) and vice versa) and the normal is changed to point the opposite direction. -
equalsCoordinates
Returns true if the endpoints of this line match the endpoints of the specified line. Ignores normal and height values.- Parameters:
line
- DirectionalLine; another line- Returns:
- true if this line's coordinates are equal to the other line's coordinates
-
equalsCoordinatesIgnoreOrder
Returns true if the endpoints of this line match the endpoints of the specified line, ignoring endpoint order (if the first point of this line is equal to the second point of the specified line, and vice versa, returns true). Ignores normal and height values.- Parameters:
line
- DirectionalLine; another line- Returns:
- true if coordinates match independent of the order
-
toString
-
getSideThick
Gets the side of this line the specified point is on. This method treats the line as 1-unit thick, so points within this 1-unit border are considered collinear. For this to work correctly, the normal of this line must be normalized, either by setting this line to a polygon or by calling normalize(). Returns either FRONTSIDE, BACKSIDE, or COLLINEAR.- Parameters:
x
- double; coordinate xy
- double; coordinate y- Returns:
- the side
-
getSideThin
Gets the side of this line the specified point is on. Because of doubling point inaccuracy, a collinear line will be rare. For this to work correctly, the normal of this line must be normalized, either by setting this line to a polygon or by calling normalize(). Returns either FRONTSIDE, BACKSIDE, or COLLINEAR.- Parameters:
x
- double; coordinate xy
- double; coordinate y- Returns:
- the side
-
getSide
Gets the side of this line that the specified line segment is on. Returns either FRONT, BACK, COLINEAR, or SPANNING.- Parameters:
line
- Line2D.Double; line segment- Returns:
- the side
-
getIntersection
Returns the fraction of intersection along this line. Returns a value from 0 to 1 if the segments intersect. For example, a return value of 0 means the intersection occurs at point (x1, y1), 1 means the intersection occurs at point (x2, y2), and .5 mean the intersection occurs halfway between the two endpoints of this line. Returns -1 if the lines are parallel.- Parameters:
line
- Line2D.Double; a line- Returns:
- the intersection
-
getIntersectionPoint
Returns the intersection point of this line with the specified line.- Parameters:
line
- Line2D.Double; a line- Returns:
- intersection point
-
getLineThickness
public double getLineThickness()Gets the thickness of the line.- Returns:
- returns the lineThickness
-
setLineThickness
public void setLineThickness(double lineThickness) Sets the thickness of the line.- Parameters:
lineThickness
- double; the lineThickness to set
-
getNormalx
public double getNormalx()- Returns:
- returns the normalX
-
getNormaly
public double getNormaly()- Returns:
- returns the normalY
-