Package com.irurueta.geometry
Class Line2D
java.lang.Object
com.irurueta.geometry.Line2D
- All Implemented Interfaces:
Serializable
Line2D in R2. Lines can be expressed using the following expression:
A * x + B * y + C = 0
where A and B are different from zero. Changing this expression to a
y = m * x + b format leads to the following definitions:
m: slope of the line. Defined as m = -A/B
b: interception point of this line. Defined as b = -c/B
angle of the line with respect to the x-axis = Math.atan(slope).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate doubleParameter A of a line.private doubleParameter B of a line.private doubleParameter C of a line.static final doubleDefines the threshold used when comparing two values.static final doublePositive threshold determine whether points lay inside (is locus) of a given line or not.private static final intConstant defining the size of vector that define the direction of a line.static final intNumber of line parameters.static final doubleMinimum allowed threshold.private booleanIndicates if line is normalized or not.private static final doubleMachine precision. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondouble[]asArray()Returns parameters of this line as an array containing [a, b, c].voidasArray(double[] array) Stores the parameters of this line in provided array as [a, b, c].voidclosestPoint(Point2D point, Point2D result) Computes the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line.voidclosestPoint(Point2D point, Point2D result, double threshold) Computes the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line.static Line2DCreates a new instance of a 2D line located at the canonical infinity.voiddirectorVector(double[] directorVector) Computes director vector of this line and stores the result in provided array.doubledotProduct(Line2D line) Computes the dot product between the parameters A, B,C of this line and the ones of provided line.booleanChecks if the line described by this instance equals provided line up to default comparison threshold.booleanChecks if the line described by this instance equals provided line up to provided threshold.booleanChecks if provided object equals current line.doublegetA()Returns parameter A of this line.doublegetAngle()Returns the angle of this line in radians.doublegetB()Returns parameter B of this line.doublegetC()Returns parameter C of this line.getClosestPoint(Point2D point) Returns the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line.getClosestPoint(Point2D point, double threshold) Returns the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line.double[]Returns director vector of this line.getIntersection(Line2D otherLine) Computes the intersection of this line with provided line.doublegetSlope()Returns the slope of this line.doubleReturns the y-coordinate intercept point of this line.inthashCode()Returns hash code value.voidintersection(Line2D otherLine, Point2D result) Computes the intersection of this line with provided line.booleanReturns boolean indicating whether provided point lies within this line (at a maximum distance of DEFAULT_LOCUS_THRESHOLD).booleanReturns boolean indicating whether provided point lies within this line (at a maximum distance of provided threshold).booleanReturns boolean indicating whether this line has already been normalized.voidNormalizes the parameters of this line to increase the accuracy of some computations.voidsetA(double a) Sets parameter A of this line.voidsetAngle(double angle) Sets angle of this line in radians.static voidSets provided 2D line into the canonical infinity.voidsetB(double b) Sets parameter B of this line.voidsetC(double c) Sets parameter C of this line.final voidsetParameters(double[] array) Sets parameters of this line.final voidsetParameters(double a, double b, double c) Sets parameters of this line.final voidsetParametersFromPairOfPoints(Point2D pointA, Point2D pointB) Sets parameters of this line from a pair of 2D points.final voidsetParametersFromPairOfPoints(Point2D pointA, Point2D pointB, boolean noThrow) Sets parameters of this line from a pair of 2D points.final voidsetParametersFromPointAndDirectorVector(Point2D point, double[] vector) Sets parameters of a 2D line from one point and its director vector.voidsetSlope(double slope) Sets the slope of this line.voidsetYIntercept(double yIntercept) Sets the y-coordinate intercept point of this line.doublesignedDistance(Point2D point) Distance between a line and a point.
-
Field Details
-
LINE_NUMBER_PARAMS
public static final int LINE_NUMBER_PARAMSNumber of line parameters.- See Also:
-
DEFAULT_LOCUS_THRESHOLD
public static final double DEFAULT_LOCUS_THRESHOLDPositive threshold determine whether points lay inside (is locus) of a given line or not.- See Also:
-
MIN_THRESHOLD
public static final double MIN_THRESHOLDMinimum allowed threshold.- See Also:
-
DEFAULT_COMPARISON_THRESHOLD
public static final double DEFAULT_COMPARISON_THRESHOLDDefines the threshold used when comparing two values.- See Also:
-
INHOM_VECTOR_SIZE
private static final int INHOM_VECTOR_SIZEConstant defining the size of vector that define the direction of a line.- See Also:
-
PRECISION
private static final double PRECISIONMachine precision.- See Also:
-
a
private double aParameter A of a line. -
b
private double bParameter B of a line. -
c
private double cParameter C of a line. -
normalized
private boolean normalizedIndicates if line is normalized or not.
-
-
Constructor Details
-
Line2D
public Line2D()Constructor. -
Line2D
public Line2D(double a, double b, double c) Constructor with parameters. Parameters of a line are provided in the following homogeneous format: A * x + B * y + C = 0- Parameters:
a- Parameter A of a line.b- Parameter B of a line.c- Parameter C of a line.
-
Line2D
Constructor. This constructor takes two 2D points to build a line passing through both of them.- Parameters:
pointA- First point used to compute the line.pointB- Second point used to compute the line.noThrow- If true no exception is thrown even if points are coincident.- Throws:
CoincidentPointsException- Raised if points are equal.
-
Line2D
Constructor. This constructor takes two 2D points to build a line passing through both of them.- Parameters:
pointA- First point used to compute the line.pointB- Second point used to compute the line.
-
Line2D
public Line2D(double[] array) Constructor.- Parameters:
array- Array containing the three parameters of a line (A, B, C).- Throws:
IllegalArgumentException- Raised if length of provided array is not three.
-
Line2D
Constructor of a line from one point and its director vector.- Parameters:
point- point passing through the line.vector- director vector.- Throws:
IllegalArgumentException- raised if vector length is not 2.
-
-
Method Details
-
getA
public double getA()Returns parameter A of this line.- Returns:
- Parameter A of this line.
-
getB
public double getB()Returns parameter B of this line.- Returns:
- Parameter B of this line.
-
getC
public double getC()Returns parameter C of this line.- Returns:
- Parameter C of this line.
-
setParameters
public final void setParameters(double a, double b, double c) Sets parameters of this line.- Parameters:
a- Parameter A of this line.b- Parameter B of this line.c- Parameter C of this line.
-
setParameters
public final void setParameters(double[] array) Sets parameters of this line.- Parameters:
array- Array containing parameters of this line.- Throws:
IllegalArgumentException- Raised if provided array does not have length equal to 3.
-
setA
public void setA(double a) Sets parameter A of this line.- Parameters:
a- Parameter A of this line.
-
setB
public void setB(double b) Sets parameter B of this line.- Parameters:
b- Parameter B of this line.
-
setC
public void setC(double c) Sets parameter C of this line.- Parameters:
c- Parameter C of this line.
-
getSlope
public double getSlope()Returns the slope of this line.- Returns:
- Slope of this line.
-
setSlope
public void setSlope(double slope) Sets the slope of this line.- Parameters:
slope- Slope of this line.
-
getAngle
public double getAngle()Returns the angle of this line in radians.- Returns:
- Angle of this line in radians.
-
setAngle
public void setAngle(double angle) Sets angle of this line in radians.- Parameters:
angle- Angle of this line in radians.
-
getYIntercept
public double getYIntercept()Returns the y-coordinate intercept point of this line. For a line following the expression A * X + B * y + C = 0 this method evaluates y for x = 0.- Returns:
- Vertical coordinate where the line intercepts with the Y axis.
-
setYIntercept
public void setYIntercept(double yIntercept) Sets the y-coordinate intercept point of this line. For a line following the expression A * x + B * y + C = 0 this method recalculates the parameters that define the line.- Parameters:
yIntercept- Vertical coordinate where the line intercepts with the Y axis.
-
setParametersFromPairOfPoints
public final void setParametersFromPairOfPoints(Point2D pointA, Point2D pointB, boolean noThrow) throws CoincidentPointsException Sets parameters of this line from a pair of 2D points. The line will pass through both points.- Parameters:
pointA- First point laying on this line.pointB- Second point laying on this line.noThrow- Enables/disables throwing exceptions.- Throws:
CoincidentPointsException- Raised if provided points are equal.
-
setParametersFromPairOfPoints
Sets parameters of this line from a pair of 2D points. The line will pass through both points.- Parameters:
pointA- First point laying on this line.pointB- Second point laying on this line.
-
setParametersFromPointAndDirectorVector
Sets parameters of a 2D line from one point and its director vector.- Parameters:
point- point passing through the line.vector- director vector.- Throws:
IllegalArgumentException- raised if vector length is not 2.
-
isLocus
Returns boolean indicating whether provided point lies within this line (at a maximum distance of provided threshold).- Parameters:
point- Point to be checked.threshold- Threshold to determine whether the point lies inside the line.- Returns:
- True if point lies inside the line (is locus), false otherwise.
- Throws:
IllegalArgumentException- Raised if provided threshold is negative.
-
isLocus
Returns boolean indicating whether provided point lies within this line (at a maximum distance of DEFAULT_LOCUS_THRESHOLD).- Parameters:
point- Point to be checked.- Returns:
- True if point lies inside the line (is locus), false otherwise.
-
signedDistance
Distance between a line and a point. Returned distance equals to the Euclidean distance between this line and provided point but having sign. Sign indicates whether point is at one side or the other of the line.- Parameters:
point- Point whose distance to this line will be computed.- Returns:
- Distance between this line and provided point.
-
getClosestPoint
Returns the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line. If provided point belong to this line, then the same point will be returned as a result.- Parameters:
point- Point to be checked.- Returns:
- Closest point.
-
getClosestPoint
Returns the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line. If provided point belong to this line, then the same point will be returned as a result.- Parameters:
point- Point to be checked.threshold- Threshold to determine whether point is locus of line or not.- Returns:
- Closest point.
- Throws:
IllegalArgumentException- Raised if threshold is negative.
-
closestPoint
Computes the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line. If provided point belongs to this line, then the same point will be returned as a result.- Parameters:
point- Point to be checked.result- Instance where the closest point will be stored.
-
closestPoint
Computes the point belonging to this line closest to provided point, which will be located at signedDistance(Point2D) from this line. If provided point belongs to this line, then the same point will be returned as a result.- Parameters:
point- Point to be checked.result- Instance where the closest point will be stored.threshold- threshold to determine whether a point belongs to the line's locus.- Throws:
IllegalArgumentException- Raised if threshold is negative.
-
asArray
public double[] asArray()Returns parameters of this line as an array containing [a, b, c].- Returns:
- Array containing all the parameters that describe this line.
-
asArray
public void asArray(double[] array) Stores the parameters of this line in provided array as [a, b, c].- Parameters:
array- Array where parameters of this line will be stored.- Throws:
IllegalArgumentException- Raised if provided array doesn't have length 3.
-
normalize
public void normalize()Normalizes the parameters of this line to increase the accuracy of some computations. -
isNormalized
public boolean isNormalized()Returns boolean indicating whether this line has already been normalized.- Returns:
- True if this line is normalized, false otherwise.
-
getDirectorVector
public double[] getDirectorVector()Returns director vector of this line.- Returns:
- Director vector of this line.
-
directorVector
public void directorVector(double[] directorVector) Computes director vector of this line and stores the result in provided array.- Parameters:
directorVector- Array containing director vector.- Throws:
IllegalArgumentException- Raised if provided array does not have length 2.
-
getIntersection
Computes the intersection of this line with provided line. Notice that parallel lines intersect at infinity.- Parameters:
otherLine- other line to be intersected with.- Returns:
- A 2D point indicating containing the intersection.
- Throws:
NoIntersectionException- if for numerical instabilities the intersection cannot be computed.
-
intersection
Computes the intersection of this line with provided line. Notice that parallel lines intersect at infinity.- Parameters:
otherLine- other line to be intersected with.result- 2D point where intersection will be stored. For greater accuracy it is recommended to use an HomogeneousPoint2D instance.- Throws:
NoIntersectionException- if for numerical instabilities the intersection cannot be computed.
-
dotProduct
Computes the dot product between the parameters A, B,C of this line and the ones of provided line. This method normalizes both lines to compute dot product.- Parameters:
line- line to compute dot product with.- Returns:
- dot product value.
-
equals
Checks if the line described by this instance equals provided line up to provided threshold.- Parameters:
line- line to be compared to.threshold- threshold grade of tolerance to determine whether the lines are equal or not. It is used because due to machine precision, the values might not be exactly equal (if not provided DEFAULT_COMPARISON_THRESHOLD is used).- Returns:
- true if current line and provided one are the same, false otherwise.
- Throws:
IllegalArgumentException- if threshold is negative.
-
equals
Checks if the line described by this instance equals provided line up to default comparison threshold.- Parameters:
line- line to be compared to.- Returns:
- true if current line and provided one are the same, false otherwise.
-
equals
Checks if provided object equals current line. -
hashCode
public int hashCode()Returns hash code value. This is only defined to keep the compiler happy. This method must be overridden in subclasses of this class. -
createCanonicalLineAtInfinity
Creates a new instance of a 2D line located at the canonical infinity. The canonical infinity corresponds to all 2D points located at infinity (i.e. m = (x,y,w = 0), hence l = (A = 0, B = 0, C = 1))- Returns:
- a new instance of a 2D line located at the canonical infinity.
-
setAsCanonicalLineAtInfinity
Sets provided 2D line into the canonical infinity. The canonical infinity corresponds to all 2D points located at infinity (i.e. m = (x,y,w = 0), hence l = (A = 0, B = 0, C = 1)).- Parameters:
line- 2D line to be set at infinity.
-