- Object
-
- AbstractShape
-
- Segment
-
- All Implemented Interfaces:
DataContainer
,Convex
,Rotatable
,Shape
,Transformable
,Translatable
,Wound
- Direct Known Subclasses:
Link
public class Segment extends AbstractShape implements Convex, Wound, Shape, Transformable, DataContainer
Implementation of a Line SegmentConvex
Shape
.This class represents a line segment that is infinitely thin.
- Since:
- 1.0.0
- Version:
- 4.2.0
- Author:
- William Bittle
-
-
Field Summary
-
Fields inherited from class AbstractShape
center, radius, userData
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
computeAABB(Transform transform, AABB aabb)
boolean
contains(Vector2 point, Transform transform)
Returns true if the given point is inside thisShape
.boolean
contains(Vector2 point, Transform transform, double radius)
Returns true if the given point is inside thisShape
.AABB
createAABB(Transform transform)
Mass
createMass(double density)
Vector2[]
getAxes(Vector2[] foci, Transform transform)
Returns an array of separating axes to test for thisShape
.Vector2
getEdgeVector()
Returns a normalized edge vector for this segment pointing from the first vertex to the second.EdgeFeature
getFarthestFeature(Vector2 vector, Transform transform)
Returns the feature farthest in the direction of n.static EdgeFeature
getFarthestFeature(Vector2 v1, Vector2 v2, Vector2 vector, Transform transform)
Returns the farthest feature on the given segment.Vector2
getFarthestPoint(Vector2 vector, Transform transform)
Returns the point farthest in the direction of the given vector.static Vector2
getFarthestPoint(Vector2 v1, Vector2 v2, Vector2 vector, Transform transform)
Returns the farthest point on the given segment.Vector2[]
getFoci(Transform transform)
Returns an array of world space foci points for circular curved edges.double
getLength()
Returns the length of the lineSegment
.Vector2
getLineIntersection(Segment segment)
static Vector2
getLineIntersection(Vector2 ap1, Vector2 ap2, Vector2 bp1, Vector2 bp2)
Returns the intersection point of the two lines or null if they are parallel or coincident.static double
getLocation(Vector2 point, Vector2 linePoint1, Vector2 linePoint2)
Determines where the point is relative to the given line.Iterator<Vector2>
getNormalIterator()
Returns an iterator for the normals.Vector2[]
getNormals()
Returns the array of edge normals in local coordinates.Vector2
getPoint1()
Returns point1 in local coordinates.Vector2
getPoint2()
Returns point2 in local coordinates.Vector2
getPointOnLineClosestToPoint(Vector2 point)
Returns the point on the line that thisSegment
defines closest to the given point.static Vector2
getPointOnLineClosestToPoint(Vector2 point, Vector2 linePoint1, Vector2 linePoint2)
Returns the point on the given line closest to the given point.Vector2
getPointOnSegmentClosestToPoint(Vector2 point)
Returns the point on thisSegment
closest to the given point.static Vector2
getPointOnSegmentClosestToPoint(Vector2 point, Vector2 linePoint1, Vector2 linePoint2)
Returns the point on the given line segment closest to the given point.double
getRadius(Vector2 center)
Returns the radius of the shape if the given point was the center for this shape.Vector2
getSegmentIntersection(Segment segment)
static Vector2
getSegmentIntersection(Vector2 ap1, Vector2 ap2, Vector2 bp1, Vector2 bp2)
Returns the intersection point of the two line segments or null if they are parallel, coincident or don't intersect.Iterator<Vector2>
getVertexIterator()
Returns an iterator for the vertices.Vector2[]
getVertices()
Returns the array of vertices in local coordinates.Interval
project(Vector2 vector, Transform transform)
void
rotate(Rotation rotation, double x, double y)
Rotates the object about the given point.String
toString()
void
translate(double x, double y)
Translates the object the given amounts in the respective directions.-
Methods inherited from class AbstractShape
computeAABB, contains, createAABB, getCenter, getRadius, getUserData, project, rotate, rotate, rotate, rotate, rotate, rotateAboutCenter, setUserData, translate
-
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface DataContainer
getUserData, setUserData
-
Methods inherited from interface Shape
computeAABB, contains, createAABB, getCenter, getRadius, project, rotateAboutCenter
-
Methods inherited from interface Translatable
translate
-
-
-
-
Constructor Detail
-
Segment
public Segment(Vector2 point1, Vector2 point2)
Full constructor.Creates a new segment using the given points. The center will be the average of the points.
A segment's points cannot be null or the same point.
- Parameters:
point1
- the first pointpoint2
- the second point- Throws:
NullPointerException
- if point1 or point2 is nullIllegalArgumentException
- if point1 == point2
-
-
Method Detail
-
toString
public String toString()
- Overrides:
toString
in classAbstractShape
-
getVertices
public Vector2[] getVertices()
Description copied from interface:Wound
Returns the array of vertices in local coordinates.For performance, this array may be the internal storage array of the shape. Both the array elements and their properties should not be modified via this method.
It's possible that this method will be deprecated and/or removed in later versions.
- Specified by:
getVertices
in interfaceWound
- Returns:
Vector2
[]- See Also:
Wound.getVertexIterator()
-
getNormals
public Vector2[] getNormals()
Description copied from interface:Wound
Returns the array of edge normals in local coordinates.For performance, this array may be the internal storage array of the shape. Both the array elements and their properties should not be modified via this method.
It's possible that this method will be deprecated and/or removed in later versions.
- Specified by:
getNormals
in interfaceWound
- Returns:
Vector2
[]- See Also:
Wound.getNormalIterator()
-
getVertexIterator
public Iterator<Vector2> getVertexIterator()
Description copied from interface:Wound
Returns an iterator for the vertices.The iterator does not support the remove method and will return a new
Vector2
in the next method.This method is safer than the
Wound.getVertices()
since its not possible to modify the array or its elements.- Specified by:
getVertexIterator
in interfaceWound
- Returns:
- Iterator<
Vector2
>
-
getNormalIterator
public Iterator<Vector2> getNormalIterator()
Description copied from interface:Wound
Returns an iterator for the normals.The iterator does not support the remove method and will return a new
Vector2
in the next method rather than the underlying value.This method is safer than the
Wound.getNormals()
since its not possible to modify the array or its elements.- Specified by:
getNormalIterator
in interfaceWound
- Returns:
- Iterator<
Vector2
>
-
getRadius
public double getRadius(Vector2 center)
Description copied from interface:Shape
Returns the radius of the shape if the given point was the center for this shape.
-
getLength
public double getLength()
Returns the length of the lineSegment
.- Returns:
- double
-
getLocation
public static double getLocation(Vector2 point, Vector2 linePoint1, Vector2 linePoint2)
Determines where the point is relative to the given line.Set L = linePoint2 - linePoint1 Set P = point - linePoint1 location = L.cross(P)
Returns 0 if the point lies on the line created from the line segment.
Assuming a right handed coordinate system:
Returns < 0 if the point lies on the right side of the line
Returns > 0 if the point lies on the left side of the lineAssumes all points are in world space.
- Parameters:
point
- the pointlinePoint1
- the first point of the linelinePoint2
- the second point of the line- Returns:
- double
- Throws:
NullPointerException
- if point, linePoint1, or linePoint2 is null
-
getPointOnLineClosestToPoint
public static Vector2 getPointOnLineClosestToPoint(Vector2 point, Vector2 linePoint1, Vector2 linePoint2)
Returns the point on the given line closest to the given point.Project the point onto the line:
Vline = P1 - P0 Vpoint = P0 - P Pclosest = Vpoint.project(Vline)
Assumes all points are in world space.- Parameters:
point
- the pointlinePoint1
- the first point of the linelinePoint2
- the second point of the line- Returns:
Vector2
- Throws:
NullPointerException
- if point, linePoint1, or linePoint2 is null- See Also:
Vector2.project(Vector2)
-
getPointOnLineClosestToPoint
public Vector2 getPointOnLineClosestToPoint(Vector2 point)
Returns the point on the line that thisSegment
defines closest to the given point.This method works in this
Segment
's local space.- Parameters:
point
- the local space point- Returns:
Vector2
- Throws:
NullPointerException
- if the given point is null- Since:
- 3.1.5
- See Also:
getPointOnLineClosestToPoint(Vector2, Vector2, Vector2)
-
getPointOnSegmentClosestToPoint
public static Vector2 getPointOnSegmentClosestToPoint(Vector2 point, Vector2 linePoint1, Vector2 linePoint2)
Returns the point on the given line segment closest to the given point.If the point closest to the given point is on the line created by the given line segment, but is not on the line segment then either of the segments end points will be returned.
Assumes all points are in world space.
- Parameters:
point
- the pointlinePoint1
- the first point of the linelinePoint2
- the second point of the line- Returns:
Vector2
- Throws:
NullPointerException
- if point, linePoint1, or linePoint2 is null- See Also:
getPointOnLineClosestToPoint(Vector2, Vector2, Vector2)
-
getPointOnSegmentClosestToPoint
public Vector2 getPointOnSegmentClosestToPoint(Vector2 point)
Returns the point on thisSegment
closest to the given point.This method works in this
Segment
's local space.- Parameters:
point
- the local space point- Returns:
Vector2
- Throws:
NullPointerException
- if the given point is null- Since:
- 3.1.5
- See Also:
getPointOnSegmentClosestToPoint(Vector2, Vector2, Vector2)
-
getLineIntersection
public static Vector2 getLineIntersection(Vector2 ap1, Vector2 ap2, Vector2 bp1, Vector2 bp2)
Returns the intersection point of the two lines or null if they are parallel or coincident.If we let:
A = Ap2 - Ap1 B = Bp2 - Bp1
we can create two parametric equations:Q = Ap1 + taA Q = Bp1 + tbB
Where Q is the intersection point:Ap1 + taA = Bp1 + tbB
We can solve for tb by applying the cross product with A on both sides:(Ap1 + taA) x A = (Bp1 + tbB) x A Ap1 x A = Bp1 x A + tbB x A (Ap1 - Bp1) x A = tbB x A tb = ((Ap1 - Bp1) x A) / (B x A)
If B x A == 0 then the lines are parallel. If both the top and bottom are zero then the lines are coincident.If the lines are parallel or coincident, null is returned.
- Parameters:
ap1
- the first point of the first lineap2
- the second point of the first linebp1
- the first point of the second linebp2
- the second point of the second line- Returns:
- Vector2 the intersection point; null if the lines are parallel or coincident
- Throws:
NullPointerException
- if ap1, ap2, bp1 or bp2 is null- Since:
- 3.1.1
- See Also:
getSegmentIntersection(Vector2, Vector2, Vector2, Vector2)
-
getLineIntersection
public Vector2 getLineIntersection(Segment segment)
Returns the line intersection of the givenSegment
and thisSegment
.This method treats this segment and the given segment as defining lines rather than segments.
This method assumes that both this and the given segment are in the same space (either local or world space).
If the lines are parallel or coincident, null is returned.
- Parameters:
segment
- the other segment- Returns:
Vector2
- Throws:
NullPointerException
- if the given segment is null- Since:
- 3.1.5
- See Also:
getLineIntersection(Vector2, Vector2, Vector2, Vector2)
-
getSegmentIntersection
public static Vector2 getSegmentIntersection(Vector2 ap1, Vector2 ap2, Vector2 bp1, Vector2 bp2)
Returns the intersection point of the two line segments or null if they are parallel, coincident or don't intersect.If we let:
A = Ap2 - Ap1 B = Bp2 - Bp1
we can create two parametric equations:Q = Ap1 + taA Q = Bp1 + tbB
Where Q is the intersection point:Ap1 + taA = Bp1 + tbB
We can solve for tb by applying the cross product with A on both sides:(Ap1 + taA) x A = (Bp1 + tbB) x A Ap1 x A = Bp1 x A + tbB x A (Ap1 - Bp1) x A = tbB x A tb = ((Ap1 - Bp1) x A) / (B x A)
If B x A == 0 then the segments are parallel. If the top == 0 then they don't intersect. If both the top and bottom are zero then the segments are coincident.If tb or ta less than zero or greater than 1 then the segments do not intersect.
If the segments do not intersect, are parallel, or are coincident, null is returned.
- Parameters:
ap1
- the first point of the first line segmentap2
- the second point of the first line segmentbp1
- the first point of the second line segmentbp2
- the second point of the second line segment- Returns:
- Vector2 the intersection point; null if the line segments don't intersect, are parallel, or are coincident
- Throws:
NullPointerException
- if ap1, ap2, bp1, or bp2 is null- Since:
- 3.1.1
- See Also:
getLineIntersection(Vector2, Vector2, Vector2, Vector2)
-
getSegmentIntersection
public Vector2 getSegmentIntersection(Segment segment)
Returns the intersection of the givenSegment
and thisSegment
.This method assumes that both this and the given segment are in the same space (either local or world space).
If the segments do not intersect, are parallel, or are coincident, null is returned.
- Parameters:
segment
- the other segment- Returns:
Vector2
- Throws:
NullPointerException
- if the given segment is null- Since:
- 3.1.5
- See Also:
getSegmentIntersection(Vector2, Vector2, Vector2, Vector2)
-
getFarthestFeature
public static final EdgeFeature getFarthestFeature(Vector2 v1, Vector2 v2, Vector2 vector, Transform transform)
Returns the farthest feature on the given segment.This will always return the segment itself, but must return it with the correct winding and the correct maximum.
- Parameters:
v1
- the first segment vertexv2
- the second segment vertexvector
- the directiontransform
- the local to world spaceTransform
of thisConvex
Shape
- Returns:
EdgeFeature
- Throws:
NullPointerException
- if v1, v2, vector, or transform is null- Since:
- 3.1.5
-
getFarthestPoint
public static final Vector2 getFarthestPoint(Vector2 v1, Vector2 v2, Vector2 vector, Transform transform)
Returns the farthest point on the given segment.- Parameters:
v1
- the first point of the segmentv2
- the second point of the segmentvector
- the directiontransform
- the local to world spaceTransform
of thisConvex
Shape
- Returns:
Vector2
- Throws:
NullPointerException
- if v1, v2, vector, or transform is null- Since:
- 3.1.5
-
getAxes
public Vector2[] getAxes(Vector2[] foci, Transform transform)
Description copied from interface:Convex
Returns an array of separating axes to test for thisShape
.The
foci
parameter is an array of circular focal points of the otherShape
.If foci points are given, this method will return the separating axes for this
Shape
's voronoi regions also. The points in the foci array are assumed to be in world space.The returned axes are normalized and in world space.
-
getFoci
public Vector2[] getFoci(Transform transform)
Returns an array of world space foci points for circular curved edges.This method returns null if the
Shape
has zero curved edges.The returned points are in world space.
Not applicable to this shape. Always returns null.
-
contains
public boolean contains(Vector2 point, Transform transform)
Returns true if the given point is inside thisShape
.If the given point lies on an edge the point is considered to be inside the
Shape
.The given point is assumed to be in world space.
Should almost always return false since this shape represents an infinitely thin line segment. Use the
contains(Vector2, Transform, double)
method instead for better, though technically inaccurate, results.
-
contains
public boolean contains(Vector2 point, Transform transform, double radius)
Returns true if the given point is inside thisShape
.If the given point lies on an edge the point is considered to be inside the
Shape
.The given point is assumed to be in world space.
If the radius is greater than zero then the point is tested to be within the shape expanded radially by the radius.
-
project
public Interval project(Vector2 vector, Transform transform)
Description copied from interface:Shape
-
getFarthestPoint
public Vector2 getFarthestPoint(Vector2 vector, Transform transform)
Description copied from interface:Convex
Returns the point farthest in the direction of the given vector. If two points are equally distant along the givenVector2
the first one is used.The returned point is in world space.
-
getFarthestFeature
public EdgeFeature getFarthestFeature(Vector2 vector, Transform transform)
- Specified by:
getFarthestFeature
in interfaceConvex
- Parameters:
vector
- the directiontransform
- the local to world spaceTransform
of thisConvex
Shape
- Returns:
EdgeFeature
-
rotate
public void rotate(Rotation rotation, double x, double y)
Description copied from interface:Rotatable
Rotates the object about the given point.- Specified by:
rotate
in interfaceRotatable
- Overrides:
rotate
in classAbstractShape
- Parameters:
rotation
- theRotation
representing the rotation amountx
- the x coordinate to rotate abouty
- the y coordinate to rotate about
-
translate
public void translate(double x, double y)
Description copied from interface:Translatable
Translates the object the given amounts in the respective directions.- Specified by:
translate
in interfaceTranslatable
- Overrides:
translate
in classAbstractShape
- Parameters:
x
- the translation in the x directiony
- the translation in the y direction
-
createMass
public Mass createMass(double density)
Creates aMass
object using the geometric properties of thisSegment
and the given density.m = d * length I = l2 * m / 12
- Specified by:
createMass
in interfaceShape
- Parameters:
density
- the density in kg/m2- Returns:
Mass
theMass
of thisSegment
-
createAABB
public AABB createAABB(Transform transform)
Creates anAABB
from thisShape
after applying the given transformation to the shape.Be aware that this method could produce an infinitely thin AABB if this segment is aligned to either the x or y-axis.
- Specified by:
createAABB
in interfaceShape
- Overrides:
createAABB
in classAbstractShape
- Parameters:
transform
- theTransform
for thisShape
- Returns:
AABB
theAABB
enclosing thisShape
-
computeAABB
public void computeAABB(Transform transform, AABB aabb)
Description copied from interface:Shape
Computes theAABB
from thisShape
after applying the given transformation and places the result in the givenAABB
.- Specified by:
computeAABB
in interfaceShape
- Parameters:
transform
- theTransform
for thisShape
aabb
- the destinationAABB
-
getEdgeVector
public Vector2 getEdgeVector()
Returns a normalized edge vector for this segment pointing from the first vertex to the second.- Returns:
- Vector2
- Since:
- 4.2.0
-
-