Package com.amalgamasimulation.geometry
Class Segment
java.lang.Object
com.amalgamasimulation.geometry.AbstractGeometricPrimitive
com.amalgamasimulation.geometry.Segment
Class representing a directed segment in 2D space. The segment is directed
from its begin point to its end point. This class provides methods to
determine segments' intersection, relative position of segments and relations
between segments and points. Zero-length and infinite length segments are
allowed although most of the methods will not work correctly with such
segments.
absolute offset - signed distance from the begin point of segment
to some other point lying on the line of the segment
relative offset - absolute offset divided by the length of the
segment. Thus, relative offset of segment's end point is always 1
This class uses the ideas of absolute offset and relative offset:
- Author:
- Andrey Malykhanov
-
Constructor Summary
ConstructorsConstructorDescriptionSegment(double beginPointX, double beginPointY, double endPointX, double endPointY) Creates a new instance of Segment class with the specified coordinates of begin and end points.Creates a new instance of Segment class with specified begin and end points. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsPoint(Point point) Checks whether segment contains the specified point, i.e.booleancontainsPoint(Point point, boolean includeEndPoint) Checks whether this segment contains the specified point, i.e.doubledistanceToPoint(Point point) Returns the shortest distance from segment to the specified point.booleanChecks whether the other given segment is equal to this segment, i.e.booleanReturns the begin point of this segment.getClosestPointTo(Point point) Returns the point on this segment closest to the specified point.getConnectingSegment(Segment otherSegment) Returns the shortest segment directed from the current segment to the specified segment, or null if the two segments intersect.getCopy()Returns a copy of this segment.doublegetDX()Returns X-coordinate difference between end and begin points of segment.doublegetDY()Returns Y-coordinate difference between end and begin points of segment.Returns the end point of segment.getFirstIntersection(Segment segment) Returns anIntersectionDescriptorinstance which contains information (intersection point as well as absolute and relative offsets of intersected segments) about intersection with specified segment.getFirstIntersection(Segment segment, boolean includeEndPoints) Returns anIntersectionDescriptorinstance which contains information (intersection point as well as absolute and relative offsets of intersected segments) about intersection of this segment with the specified segment.getFirstIntersectionPoint(Segment segment) Returns first intersection point of segment and the specified another segment.getFirstIntersectionPoint(Segment segment, boolean includeEndPoints) Returns first intersection point of segment and the specified another segment.doubleReturns the heading of this segment.doubleReturn the length of segment, i.e.getParallelSegment(double distance) Returns a segment which is parallel to this segment and has a specified distance to this segment.doublegetPointAbsoluteOffset(Point point) Returns absolute offset of the specified point lying on the line of the segment, or Double.NaN if the point does not lie on the line of the segment.getPointOnLineByAbsoluteOffset(double absoluteOffset) Returns point on line of this segment by its absolute offset from the begin point of this segment.getPointOnLineByRelativeOffset(double relativeOffset) Returns point on line of this segment by its relative offset from the begin point of this segment.doublegetPointRelativeOffset(Point point) Returns relative offset of the specified point lying on the line of the segment, or Double.NaN if the point does not lie on the line of the segment.Returns the segment with begin and end point swappedgetRotated(double angle) Returns segment rotated to the specified angle around its begin point.getRotatedAround(double angle, Point rotationCenter) Returns segment rotated to the specified angle around the specified point (rotation center).getSegmentOnLineByAbsoluteOffsets(double beginPointAbsoluteOffset, double endPointAbsoluteOffset) Returns segment lying on the line of this segment located by the specified absolute offsets measured from the begin point of this segment.getSegmentOnLineByRelativeOffsets(double beginPointRelativeOffset, double endPointRelativeOffset) Returns segment lying on the line of this segment located by the specified relative offsets measured from the begin point of this segment.inthashCode()booleanintersectsWith(Segment segment) Checks whether this segment intersects with the specified segment.booleanintersectsWith(Segment segment, boolean includeEndPoints) Checks whether this segment intersects with the specified segment.Returns a segment having begin and end points coordinate-wise subtracted the specified point.multiply(double factor) Returns a segment having all its coordinates multiplied by the specified factorReturns a segment having begin and end points coordinate-wise added to the specified point.protected voidprotected voidvoidsetBeginPoint(Point beginPoint) Sets a new begin point to this segment.voidsetEndPoint(Point endPoint) Sets a new end point to this segment.Returns the string representing Java code that creates this primitive in its current statetoString()
-
Constructor Details
-
Segment
Creates a new instance of Segment class with specified begin and end points.- Parameters:
beginPoint- - Begin point of segment.endPoint- - End point of segment.
-
Segment
public Segment(double beginPointX, double beginPointY, double endPointX, double endPointY) Creates a new instance of Segment class with the specified coordinates of begin and end points.- Parameters:
beginPointX- - X-coordinate of begin point of segmentbeginPointY- - Y-coordinate of begin point of segmentendPointX- - X-coordinate of end point of segmentendPointY- - Y-coordinate of end point of segment
-
-
Method Details
-
getBeginPoint
Returns the begin point of this segment.- Returns:
- begin point of segment.
-
setBeginPoint
Sets a new begin point to this segment. Recalculates all internal information of segment instance.- Parameters:
beginPoint- new begin point of segment.
-
setEndPoint
Sets a new end point to this segment. Recalculates all internal information of segment instance.- Parameters:
endPoint- - new end point of segment.
-
getEndPoint
Returns the end point of segment.- Returns:
- End point of segment.
-
getLength
public double getLength()Return the length of segment, i.e. distance between begin and end points of the segment- Returns:
- Length of segment.
-
getHeading
public double getHeading()Returns the heading of this segment. The heading is calculated as angle between the base vector pointing downwards (in screen coordinate system) and the direction from begin to end point of this segment. Counter-clockwise direction from the base vector is considered positive heading, clockwise - negative heading. The heading always lies in range (-PI, PI].
- Returns:
- heading of this segment in radians.
- See Also:
-
getDX
public double getDX()Returns X-coordinate difference between end and begin points of segment.- Returns:
- X-coordinate difference between end and begin points of segment.
-
getDY
public double getDY()Returns Y-coordinate difference between end and begin points of segment.- Returns:
- Y-coordinate difference between end and begin points of segment.
-
recalculate
protected void recalculate() -
recalculateHeading
protected void recalculateHeading() -
containsPoint
Checks whether this segment contains the specified point, i.e. the specified point lies on segment. Allows to include or exclude end point of this segment.- Parameters:
point- - Specified point.includeEndPoint- - Include or exclude end point of segment.- Returns:
trueif segment contains the specified point,falseotherwise.
-
containsPoint
Checks whether segment contains the specified point, i.e. the specified point lies on segment.- Specified by:
containsPointin classAbstractGeometricPrimitive- Parameters:
point- - Specified point.- Returns:
trueif segment contains the specified point,falseotherwise.
-
intersectsWith
Checks whether this segment intersects with the specified segment.- Parameters:
segment- - Specified segment to check intersection with.- Returns:
trueif segments intersect,falseotherwise.
-
intersectsWith
Checks whether this segment intersects with the specified segment. Allows to include or exclude end points of both segments when detecting intersection.- Parameters:
segment- - Specified segment to check intersection with.includeEndPoints- - Whether or not consider end points of segments when detecting intersection.- Returns:
trueif segments intersect,falseotherwise.
-
distanceToPoint
Returns the shortest distance from segment to the specified point.- Parameters:
point- - Specified point.- Returns:
- Shortest distance to the specified point.
-
getClosestPointTo
Returns the point on this segment closest to the specified point.- Parameters:
point- - Specified point.- Returns:
- Closest point on segment to the specified point.
-
getPointAbsoluteOffset
Returns absolute offset of the specified point lying on the line of the segment, or Double.NaN if the point does not lie on the line of the segment.- Parameters:
point- - Specified point.- Returns:
- Absolute offset (can be positive, zero or negative) of the specified point, or Double.NaN if the point does not lie on the line of the segment.
-
getPointRelativeOffset
Returns relative offset of the specified point lying on the line of the segment, or Double.NaN if the point does not lie on the line of the segment.- Parameters:
point- - Specified point.- Returns:
- Relative offset (can be positive, zero or negative) of the specified point, or Double.NaN if the point does not lie on the line of the segment.
-
getFirstIntersectionPoint
Returns first intersection point of segment and the specified another segment. Allows to include or exclude end points of segments when detecting intersection point. If segments do not intersect, returns null. If segments intersect at more than one point (segments lie on the same line and one segment is a part of another), returns point closest to the begin point of segment.- Parameters:
segment- - Specified segment.includeEndPoints- - Whether or not include end points of segments when detecting intersection point.- Returns:
- Intersection point of segments or null if segments do not intersect.
-
getFirstIntersectionPoint
Returns first intersection point of segment and the specified another segment. If segments do not intersect, returns null. If segments intersect at more than one point (segments lie on the same line and one segment is a part of another), returns point closest to the begin point of segment.- Parameters:
segment- - Specified segment.- Returns:
- Intersection point of segments or null if segments do not intersect.
-
getFirstIntersection
Returns anIntersectionDescriptorinstance which contains information (intersection point as well as absolute and relative offsets of intersected segments) about intersection of this segment with the specified segment. Allows to include or exclude end points of segments when detecting intersection. If segments do not intersect, returns null.- Parameters:
segment- - Specified segment.includeEndPoints- - - Whether or not include end points of segments when detecting intersection.- Returns:
- IntersectionDescriptor with information about intersection or null if segments do not intersect
-
getFirstIntersection
Returns anIntersectionDescriptorinstance which contains information (intersection point as well as absolute and relative offsets of intersected segments) about intersection with specified segment. If segments do not intersect, returns null.- Parameters:
segment- - Specified segment.- Returns:
- IntersectionDescriptor with information about intersection or null if segments do not intersect
-
getParallelSegment
Returns a segment which is parallel to this segment and has a specified distance to this segment. If specified distance is positive, the returned segment will be located above and to the right of this one. Otherwise, if specified distance is negative, the returned segment will be located below and to the left of this one.- Parameters:
distance- - Distance of parallel segment.- Returns:
- A parallel segment.
-
getReversed
Returns the segment with begin and end point swapped- Returns:
- Reversed segment.
-
getRotated
Returns segment rotated to the specified angle around its begin point. In the resulting segment, begin point stays the same and end point is a result of rotation around the begin point to the specified angle.- Parameters:
angle- angle (in radians) to which the result segment will be rotated.- Returns:
- rotated segment
- See Also:
-
getRotatedAround
Returns segment rotated to the specified angle around the specified point (rotation center).- Parameters:
angle- angle (in radians) to which the result segment will be rotated.rotationCenter- point specifying the center of rotation.- Returns:
- rotated segment
- See Also:
-
getPointOnLineByRelativeOffset
Returns point on line of this segment by its relative offset from the begin point of this segment. The offset can be both positive and negative.- Parameters:
relativeOffset- specified relative offset of point.- Returns:
- point on line of this segment at distance of |relativeOffset * length of the segment|.
-
getSegmentOnLineByRelativeOffsets
public Segment getSegmentOnLineByRelativeOffsets(double beginPointRelativeOffset, double endPointRelativeOffset) Returns segment lying on the line of this segment located by the specified relative offsets measured from the begin point of this segment.If
endPointRelativeOffset > beginPointRelativeOffset, the resulting segment will have the same heading as this segment. IfendPointRelativeOffset < beginPointRelativeOffset, the resulting segment will have the opposite heading.- Parameters:
beginPointRelativeOffset- specified begin relative offsetendPointRelativeOffset- specified end relative offset- Returns:
- segment lying on the line of this segment located by the specified relative offsets
-
getPointOnLineByAbsoluteOffset
Returns point on line of this segment by its absolute offset from the begin point of this segment. The offset can be both positive and negative.- Parameters:
absoluteOffset- specified absolute offset of point- Returns:
- point on line of this segment at distance of |absoluteOffset|
-
getConnectingSegment
Returns the shortest segment directed from the current segment to the specified segment, or null if the two segments intersect. The returned segment touches both current and the specified segments, its distance is equal to the shortest distance between the current and the specified segments- Parameters:
otherSegment- - Specified other segment- Returns:
- Segment connecting the two segments, or null if the segments intersect
-
getSegmentOnLineByAbsoluteOffsets
public Segment getSegmentOnLineByAbsoluteOffsets(double beginPointAbsoluteOffset, double endPointAbsoluteOffset) Returns segment lying on the line of this segment located by the specified absolute offsets measured from the begin point of this segment.If
endPointAbsoluteOffset > beginPointAbsoluteOffset, the resulting segment will have the same heading as this segment. IfendPointAbsoluteOffset < beginPointAbsoluteOffset, the resulting segment will have the opposite heading.- Parameters:
beginPointAbsoluteOffset- specified begin absolute offsetendPointAbsoluteOffset- specified end absolute offset- Returns:
- segment lying on the line of this segment located by the specified absolute offsets
-
plus
Returns a segment having begin and end points coordinate-wise added to the specified point. In other words, returns a segment shifted by the coordinates of the specified point- Parameters:
point- specified point- Returns:
- segment shifted by the coordinates of the specified point
-
minus
Returns a segment having begin and end points coordinate-wise subtracted the specified point. In other words, returns a segment negatively shifted by the coordinates of the specified point- Parameters:
point- specified point- Returns:
- segment negatively shifted by the coordinates of the specified point
-
multiply
Returns a segment having all its coordinates multiplied by the specified factor- Parameters:
factor- specified factor- Returns:
- segment having all its coordinates multiplied by the specified factor
-
toString
-
hashCode
public int hashCode() -
equals
-
equals
Checks whether the other given segment is equal to this segment, i.e. if both begin and end points of the other segment equal correspondingly to begin and end points of this segment.The equality of the points is determined with respect to the default epsilon, that can be obtained by
Compare.getEpsilon().- Parameters:
segment- other given segment- Returns:
trueif the other given segment is equal to this segment,falseotherwise- See Also:
-
getCopy
Returns a copy of this segment.- Returns:
- copy of this segment
-
toCodeString
Description copied from class:AbstractGeometricPrimitiveReturns the string representing Java code that creates this primitive in its current state- Specified by:
toCodeStringin classAbstractGeometricPrimitive- Returns:
- Java code required to create this primitive in its current state
-