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
public class DirectionalLine extends Line2D.Double
A directional line with normal vector. Based on the BSPLine-example from the book Developing games in Java from David Brackeen.Copyright (c) 2003-2023 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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDirectionalLine.Sidedifferent values for the side a point can be at w.r.t.-
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
Constructors Constructor Description DirectionalLine(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcalcNormal()Calculates the normal to this line.booleanequalsCoordinates(DirectionalLine line)Returns true if the endpoints of this line match the endpoints of the specified line.booleanequalsCoordinatesIgnoreOrder(DirectionalLine line)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).voidflip()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.doublegetIntersection(Line2D.Double line)Returns the fraction of intersection along this line.Point2D.DoublegetIntersectionPoint(Line2D.Double line)Returns the intersection point of this line with the specified line.doublegetLineThickness()Gets the thickness of the line.doublegetNormalx()doublegetNormaly()DirectionalLine.SidegetSide(Line2D.Double line)Gets the side of this line that the specified line segment is on.DirectionalLine.SidegetSideThick(double x, double y)Gets the side of this line the specified point is on.DirectionalLine.SidegetSideThin(double x, double y)Gets the side of this line the specified point is on.voidnormalize()Normalizes the normal of this line (make the normal's length 1).voidsetLine(double x1, double y1, double x2, double y2)voidsetLine(float x1, float y1, float x2, float y2)Set the line using floats.voidsetLineThickness(double lineThickness)Sets the thickness of the line.StringtoString()-
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 Detail
-
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 Detail
-
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:
setLinein 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
public boolean equalsCoordinates(DirectionalLine line)
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
public boolean equalsCoordinatesIgnoreOrder(DirectionalLine line)
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
-
getSideThick
public DirectionalLine.Side getSideThick(double x, double y)
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
public DirectionalLine.Side getSideThin(double x, double y)
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
public DirectionalLine.Side getSide(Line2D.Double line)
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
public double getIntersection(Line2D.Double line)
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
public Point2D.Double getIntersectionPoint(Line2D.Double line)
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
-
-