org.openimaj.math.geometry.line

## Class Line2d

• All Implemented Interfaces:
Cloneable, GeometricObject2d, Path2d

```public class Line2d
extends Object
implements Path2d, Cloneable```
A line in two-dimensional space.
Author:
Jonathon Hare (jsh2@ecs.soton.ac.uk), David Dupplaw (dpd@ecs.soton.ac.uk)
• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
`static class ` `Line2d.IntersectionResult`
The result of a line intersection.
`static class ` `Line2d.IntersectionType`
The type of a result of a line intersection
• ### Field Summary

Fields
Modifier and Type Field and Description
`Point2d` `begin`
Start point of line
`Point2d` `end`
End point of line
• ### Constructor Summary

Constructors
Constructor and Description
`Line2d()`
Construct a line
```Line2d(float x1, float y1, float x2, float y2)```
Construct a line
```Line2d(Point2d begin, Point2d end)```
Construct a line
• ### Method Summary

All Methods
Modifier and Type Method and Description
`Polyline` `asPolyline()`
Convert the path to a polyline representation
`Point2d` `begin()`
Get the beginning of the path
`Point2d` `calculateCentroid()`
Calculate the centroid of the shape
`double` `calculateHorizontalAngle()`
Returns the angle (radians) this line makes with a horizontal line
`double` `calculateLength()`
Calculate the length of the path
`Rectangle` `calculateRegularBoundingBox()`
Compute the regular (oriented to the axes) bounding box of the shape.
`double` `calculateVerticalAngle()`
Returns the angle (radians) this line makes with a vertical line
`Line2d` `clone()`
`static double` ```distance(float p1x, float p1y, float p2x, float p2y)```
Get the Euclidean distance between two points
`static double` ```distance(Point2d p1, Point2d p2)```
Get the Euclidean distance between two points
`float` `distanceToLine(Point2d p)`
Returns the shortest distance between the point and this line.
`Point2d` `end()`
Get the end of the path
`boolean` `equals(Object other)`
`Point2d` `getBeginPoint()`
Get the start point
`Point2d` `getEndPoint()`
Get the end point
`double` `getHeight()`
`Line2d.IntersectionResult` `getIntersection(Line2d otherLine)`
Calculates the intersection point of this line and another line
`Line2d` `getNormal()`
Returns a line that is at 90 degrees to the original line.
`Line2d` `getNormal(Point2d p)`
Returns a line that is at 90 degrees to the original line and also passes through the given point.
`double` `getWidth()`
`int` `hashCode()`
`boolean` ```isInLine(Point2d p, float tolerance)```
Tests whether the given point lies on this line.
`boolean` ```isOnLine(Point2d p, float tolerance)```
Tests whether the given point lies on this line.
`static Line2d` ```lineFromRotation(int x1, int y1, double theta, int length)```
Create a line of a given length that starts at a point and has a given angle.
`Iterator<Line2d>` `lineIterator()`
Convert the path to a iterated polyline representation
`Line2d` `lineWithinSquare(Rectangle r)`
Given a rectangle, return the line that actually fits inside the rectangle for this line
`double` `maxX()`
`double` `maxY()`
`double` `minX()`
`double` `minY()`
`Point2d` `reflectAroundLine(Point2d pointToReflect)`
Reflects a point around a this line.
`void` `scale(float sc)`
Scale the shape by the given amount about (0,0).
`void` ```scale(Point2d centre, float sc)```
Scale the shape by the given amount about the given point.
`void` `scaleCentroid(float sc)`
Scale the shape about its centroid.
`void` `setBeginPoint(Point2d begin)`
Set the start point
`Point2d` `setEndPoint()`
Get the end point
`void` `setEndPoint(Point2d end)`
Set the end point
`String` `toString()`
`Point2dImpl` `toUnitVector()`
Convert the line to a unit vector
`Line2d` `transform(Jama.Matrix transform)`
Transform a line.
`void` ```translate(float x, float y)```
Translate the shapes position
• ### Methods inherited from class java.lang.Object

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

• #### begin

`public Point2d begin`
Start point of line
• #### end

`public Point2d end`
End point of line
• ### Constructor Detail

• #### Line2d

`public Line2d()`
Construct a line
• #### Line2d

```public Line2d(Point2d begin,
Point2d end)```
Construct a line
Parameters:
`begin` - start point
`end` - end point
• #### Line2d

```public Line2d(float x1,
float y1,
float x2,
float y2)```
Construct a line
Parameters:
`x1` - x-ordinate of start point
`y1` - y-ordinate of start point
`x2` - x-ordinate of end point
`y2` - y-ordinate of end point
• ### Method Detail

• #### setBeginPoint

`public void setBeginPoint(Point2d begin)`
Set the start point
Parameters:
`begin` - start point
• #### setEndPoint

`public void setEndPoint(Point2d end)`
Set the end point
Parameters:
`end` - end point
• #### getBeginPoint

`public Point2d getBeginPoint()`
Get the start point
Returns:
the start point
• #### getEndPoint

`public Point2d getEndPoint()`
Get the end point
Returns:
The end point
• #### setEndPoint

`public Point2d setEndPoint()`
Get the end point
Returns:
the end point
• #### getIntersection

`public Line2d.IntersectionResult getIntersection(Line2d otherLine)`
Calculates the intersection point of this line and another line
Parameters:
`otherLine` - The other line segment
Returns:
a `Line2d.IntersectionResult`
"http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/"
• #### reflectAroundLine

`public Point2d reflectAroundLine(Point2d pointToReflect)`
Reflects a point around a this line.
Parameters:
`pointToReflect` - The point to reflect
Returns:
The reflected point
• #### lineWithinSquare

`public Line2d lineWithinSquare(Rectangle r)`
Given a rectangle, return the line that actually fits inside the rectangle for this line
Parameters:
`r` - the bounds
Returns:
the line
• #### distance

```public static double distance(Point2d p1,
Point2d p2)```
Get the Euclidean distance between two points
Parameters:
`p1` - the first point
`p2` - the second point
Returns:
the distance
• #### distance

```public static double distance(float p1x,
float p1y,
float p2x,
float p2y)```
Get the Euclidean distance between two points
Parameters:
`p1x` - the first point
`p1y` - the first point
`p2x` - the second point
`p2y` - the first point
Returns:
the distance
• #### lineFromRotation

```public static Line2d lineFromRotation(int x1,
int y1,
double theta,
int length)```
Create a line of a given length that starts at a point and has a given angle.
Parameters:
`x1` - x-ordinate of starting point
`y1` - y-ordinate of starting point
`theta` - angle in radians
`length` - line length
Returns:
the line
• #### calculateLength

`public double calculateLength()`
Description copied from interface: `Path2d`
Calculate the length of the path
Specified by:
`calculateLength` in interface `Path2d`
Returns:
The length of the line.
• #### calculateHorizontalAngle

`public double calculateHorizontalAngle()`
Returns the angle (radians) this line makes with a horizontal line
Returns:
the angle this line makes with a horizontal line
• #### calculateVerticalAngle

`public double calculateVerticalAngle()`
Returns the angle (radians) this line makes with a vertical line
Returns:
the angle this line makes with a vertical line
• #### transform

`public Line2d transform(Jama.Matrix transform)`
Transform a line.
Specified by:
`transform` in interface `GeometricObject2d`
Parameters:
`transform` - the transform matrix.
Returns:
the transformed line.
• #### getNormal

`public Line2d getNormal()`
Returns a line that is at 90 degrees to the original line.
Returns:
the normal line
• #### getNormal

`public Line2d getNormal(Point2d p)`
Returns a line that is at 90 degrees to the original line and also passes through the given point.
Parameters:
`p` - A point that must exist on the normal line
Returns:
a new line at right-angles to this
• #### translate

```public void translate(float x,
float y)```
Description copied from interface: `GeometricObject2d`
Translate the shapes position
Specified by:
`translate` in interface `GeometricObject2d`
Parameters:
`x` - x-translation
`y` - y-translation
• #### isInLine

```public boolean isInLine(Point2d p,
float tolerance)```
Tests whether the given point lies on this line. If the point sits on the line but is outside of the end points, then this function will return false.
Parameters:
`p` - The point to test.
`tolerance` - The tolerance to use in the test
Returns:
TRUE if the point lies on this line.
• #### toString

`public String toString()`
Overrides:
`toString` in class `Object`
• #### calculateRegularBoundingBox

`public Rectangle calculateRegularBoundingBox()`
Description copied from interface: `GeometricObject2d`
Compute the regular (oriented to the axes) bounding box of the shape.
Specified by:
`calculateRegularBoundingBox` in interface `GeometricObject2d`
Returns:
the regular bounding box as [x,y,width,height]
• #### scale

`public void scale(float sc)`
Description copied from interface: `GeometricObject2d`
Scale the shape by the given amount about (0,0). Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scale` in interface `GeometricObject2d`
Parameters:
`sc` - the scale factor.
• #### scale

```public void scale(Point2d centre,
float sc)```
Description copied from interface: `GeometricObject2d`
Scale the shape by the given amount about the given point. Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scale` in interface `GeometricObject2d`
Parameters:
`centre` - the centre of the scaling operation
`sc` - the scale factor
• #### scaleCentroid

`public void scaleCentroid(float sc)`
Description copied from interface: `GeometricObject2d`
Scale the shape about its centroid. Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scaleCentroid` in interface `GeometricObject2d`
Parameters:
`sc` - the scale factor
• #### calculateCentroid

`public Point2d calculateCentroid()`
Description copied from interface: `GeometricObject2d`
Calculate the centroid of the shape
Specified by:
`calculateCentroid` in interface `GeometricObject2d`
Returns:
the centroid of the shape
• #### minX

`public double minX()`
Specified by:
`minX` in interface `GeometricObject2d`
Returns:
the minimum x-ordinate
• #### minY

`public double minY()`
Specified by:
`minY` in interface `GeometricObject2d`
Returns:
the minimum y-ordinate
• #### maxX

`public double maxX()`
Specified by:
`maxX` in interface `GeometricObject2d`
Returns:
the maximum x-ordinate
• #### maxY

`public double maxY()`
Specified by:
`maxY` in interface `GeometricObject2d`
Returns:
the maximum y-ordinate
• #### getWidth

`public double getWidth()`
Specified by:
`getWidth` in interface `GeometricObject2d`
Returns:
the width of the regular bounding box
• #### getHeight

`public double getHeight()`
Specified by:
`getHeight` in interface `GeometricObject2d`
Returns:
the height of the regular bounding box
• #### toUnitVector

`public Point2dImpl toUnitVector()`
Convert the line to a unit vector
Returns:
unit vector in the same direction as the line
• #### clone

`public Line2d clone()`
Overrides:
`clone` in class `Object`
• #### hashCode

`public int hashCode()`
Overrides:
`hashCode` in class `Object`
• #### equals

`public boolean equals(Object other)`
Overrides:
`equals` in class `Object`
• #### begin

`public Point2d begin()`
Description copied from interface: `Path2d`
Get the beginning of the path
Specified by:
`begin` in interface `Path2d`
Returns:
the beginning
• #### end

`public Point2d end()`
Description copied from interface: `Path2d`
Get the end of the path
Specified by:
`end` in interface `Path2d`
Returns:
the end
• #### asPolyline

`public Polyline asPolyline()`
Description copied from interface: `Path2d`
Convert the path to a polyline representation
Specified by:
`asPolyline` in interface `Path2d`
Returns:
a polyline representation of the path
• #### lineIterator

`public Iterator<Line2d> lineIterator()`
Description copied from interface: `Path2d`
Convert the path to a iterated polyline representation
Specified by:
`lineIterator` in interface `Path2d`
Returns:
an iterated polyline representation of the path
• #### isOnLine

```public boolean isOnLine(Point2d p,
float tolerance)```
Tests whether the given point lies on this line. Note that this will test whether the point sits on a line that travels to infinity in both directions.
Parameters:
`p` - The point to test.
`tolerance` - The tolerance to use in the test
Returns:
TRUE if the point lies on this line.
• #### distanceToLine

`public float distanceToLine(Point2d p)`
Returns the shortest distance between the point and this line. Note that this assumes the line travels to infinity in both directions.
Parameters:
`p` - The point to test.
Returns:
The distance from the point to the closest point on the line