org.geotools.metadata.math

## Class Line

• All Implemented Interfaces:
Serializable, Cloneable

```public class Line
extends Object
implements Cloneable, Serializable```
Equation of a line in a two dimensional space (x,y). A line has an equation of the form y=ax+b. At the difference of `Line2D` (which are bounded by (x1,y1) and (x2,y2) points), `Line` objects extends toward infinity.

The equation parameters for a `Line` object can bet set at construction time or using one of the `setLine(...)` methods. The y value can be computed for a given x value using the `y(double)` method. Method `x(double)` compute the converse and should work even if the line is vertical.

Since:
2.0
Author:
Martin Desruisseaux (PMO, IRD)
See Also:
`Point2D`, `Line2D`, `Plane`, Serialized Form
• ### Constructor Summary

Constructors
Constructor and Description
`Line()`
Construct an initially unitialized line.
```Line(double slope, double y0)```
Construct a line with the specified slope and offset.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`Line` `clone()`
Returns a clone of this line.
`boolean` `equals(Object object)`
Compares this object with the specified one for equality.
`double` `getSlope()`
Returns the slope.
`double` `getX0()`
Returns the x value for y==0.
`double` `getY0()`
Returns the y value for x==0.
`int` `hashCode()`
Returns a hash code value for this line.
`Point2D` `intersectionPoint(Line line)`
Returns the intersection point between this line and the specified one.
`Point2D` `intersectionPoint(Line2D line)`
Returns the intersection point between this line and the specified bounded line.
`Line2D` ```isoscelesTriangleBase(Point2D summit, double sideLength)```
Computes the base of a isosceles triangle having the specified summit and side length.
`Point2D` `nearestColinearPoint(Point2D point)`
Returns the nearest point on this line from the specified point.
`double` ```setLine(double[] x, double[] y)```
Given a set of data points `x[0..ndata-1]`, `y[0..ndata-1]`, fit them to a straight line y=b+mx in a least-squares senses.
`void` ```setLine(double slope, double y0)```
Sets the slope and offset for this line.
`void` `setLine(Line2D line)`
Sets a line colinear with the specified line segment.
`void` ```setLine(Point2D p1, Point2D p2)```
Sets a line through the specified point.
`String` `toString()`
Returns a string representation of this line.
`void` ```translate(double dx, double dy)```
Translates the line.
`double` `x(double y)`
Computes x=f-1(y).
`double` `y(double x)`
Computes y=f(x).
• ### Methods inherited from class Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`
• ### Constructor Detail

• #### Line

`public Line()`
Construct an initially unitialized line. All methods will returns `Double.NaN`.
• #### Line

```public Line(double slope,
double y0)```
Construct a line with the specified slope and offset. The linear equation will be y=slope*x+y0.
Parameters:
`slope` - The slope.
`y0` - The y value at x==0.
See Also:
`setLine(double, double)`
• ### Method Detail

• #### setLine

`public void setLine(Line2D line)`
Sets a line colinear with the specified line segment. The line will continues toward infinity after the line segment extremities.
Parameters:
`line` - The line segment.
See Also:
`setLine(Point2D,Point2D)`
• #### setLine

```public void setLine(Point2D p1,
Point2D p2)```
Sets a line through the specified point. The line will continues toward infinity after the point.
Parameters:
`p1` - Coordinate of the first point.
`p2` - Coordinate of the second point.
See Also:
`setLine(Line2D)`
• #### setLine

```public double setLine(double[] x,
double[] y)
throws IllegalArgumentException```
Given a set of data points `x[0..ndata-1]`, `y[0..ndata-1]`, fit them to a straight line y=b+mx in a least-squares senses. This method assume that the x values are precise and all uncertainty is in y.

Reference: Linear Regression Curve Fitting.

Parameters:
`x` - Vector of x values (independant variable).
`y` - Vector of y values (dependant variable).
Returns:
Estimation of the correlation coefficient. The closer this coefficient is to 1, the better the fit.
Throws:
`MismatchedSizeException` - if x and y don't have the same length.
`IllegalArgumentException`
• #### translate

```public void translate(double dx,
double dy)```
Translates the line. The slope stay unchanged.
Parameters:
`dx` - The horizontal translation.
`dy` - The vertical translation.
• #### y

`public final double y(double x)`
Computes y=f(x). If the line is vertical, then this method returns an infinite value. This method is final for performance reason.
Parameters:
`x` - The x value.
Returns:
The y value.
See Also:
`x(double)`
• #### x

`public final double x(double y)`
Computes x=f-1(y). If the line is horizontal, then this method returns an infinite value. This method is final for performance reason.
Parameters:
`y` - The y value.
Returns:
The x value.
See Also:
`y(double)`
• #### getY0

`public final double getY0()`
Returns the y value for x==0. Coordinate (0, y0) is the intersection point with the y axis.
• #### getX0

`public final double getX0()`
Returns the x value for y==0. Coordinate (x0,0) is the intersection point with the x axis.
• #### getSlope

`public final double getSlope()`
Returns the slope.
• #### intersectionPoint

`public Point2D intersectionPoint(Line line)`
Returns the intersection point between this line and the specified one. If both lines are parallel, then this method returns `null`.
Parameters:
`line` - The line to intersect.
Returns:
The intersection point, or `null`.
• #### intersectionPoint

`public Point2D intersectionPoint(Line2D line)`
Returns the intersection point between this line and the specified bounded line. If both lines are parallel or if the specified `line` doesn't reach this line (since `Line2D` do not extends toward infinities), then this method returns `null`.
Parameters:
`line` - The bounded line to intersect.
Returns:
The intersection point, or `null`.
• #### nearestColinearPoint

`public Point2D nearestColinearPoint(Point2D point)`
Returns the nearest point on this line from the specified point.
Parameters:
`point` - An arbitrary point.
Returns:
The point on this line which is the nearest of the specified `point`.
• #### isoscelesTriangleBase

```public Line2D isoscelesTriangleBase(Point2D summit,
double sideLength)```
Computes the base of a isosceles triangle having the specified summit and side length. The base will be colinear with this line. In other words, this method compute two points (x1,y1) and (x2,y2) located in such a way that:
• Both points are on this line.
• The distance between any of the two points and the specified `summit` is exactly `sideLength`.
Parameters:
`summit` - The summit of the isosceles triangle.
`sideLength` - The length for the two sides of the isosceles triangle.
Returns:
The base of the isoscele triangle, colinear with this line, or `null` if the base can't be computed. If non-null, then the triangle is the figure formed by joining (x1,y1), (x2,y2) and `summit`.
• #### toString

`public String toString()`
Returns a string representation of this line. This method returns the linear equation in the form `"y=m*x+b"`.
Overrides:
`toString` in class `Object`
Returns:
A string representation of this line.
• #### equals

`public boolean equals(Object object)`
Compares this object with the specified one for equality.
Overrides:
`equals` in class `Object`
• #### hashCode

`public int hashCode()`
Returns a hash code value for this line.
Overrides:
`hashCode` in class `Object`
• #### clone

`public Line clone()`
Returns a clone of this line.
Overrides:
`clone` in class `Object`
Returns:
A copy of this object.
See Also:
`Object.clone()`

Copyright © 1996–2021 Geotools. All rights reserved.