Class SlopeInterceptLine2D
-
- All Implemented Interfaces:
-
it.unibo.alchemist.model.geometry.Line2D
public final class SlopeInterceptLine2D<P extends Vector2D<P>> implements Line2D<P>
A Line2D represented in the slope-intercept form: y = slope * x + yIntercept. Doubles are only compared with fuzzyEquals.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public class
SlopeInterceptLine2D.Companion
Factories, constants, and utility methods for SlopeInterceptLine2D.
-
Field Summary
Fields Modifier and Type Field Description private final Boolean
isHorizontal
private final Boolean
isVertical
private final Double
slope
private final Double
yIntercept
private final Double
xIntercept
public final static SlopeInterceptLine2D.Companion
Companion
-
Constructor Summary
Constructors Constructor Description SlopeInterceptLine2D(Double slope, Double yIntercept, Function2<Double, Double, P> createPoint)
Creates a non-vertical line given its slope and yIntercept. SlopeInterceptLine2D(Double xIntercept, Function2<Double, Double, P> createPoint)
Creates a vertical line given its xIntercept.
-
Method Summary
Modifier and Type Method Description Boolean
isHorizontal()
Indicates if the line is aligned to the x-axis. Boolean
isVertical()
Indicates if the line is aligned to the y-axis. Double
getSlope()
The slope of the line, if isVertical this is undefined (= Double.NaN). Double
getYIntercept()
The y-coordinate of the y-intercept (= the point where the line intersects the y-axis). Double
getXIntercept()
The x-coordinate of the x-intercept (= the point where the line intersects the x-axis). Boolean
contains(P point)
Checks if the point belongs to this line. final Double
findY(Double x)
Solves the line equation for the given x. P
findPoint(Double x)
Finds the point belonging to the line with the given x-coordinate. Boolean
isParallelTo(Line2D<P> other)
Checks if two lines are parallel. final Boolean
coincidesWith(Line2D<?> other)
Checks if two lines coincide. Intersection2D<P>
intersect(Line2D<P> other)
Intersects two lines. Intersection2D<P>
intersectCircle(P center, Double radius)
Intersects a line and a circle. Boolean
equals(Object other)
Checks if other is a Line2D and if it coincidesWith this one. Integer
hashCode()
Uses slope, yIntercept and xIntercept. -
-
Method Detail
-
isHorizontal
Boolean isHorizontal()
Indicates if the line is aligned to the x-axis.
-
isVertical
Boolean isVertical()
Indicates if the line is aligned to the y-axis.
-
getSlope
Double getSlope()
The slope of the line, if isVertical this is undefined (= Double.NaN).
-
getYIntercept
Double getYIntercept()
The y-coordinate of the y-intercept (= the point where the line intersects the y-axis). If isVertical there's no y-intercept and this is Double.NaN. The slope-intercept representation (y = mx + b) uses this intercept.
-
getXIntercept
Double getXIntercept()
The x-coordinate of the x-intercept (= the point where the line intersects the x-axis). If isHorizontal there's no x-intercept and this is Double.NaN.
-
findY
final Double findY(Double x)
Solves the line equation for the given x. Throws an UnsupportedOperationException if the line isVertical.
-
findPoint
P findPoint(Double x)
Finds the point belonging to the line with the given x-coordinate. Throws an UnsupportedOperationException if the line isVertical.
-
isParallelTo
Boolean isParallelTo(Line2D<P> other)
Checks if two lines are parallel.
-
coincidesWith
final Boolean coincidesWith(Line2D<?> other)
Checks if two lines coincide.
-
intersect
Intersection2D<P> intersect(Line2D<P> other)
Intersects two lines.
-
intersectCircle
Intersection2D<P> intersectCircle(P center, Double radius)
Intersects a line and a circle. Radius must be positive. Intersection is performed by plugging the line equation in the circle equation and solving the resulting quadratic equation. Circle equation: (x - center.x)^2 + (y - center.y)^2 = r^2. Line equation: y = slope * x + yIntercept unless isVertical, x = xIntercept otherwise.
-
hashCode
Integer hashCode()
Uses slope, yIntercept and xIntercept.
-
-
-
-