Class Line2D
- java.lang.Object
-
- math.geom2d.curve.AbstractContinuousCurve2D
-
- math.geom2d.curve.AbstractSmoothCurve2D
-
- math.geom2d.line.Line2D
-
- All Implemented Interfaces:
- java.lang.Cloneable, CirculinearContinuousCurve2D, CirculinearCurve2D, CirculinearElement2D, CirculinearShape2D, ContinuousCurve2D, Curve2D, SmoothCurve2D, ContinuousOrientedCurve2D, OrientedCurve2D, SmoothOrientedCurve2D, GeometricObject2D, LinearElement2D, LinearShape2D, Shape2D
public class Line2D extends AbstractSmoothCurve2D implements LinearElement2D, java.lang.Cloneable
Line object defined from 2 points. This object keep points reference in memory, and recomputes properties directly from points. Line2D is mutable.Example :
// Create an Edge2D
Line2D line = new Line2D(new Point2D(0, 0), new Point2D(1, 2));
// Change direction of line, by changing second point :
line.setPoint2(new Point2D(4, 5));
// Change position and direction of the line, by changing first point.
// 'line' is now the edge (2,3)-(4,5)
line.setPoint1(new Point2D(2, 3));
This class may be slower than Edge2D or StraightLine2D, because parameters are updated each time a computation is made, causing lot of additional processing. Moreover, as inner point fields are public, it is not as safe as
LineSegment2D.
-
-
Field Summary
Fields Modifier and Type Field and Description Point2Dp1The origin point.Point2Dp2The destination point.
-
Constructor Summary
Constructors Constructor and Description Line2D(double x1, double y1, double x2, double y2)Define a new Line2D with two extremities.Line2D(Line2D line)Copy constructor.Line2D(Point2D point1, Point2D point2)Define a new Line2D with two extremities.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description booleanalmostEquals(GeometricObject2D obj, double eps)Checks if the two objects are similar up to a given threshold value.java.awt.geom.GeneralPathappendPath(java.awt.geom.GeneralPath path)Append the path of the curve to the given path.Box2DboundingBox()Returns the bounding box of the Line2D.CirculinearDomain2Dbuffer(double dist)Computes the buffer of the shape, formed by the set of points located at a distance from the shape that is lower or equal to d.double[]cartesianEquation()CurveSet2D<? extends Line2D>clip(Box2D box)Clips the line object by a box.Line2Dclone()Deprecated.use copy constructor instead (0.11.2)booleancontains(double x, double y)Returns true if the point (x, y) lies on the line, with precision given by Shape2D.ACCURACY.booleancontains(Point2D p)Returns true if the point p lies on the line, with precision given by Shape2D.ACCURACY.java.util.Collection<? extends Line2D>continuousCurves()Returns the collection of continuous curves which constitute this curve.static Line2Dcreate(Point2D p1, Point2D p2)Deprecated.since 0.11.1doublecurvature(double t)Returns 0 as every linear shape.Vector2Ddirection()Return one direction vector of the linear shape.doubledistance(double x, double y)Returns the distance of the point (x, y) to this edge.doubledistance(Point2D p)Returns the distance of the pointpto this edge.booleanequals(java.lang.Object obj)Two Line2D are equals if the share the two same points, in the same order.Point2DfirstPoint()Get the first point of the curve.java.awt.geom.GeneralPathgetGeneralPath()Point2DgetOtherPoint(Point2D point)Return the opposite vertex of the edge.Point2DgetPoint1()Return the first point of the edge.Point2DgetPoint2()Return the last point of the edge.doublegetT0()Deprecated.replaced by t0() (since 0.11.1).doublegetT1()Deprecated.replaced by t1() (since 0.11.1).doublegetX1()doublegetX2()doublegetY1()doublegetY2()doublehorizontalAngle()Returns the angle with axis (O,i), counted counter-clockwise.Point2Dintersection(LinearShape2D line)Returns the unique intersection with a linear shape.java.util.Collection<Point2D>intersections(LinearShape2D line)Returns the intersection points of the curve with the specified line.static booleanintersects(Line2D line1, Line2D line2)Checks if two line intersect.booleanisBounded()Returns truebooleanisClosed()Returns false.booleanisColinear(LinearShape2D line)booleanisEmpty()Returns falsebooleanisInside(Point2D point)Returns true if the point is 'inside' the domain bounded by the curve.booleanisParallel(LinearShape2D line)Test if the this object is parallel to the given one.Point2DlastPoint()Get the last point of the curve.doublelength()doublelength(double pos)Point2Dorigin()Returns a point in the linear shape.Line2Dparallel(double d)Creates a new curve, formed by the points with parameterization:p(t) = c(t) + d*n(t)/|n(t)|, with p(t) being a point of the original curve, n(t) the normal of the curve, and |n| being the norm of n.
In the case of a continuous curve formed by several smooth circulinear elements, the parallels of contiguous elements are joined by a circle arc.StraightLine2Dparallel(Point2D point)Creates a straight line parallel to this object, and passing through the given point.double[][]parametric()StraightLine2Dperpendicular(Point2D point)Creates a straight line perpendicular to this object, and passing through the given point.Point2Dpoint(double t)Returns the point located at the given position on the curve.double[]polarCoefficients()double[]polarCoefficientsSigned()doubleposition(double length)doubleposition(Point2D point)Returns the position of the point on the line.doubleproject(Point2D point)Returns the position of the closest orthogonal projection of the point on the curve, or of the closest singular point.Line2Dreverse()Returns the Line2D object which starts atpoint2and ends atpoint1.voidsetPoint1(Point2D point)voidsetPoint2(Point2D point)doublesignedDistance(double x, double y)The same as distanceSigned(Point2D), but by passing 2 double as arguments.doublesignedDistance(Point2D p)Returns the signed distance of the curve to the given point.java.util.Collection<? extends Line2D>smoothPieces()Returns a set of smooth curves.Line2DsubCurve(double t0, double t1)Returns a new Line2D, which is the portion of the line delimited by parameters t0 and t1.StraightLine2DsupportingLine()Returns the straight line that contains this linear shape.doublet0()Returns 0.doublet1()Returns 1.Vector2Dtangent(double t)Returns the tangent of the curve at the given position.java.lang.StringtoString()Line2Dtransform(AffineTransform2D trans)Transforms the shape by an affine transform.CirculinearElement2Dtransform(CircleInversion2D inv)Transforms the shape by a circle inversion.doublewindingAngle(Point2D point)Return the angle portion that the curve turn around the given point.-
Methods inherited from class math.geom2d.curve.AbstractSmoothCurve2D
isSingular, leftTangent, normal, rightTangent, singularPoints, vertices
-
Methods inherited from class math.geom2d.curve.AbstractContinuousCurve2D
asAwtShape, asPolyline, draw
-
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface math.geom2d.curve.SmoothCurve2D
normal
-
Methods inherited from interface math.geom2d.curve.ContinuousCurve2D
asPolyline, leftTangent, rightTangent
-
Methods inherited from interface math.geom2d.curve.Curve2D
asAwtShape, draw, isSingular, singularPoints, vertices
-
-
-
-
Constructor Detail
-
Line2D
public Line2D(double x1, double y1, double x2, double y2)Define a new Line2D with two extremities.
-
Line2D
public Line2D(Line2D line)
Copy constructor.
-
-
Method Detail
-
intersects
public static boolean intersects(Line2D line1, Line2D line2)
Checks if two line intersect. Uses thePoint2D.ccwmethod, which is based on Sedgewick algorithm.- Parameters:
line1- a Line2D objectline2- a Line2D object- Returns:
- true if the 2 lines intersect
-
create
@Deprecated public static Line2D create(Point2D p1, Point2D p2)
Deprecated. since 0.11.1Static factory for creating a new Line2D, starting from p1 and finishing at p2.
-
getPoint1
public Point2D getPoint1()
Return the first point of the edge. It corresponds to getPoint(0).- Returns:
- the first point.
-
getPoint2
public Point2D getPoint2()
Return the last point of the edge. It corresponds to getPoint(1).- Returns:
- the last point.
-
getX1
public double getX1()
-
getY1
public double getY1()
-
getX2
public double getX2()
-
getY2
public double getY2()
-
getOtherPoint
public Point2D getOtherPoint(Point2D point)
Return the opposite vertex of the edge.- Parameters:
point- : one of the vertices of the edge- Returns:
- the other vertex
-
setPoint1
public void setPoint1(Point2D point)
-
setPoint2
public void setPoint2(Point2D point)
-
isColinear
public boolean isColinear(LinearShape2D line)
-
isParallel
public boolean isParallel(LinearShape2D line)
Test if the this object is parallel to the given one. This method is overloaded to update parameters before computation.
-
buffer
public CirculinearDomain2D buffer(double dist)
Description copied from interface:CirculinearShape2DComputes the buffer of the shape, formed by the set of points located at a distance from the shape that is lower or equal to d.- Specified by:
bufferin interfaceCirculinearShape2D- Parameters:
dist- the maximal distance between a point of the buffer and the shape- Returns:
- the buffer of the shape
-
parallel
public Line2D parallel(double d)
Description copied from interface:CirculinearCurve2DCreates a new curve, formed by the points with parameterization:p(t) = c(t) + d*n(t)/|n(t)|, with p(t) being a point of the original curve, n(t) the normal of the curve, and |n| being the norm of n.
In the case of a continuous curve formed by several smooth circulinear elements, the parallels of contiguous elements are joined by a circle arc.- Specified by:
parallelin interfaceCirculinearContinuousCurve2D- Specified by:
parallelin interfaceCirculinearCurve2D- Specified by:
parallelin interfaceCirculinearElement2D- Parameters:
d- the distance between the original curve and he parallel curve.- Returns:
- the parallel curve
-
length
public double length()
- Specified by:
lengthin interfaceCirculinearCurve2D- Returns:
- the length of the curve
-
length
public double length(double pos)
- Specified by:
lengthin interfaceCirculinearCurve2D- Returns:
- the length from the beginning to the position given by pos
-
position
public double position(double length)
- Specified by:
positionin interfaceCirculinearCurve2D- Returns:
- the position located at a given geodesic distance from the origin
-
transform
public CirculinearElement2D transform(CircleInversion2D inv)
Description copied from interface:CirculinearShape2DTransforms the shape by a circle inversion. The result is still an instance a CirculinearShape2D.- Specified by:
transformin interfaceCirculinearContinuousCurve2D- Specified by:
transformin interfaceCirculinearCurve2D- Specified by:
transformin interfaceCirculinearElement2D- Specified by:
transformin interfaceCirculinearShape2D- Parameters:
inv- the circle inversion- Returns:
- the transformed shape
-
parametric
public double[][] parametric()
-
cartesianEquation
public double[] cartesianEquation()
-
polarCoefficients
public double[] polarCoefficients()
-
polarCoefficientsSigned
public double[] polarCoefficientsSigned()
-
horizontalAngle
public double horizontalAngle()
Description copied from interface:LinearShape2DReturns the angle with axis (O,i), counted counter-clockwise. Result is given between 0 and 2*pi.- Specified by:
horizontalAnglein interfaceLinearShape2D
-
intersection
public Point2D intersection(LinearShape2D line)
Description copied from interface:LinearShape2DReturns the unique intersection with a linear shape. If the intersection doesn't exist (parallel lines), returns null.- Specified by:
intersectionin interfaceLinearShape2D
-
origin
public Point2D origin()
Description copied from interface:LinearShape2DReturns a point in the linear shape.- Specified by:
originin interfaceLinearShape2D- Returns:
- a point in the linear shape.
-
supportingLine
public StraightLine2D supportingLine()
Description copied from interface:LinearShape2DReturns the straight line that contains this linear shape. The direction is the same, and if possible the direction vector should be the same.- Specified by:
supportingLinein interfaceLinearShape2D- Returns:
- the straight line that contains this linear shape
-
direction
public Vector2D direction()
Description copied from interface:LinearShape2DReturn one direction vector of the linear shape.- Specified by:
directionin interfaceLinearShape2D- Returns:
- a direction vector
-
signedDistance
public double signedDistance(Point2D p)
Description copied from interface:OrientedCurve2DReturns the signed distance of the curve to the given point. The distance is positive if the point lies outside the shape, and negative if the point lies inside the shape. In both cases, absolute value of distance is equals to the distance to the border of the shape.- Specified by:
signedDistancein interfaceOrientedCurve2D- Parameters:
p- a point of the plane- Returns:
- the signed distance to the curve
-
signedDistance
public double signedDistance(double x, double y)Description copied from interface:OrientedCurve2DThe same as distanceSigned(Point2D), but by passing 2 double as arguments.- Specified by:
signedDistancein interfaceOrientedCurve2D- Parameters:
x- x-coord of a pointy- y-coord of a point- Returns:
- the signed distance of the point (x,y) to the curve
-
smoothPieces
public java.util.Collection<? extends Line2D> smoothPieces()
Description copied from interface:ContinuousCurve2DReturns a set of smooth curves.- Specified by:
smoothPiecesin interfaceCirculinearContinuousCurve2D- Specified by:
smoothPiecesin interfaceContinuousCurve2D- Overrides:
smoothPiecesin classAbstractSmoothCurve2D
-
isClosed
public boolean isClosed()
Returns false.- Specified by:
isClosedin interfaceContinuousCurve2D- See Also:
ContinuousCurve2D.isClosed()
-
distance
public double distance(Point2D p)
Returns the distance of the pointpto this edge.
-
distance
public double distance(double x, double y)Returns the distance of the point (x, y) to this edge.
-
parallel
public StraightLine2D parallel(Point2D point)
Creates a straight line parallel to this object, and passing through the given point.- Parameters:
point- the point to go through- Returns:
- the parallel through the point
-
perpendicular
public StraightLine2D perpendicular(Point2D point)
Creates a straight line perpendicular to this object, and passing through the given point.- Parameters:
point- the point to go through- Returns:
- the perpendicular through point
-
clip
public CurveSet2D<? extends Line2D> clip(Box2D box)
Clips the line object by a box. The result is an instance of CurveSet2D, which contains only instances of LineArc2D. If the line object is not clipped, the result is an instance of CurveSet2D which contains 0 curves.- Specified by:
clipin interfaceCirculinearContinuousCurve2D- Specified by:
clipin interfaceCirculinearCurve2D- Specified by:
clipin interfaceCirculinearElement2D- Specified by:
clipin interfaceContinuousCurve2D- Specified by:
clipin interfaceCurve2D- Specified by:
clipin interfaceSmoothCurve2D- Specified by:
clipin interfaceContinuousOrientedCurve2D- Specified by:
clipin interfaceOrientedCurve2D- Specified by:
clipin interfaceSmoothOrientedCurve2D- Specified by:
clipin interfaceLinearElement2D- Specified by:
clipin interfaceShape2D- Parameters:
box- the clipping box- Returns:
- the clipped shape
-
boundingBox
public Box2D boundingBox()
Returns the bounding box of the Line2D.- Specified by:
boundingBoxin interfaceShape2D- Returns:
- the bounding box of the shape.
-
tangent
public Vector2D tangent(double t)
Description copied from interface:SmoothCurve2DReturns the tangent of the curve at the given position.- Specified by:
tangentin interfaceSmoothCurve2D- Parameters:
t- a position on the curve- Returns:
- the tangent vector computed for position t
- See Also:
SmoothCurve2D.normal(double)
-
curvature
public double curvature(double t)
Returns 0 as every linear shape.- Specified by:
curvaturein interfaceContinuousCurve2D- Parameters:
t- the position on the curve- Returns:
- the curvature of the curve for position t
-
windingAngle
public double windingAngle(Point2D point)
Description copied from interface:OrientedCurve2DReturn the angle portion that the curve turn around the given point. Result is a signed angle.- Specified by:
windingAnglein interfaceOrientedCurve2D- Parameters:
point- a point of the plane- Returns:
- a signed angle
-
isInside
public boolean isInside(Point2D point)
Description copied from interface:OrientedCurve2DReturns true if the point is 'inside' the domain bounded by the curve.- Specified by:
isInsidein interfaceOrientedCurve2D- Parameters:
point- a point in the plane- Returns:
- true if the point is on the left side of the curve.
-
getT0
@Deprecated public double getT0()
Deprecated. replaced by t0() (since 0.11.1).
-
getT1
@Deprecated public double getT1()
Deprecated. replaced by t1() (since 0.11.1).
-
point
public Point2D point(double t)
Description copied from interface:Curve2DReturns the point located at the given position on the curve. If the parameter lies outside the definition range, the parameter corresponding to the closest bound is used instead. This method can be used to draw an approximated outline of a curve, by selecting multiple values for t and drawing lines between them.
-
firstPoint
public Point2D firstPoint()
Get the first point of the curve.- Specified by:
firstPointin interfaceCurve2D- Overrides:
firstPointin classAbstractContinuousCurve2D- Returns:
- the first point of the curve
- See Also:
Curve2D.t0(),Curve2D.point(double)
-
lastPoint
public Point2D lastPoint()
Get the last point of the curve.- Specified by:
lastPointin interfaceCurve2D- Overrides:
lastPointin classAbstractContinuousCurve2D- Returns:
- the last point of the curve.
- See Also:
Curve2D.t1(),Curve2D.point(double)
-
position
public double position(Point2D point)
Returns the position of the point on the line. If point belongs to the line, this position is defined by the ratio:t = (xp - x0)/dx <\code>, or equivalently :t = (yp - y0)/dy <\code>.If point does not belong to edge, return Double.NaN. The current implementation uses the direction with the biggest derivative, in order to avoid divisions by zero.
- Specified by:
positionin interfaceCurve2D- Parameters:
point- a point belonging to the curve- Returns:
- the position of the point on the curve
- See Also:
Curve2D.point(double)
-
project
public double project(Point2D point)
Description copied from interface:Curve2DReturns the position of the closest orthogonal projection of the point on the curve, or of the closest singular point. This function should always returns a valid value.
-
reverse
public Line2D reverse()
Returns the Line2D object which starts atpoint2and ends atpoint1.- Specified by:
reversein interfaceCirculinearContinuousCurve2D- Specified by:
reversein interfaceCirculinearCurve2D- Specified by:
reversein interfaceCirculinearElement2D- Specified by:
reversein interfaceContinuousCurve2D- Specified by:
reversein interfaceCurve2D- Specified by:
reversein interfaceSmoothCurve2D- Specified by:
reversein interfaceContinuousOrientedCurve2D- Specified by:
reversein interfaceOrientedCurve2D- Specified by:
reversein interfaceSmoothOrientedCurve2D
-
continuousCurves
public java.util.Collection<? extends Line2D> continuousCurves()
Description copied from interface:Curve2DReturns the collection of continuous curves which constitute this curve.- Specified by:
continuousCurvesin interfaceCirculinearCurve2D- Specified by:
continuousCurvesin interfaceCurve2D- Overrides:
continuousCurvesin classAbstractContinuousCurve2D- Returns:
- a collection of continuous curves.
-
subCurve
public Line2D subCurve(double t0, double t1)
Returns a new Line2D, which is the portion of the line delimited by parameters t0 and t1.- Specified by:
subCurvein interfaceCirculinearContinuousCurve2D- Specified by:
subCurvein interfaceCirculinearCurve2D- Specified by:
subCurvein interfaceCirculinearElement2D- Specified by:
subCurvein interfaceContinuousCurve2D- Specified by:
subCurvein interfaceCurve2D- Specified by:
subCurvein interfaceSmoothCurve2D- Specified by:
subCurvein interfaceContinuousOrientedCurve2D- Specified by:
subCurvein interfaceSmoothOrientedCurve2D- Specified by:
subCurvein interfaceLinearElement2D- Parameters:
t0- position of the start of the sub-curvet1- position of the end of the sub-curve- Returns:
- the portion of original curve comprised between t0 and t1.
-
intersections
public java.util.Collection<Point2D> intersections(LinearShape2D line)
Description copied from interface:Curve2DReturns the intersection points of the curve with the specified line. The length of the result array is the number of intersection points.- Specified by:
intersectionsin interfaceCurve2D
-
transform
public Line2D transform(AffineTransform2D trans)
Description copied from interface:Shape2DTransforms the shape by an affine transform. Subclasses may override the type of returned shape.- Specified by:
transformin interfaceContinuousCurve2D- Specified by:
transformin interfaceCurve2D- Specified by:
transformin interfaceSmoothCurve2D- Specified by:
transformin interfaceContinuousOrientedCurve2D- Specified by:
transformin interfaceOrientedCurve2D- Specified by:
transformin interfaceSmoothOrientedCurve2D- Specified by:
transformin interfaceLinearElement2D- Specified by:
transformin interfaceLinearShape2D- Specified by:
transformin interfaceShape2D- Parameters:
trans- an affine transform- Returns:
- the transformed shape
-
contains
public boolean contains(double x, double y)Returns true if the point (x, y) lies on the line, with precision given by Shape2D.ACCURACY.
-
contains
public boolean contains(Point2D p)
Returns true if the point p lies on the line, with precision given by Shape2D.ACCURACY.
-
isEmpty
public boolean isEmpty()
Returns false
-
getGeneralPath
public java.awt.geom.GeneralPath getGeneralPath()
-
appendPath
public java.awt.geom.GeneralPath appendPath(java.awt.geom.GeneralPath path)
Description copied from interface:ContinuousCurve2DAppend the path of the curve to the given path.- Specified by:
appendPathin interfaceContinuousCurve2D- Parameters:
path- a path to modify- Returns:
- the modified path
-
almostEquals
public boolean almostEquals(GeometricObject2D obj, double eps)
Description copied from interface:GeometricObject2DChecks if the two objects are similar up to a given threshold value. This method can be used to compare the results of geometric computations, that introduce errors due to numerical computations.- Specified by:
almostEqualsin interfaceGeometricObject2D- Parameters:
obj- the object to compareeps- a threshold value, for example the minimal coordinate difference- Returns:
- true if both object have the same value up to the threshold
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
Two Line2D are equals if the share the two same points, in the same order.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the edge to compare to.- Returns:
- true if extremities of both edges are the same.
-
clone
@Deprecated public Line2D clone()
Deprecated. use copy constructor instead (0.11.2)- Specified by:
clonein interfaceCurve2D- Specified by:
clonein classAbstractSmoothCurve2D
-
-
DMelt 3.0 © DataMelt by jWork.ORG