math.geom2d.line
Class Line2D

java.lang.Object
  extended by math.geom2d.curve.AbstractContinuousCurve2D
      extended by math.geom2d.curve.AbstractSmoothCurve2D
          extended by 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
 Point2D p1
          The origin point.
 Point2D p2
          The destination point.
 
Fields inherited from interface math.geom2d.Shape2D
ACCURACY
 
Constructor Summary
Line2D(double x1, double y1, double x2, double y2)
          Define a new Line2D with two extremities.
Line2D(Point2D point1, Point2D point2)
          Define a new Line2D with two extremities.
 
Method Summary
 boolean almostEquals(GeometricObject2D obj, double eps)
          Checks if the two objects are similar up to a given threshold value.
 java.awt.geom.GeneralPath appendPath(java.awt.geom.GeneralPath path)
          Append the path of the curve to the given path.
 Box2D boundingBox()
          Returns the bounding box of the Line2D.
 CirculinearDomain2D buffer(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.
 Line2D clone()
          Overrides Object declaration to ensure Curve2D implementation are cloned as Curve2D.
 boolean contains(double x, double y)
          Returns true if the point (x, y) lies on the line, with precision given by Shape2D.ACCURACY.
 boolean contains(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 Line2D create(Point2D p1, Point2D p2)
          Deprecated. since 0.11.1
 double curvature(double t)
          Returns 0 as every linear shape.
 Vector2D direction()
          Return one direction vector of the linear shape.
 double distance(double x, double y)
          Returns the distance of the point (x, y) to this edge.
 double distance(Point2D p)
          Returns the distance of the point p to this edge.
 boolean equals(java.lang.Object obj)
          Two Line2D are equals if the share the two same points, in the same order.
 Point2D firstPoint()
          Get the first point of the curve.
 java.awt.geom.GeneralPath getGeneralPath()
           
 Point2D getOtherPoint(Point2D point)
          Return the opposite vertex of the edge.
 Point2D getPoint1()
          Return the first point of the edge.
 Point2D getPoint2()
          Return the last point of the edge.
 double getT0()
          Deprecated. replaced by t0() (since 0.11.1).
 double getT1()
          Deprecated. replaced by t1() (since 0.11.1).
 double getX1()
           
 double getX2()
           
 double getY1()
           
 double getY2()
           
 double horizontalAngle()
          Returns the angle with axis (O,i), counted counter-clockwise.
 Point2D intersection(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 boolean intersects(Line2D line1, Line2D line2)
          Checks if two line intersect.
 boolean isBounded()
          Returns true
 boolean isClosed()
          Returns false.
 boolean isColinear(LinearShape2D line)
           
 boolean isEmpty()
          Returns false
 boolean isInside(Point2D point)
          Returns true if the point is 'inside' the domain bounded by the curve.
 boolean isParallel(LinearShape2D line)
          Test if the this object is parallel to the given one.
 Point2D lastPoint()
          Get the last point of the curve.
 double length()
           
 double length(double pos)
           
 Point2D origin()
          Returns a point in the linear shape.
 Line2D parallel(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.
 StraightLine2D parallel(Point2D point)
          Creates a straight line parallel to this object, and passing through the given point.
 double[][] parametric()
           
 StraightLine2D perpendicular(Point2D point)
          Creates a straight line perpendicular to this object, and passing through the given point.
 Point2D point(double t)
          Returns the point located at the given position on the curve.
 double[] polarCoefficients()
           
 double[] polarCoefficientsSigned()
           
 double position(double length)
           
 double position(Point2D point)
          Returns the position of the point on the line.
 double project(Point2D point)
          Returns the position of the closest orthogonal projection of the point on the curve, or of the closest singular point.
 Line2D reverse()
          Returns the Line2D object which starts at point2 and ends at point1.
 void setPoint1(Point2D point)
           
 void setPoint2(Point2D point)
           
 double signedDistance(double x, double y)
          The same as distanceSigned(Point2D), but by passing 2 double as arguments.
 double signedDistance(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.
 Line2D subCurve(double t0, double t1)
          Returns a new Line2D, which is the portion of the line delimited by parameters t0 and t1.
 StraightLine2D supportingLine()
          Returns the straight line that contains this linear shape.
 double t0()
          Returns 0.
 double t1()
          Returns 1.
 Vector2D tangent(double t)
          Returns the tangent of the curve at the given position.
 java.lang.String toString()
           
 Line2D transform(AffineTransform2D trans)
          Transforms the shape by an affine transform.
 CirculinearElement2D transform(CircleInversion2D inv)
          Transforms the shape by a circle inversion.
 double windingAngle(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
 

Field Detail

p1

public Point2D p1
The origin point.


p2

public Point2D p2
The destination point.

Constructor Detail

Line2D

public Line2D(Point2D point1,
              Point2D point2)
Define a new Line2D with two extremities.


Line2D

public Line2D(double x1,
              double y1,
              double x2,
              double y2)
Define a new Line2D with two extremities.

Method Detail

intersects

public static boolean intersects(Line2D line1,
                                 Line2D line2)
Checks if two line intersect. Uses the Point2D.ccw method, which is based on Sedgewick algorithm.

Parameters:
line1 - a Line2D object
line2 - a Line2D object
Returns:
true if the 2 lines intersect

create

@Deprecated
public static Line2D create(Point2D p1,
                                       Point2D p2)
Deprecated. since 0.11.1

Static 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: CirculinearShape2D
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.

Specified by:
buffer in interface CirculinearShape2D
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: CirculinearCurve2D
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.

Specified by:
parallel in interface CirculinearContinuousCurve2D
Specified by:
parallel in interface CirculinearCurve2D
Specified by:
parallel in interface CirculinearElement2D
Parameters:
d - the distance between the original curve and he parallel curve.
Returns:
the parallel curve

length

public double length()
Specified by:
length in interface CirculinearCurve2D
Returns:
the length of the curve

length

public double length(double pos)
Specified by:
length in interface CirculinearCurve2D
Returns:
the length from the beginning to the position given by pos

position

public double position(double length)
Specified by:
position in interface CirculinearCurve2D
Returns:
the position located at a given geodesic distance from the origin

transform

public CirculinearElement2D transform(CircleInversion2D inv)
Description copied from interface: CirculinearShape2D
Transforms the shape by a circle inversion. The result is still an instance a CirculinearShape2D.

Specified by:
transform in interface CirculinearContinuousCurve2D
Specified by:
transform in interface CirculinearCurve2D
Specified by:
transform in interface CirculinearElement2D
Specified by:
transform in interface CirculinearShape2D
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: LinearShape2D
Returns the angle with axis (O,i), counted counter-clockwise. Result is given between 0 and 2*pi.

Specified by:
horizontalAngle in interface LinearShape2D

intersection

public Point2D intersection(LinearShape2D line)
Description copied from interface: LinearShape2D
Returns the unique intersection with a linear shape. If the intersection doesn't exist (parallel lines), returns null.

Specified by:
intersection in interface LinearShape2D

origin

public Point2D origin()
Description copied from interface: LinearShape2D
Returns a point in the linear shape.

Specified by:
origin in interface LinearShape2D
Returns:
a point in the linear shape.

supportingLine

public StraightLine2D supportingLine()
Description copied from interface: LinearShape2D
Returns 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:
supportingLine in interface LinearShape2D
Returns:
the straight line that contains this linear shape

direction

public Vector2D direction()
Description copied from interface: LinearShape2D
Return one direction vector of the linear shape.

Specified by:
direction in interface LinearShape2D
Returns:
a direction vector

signedDistance

public double signedDistance(Point2D p)
Description copied from interface: OrientedCurve2D
Returns 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:
signedDistance in interface OrientedCurve2D
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: OrientedCurve2D
The same as distanceSigned(Point2D), but by passing 2 double as arguments.

Specified by:
signedDistance in interface OrientedCurve2D
Parameters:
x - x-coord of a point
y - 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: ContinuousCurve2D
Returns a set of smooth curves.

Specified by:
smoothPieces in interface CirculinearContinuousCurve2D
Specified by:
smoothPieces in interface ContinuousCurve2D
Overrides:
smoothPieces in class AbstractSmoothCurve2D

isClosed

public boolean isClosed()
Returns false.

Specified by:
isClosed in interface ContinuousCurve2D
See Also:
ContinuousCurve2D.isClosed()

distance

public double distance(Point2D p)
Returns the distance of the point p to this edge.

Specified by:
distance in interface Shape2D

distance

public double distance(double x,
                       double y)
Returns the distance of the point (x, y) to this edge.

Specified by:
distance in interface Shape2D

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:
clip in interface CirculinearContinuousCurve2D
Specified by:
clip in interface CirculinearCurve2D
Specified by:
clip in interface CirculinearElement2D
Specified by:
clip in interface ContinuousCurve2D
Specified by:
clip in interface Curve2D
Specified by:
clip in interface SmoothCurve2D
Specified by:
clip in interface ContinuousOrientedCurve2D
Specified by:
clip in interface OrientedCurve2D
Specified by:
clip in interface SmoothOrientedCurve2D
Specified by:
clip in interface LinearElement2D
Specified by:
clip in interface Shape2D
Parameters:
box - the clipping box
Returns:
the clipped shape

boundingBox

public Box2D boundingBox()
Returns the bounding box of the Line2D.

Specified by:
boundingBox in interface Shape2D
Returns:
the bounding box of the shape.

tangent

public Vector2D tangent(double t)
Description copied from interface: SmoothCurve2D
Returns the tangent of the curve at the given position.

Specified by:
tangent in interface SmoothCurve2D
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:
curvature in interface ContinuousCurve2D
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: OrientedCurve2D
Return the angle portion that the curve turn around the given point. Result is a signed angle.

Specified by:
windingAngle in interface OrientedCurve2D
Parameters:
point - a point of the plane
Returns:
a signed angle

isInside

public boolean isInside(Point2D point)
Description copied from interface: OrientedCurve2D
Returns true if the point is 'inside' the domain bounded by the curve.

Specified by:
isInside in interface OrientedCurve2D
Parameters:
point - a point in the plane
Returns:
true if the point is on the left side of the curve.

t0

public double t0()
Returns 0.

Specified by:
t0 in interface Curve2D

getT0

@Deprecated
public double getT0()
Deprecated. replaced by t0() (since 0.11.1).

Specified by:
getT0 in interface Curve2D

t1

public double t1()
Returns 1.

Specified by:
t1 in interface Curve2D

getT1

@Deprecated
public double getT1()
Deprecated. replaced by t1() (since 0.11.1).

Specified by:
getT1 in interface Curve2D

point

public Point2D point(double t)
Description copied from interface: Curve2D
Returns 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.

Specified by:
point in interface Curve2D

firstPoint

public Point2D firstPoint()
Get the first point of the curve.

Specified by:
firstPoint in interface Curve2D
Overrides:
firstPoint in class AbstractContinuousCurve2D
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:
lastPoint in interface Curve2D
Overrides:
lastPoint in class AbstractContinuousCurve2D
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:
position in interface Curve2D
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: Curve2D
Returns 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.

Specified by:
project in interface Curve2D
Parameters:
point - a point to project
Returns:
the position of the closest orthogonal projection

reverse

public Line2D reverse()
Returns the Line2D object which starts at point2 and ends at point1.

Specified by:
reverse in interface CirculinearContinuousCurve2D
Specified by:
reverse in interface CirculinearCurve2D
Specified by:
reverse in interface CirculinearElement2D
Specified by:
reverse in interface ContinuousCurve2D
Specified by:
reverse in interface Curve2D
Specified by:
reverse in interface SmoothCurve2D
Specified by:
reverse in interface ContinuousOrientedCurve2D
Specified by:
reverse in interface OrientedCurve2D
Specified by:
reverse in interface SmoothOrientedCurve2D

continuousCurves

public java.util.Collection<? extends Line2D> continuousCurves()
Description copied from interface: Curve2D
Returns the collection of continuous curves which constitute this curve.

Specified by:
continuousCurves in interface CirculinearCurve2D
Specified by:
continuousCurves in interface Curve2D
Overrides:
continuousCurves in class AbstractContinuousCurve2D
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:
subCurve in interface CirculinearContinuousCurve2D
Specified by:
subCurve in interface CirculinearCurve2D
Specified by:
subCurve in interface CirculinearElement2D
Specified by:
subCurve in interface ContinuousCurve2D
Specified by:
subCurve in interface Curve2D
Specified by:
subCurve in interface SmoothCurve2D
Specified by:
subCurve in interface ContinuousOrientedCurve2D
Specified by:
subCurve in interface SmoothOrientedCurve2D
Specified by:
subCurve in interface LinearElement2D
Parameters:
t0 - position of the start of the sub-curve
t1 - 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: Curve2D
Returns the intersection points of the curve with the specified line. The length of the result array is the number of intersection points.

Specified by:
intersections in interface Curve2D

transform

public Line2D transform(AffineTransform2D trans)
Description copied from interface: Shape2D
Transforms the shape by an affine transform. Subclasses may override the type of returned shape.

Specified by:
transform in interface ContinuousCurve2D
Specified by:
transform in interface Curve2D
Specified by:
transform in interface SmoothCurve2D
Specified by:
transform in interface ContinuousOrientedCurve2D
Specified by:
transform in interface OrientedCurve2D
Specified by:
transform in interface SmoothOrientedCurve2D
Specified by:
transform in interface LinearElement2D
Specified by:
transform in interface LinearShape2D
Specified by:
transform in interface Shape2D
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.

Specified by:
contains in interface Shape2D

contains

public boolean contains(Point2D p)
Returns true if the point p lies on the line, with precision given by Shape2D.ACCURACY.

Specified by:
contains in interface Shape2D

isBounded

public boolean isBounded()
Returns true

Specified by:
isBounded in interface Shape2D

isEmpty

public boolean isEmpty()
Returns false

Specified by:
isEmpty in interface Shape2D
Returns:
true if the shape does not contain any point.

getGeneralPath

public java.awt.geom.GeneralPath getGeneralPath()

appendPath

public java.awt.geom.GeneralPath appendPath(java.awt.geom.GeneralPath path)
Description copied from interface: ContinuousCurve2D
Append the path of the curve to the given path.

Specified by:
appendPath in interface ContinuousCurve2D
Parameters:
path - a path to modify
Returns:
the modified path

almostEquals

public boolean almostEquals(GeometricObject2D obj,
                            double eps)
Description copied from interface: GeometricObject2D
Checks 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:
almostEquals in interface GeometricObject2D
Parameters:
obj - the object to compare
eps - 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:
toString in class java.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:
equals in class java.lang.Object
Parameters:
obj - the edge to compare to.
Returns:
true if extremities of both edges are the same.

clone

public Line2D clone()
Description copied from interface: Curve2D
Overrides Object declaration to ensure Curve2D implementation are cloned as Curve2D.

Specified by:
clone in interface Curve2D
Specified by:
clone in class AbstractSmoothCurve2D
Returns:
the cloned curve