com.partnersoft.geometry.xy
Class XyLineSegment

java.lang.Object
  extended by com.partnersoft.geometry.xy.XyLineSegment
All Implemented Interfaces:
Immutable, XyPath<XyLineSegment>, XyShape<XyLineSegment>, java.lang.Iterable<XyPoint>

public class XyLineSegment
extends java.lang.Object
implements XyPath<XyLineSegment>

A line segment in (x, y) space. Has an explicit start and end point.

Copyright 2006-2007 Partner Software, Inc.

Version:
$Id: XyLineSegment.java 2474 2010-03-13 14:28:43Z paul $
Author:
Paul Reavis

Constructor Summary
XyLineSegment(double startX, double startY, double endX, double endY)
          Create a new line segment with the given starting and ending coordinates.
XyLineSegment(XyPoint start, XyPoint end)
          Create a new line segment starting and ending at the given points.
 
Method Summary
 XyLineSegment clippedTo(XyBounds bounds)
          Clip this line to the given bounds.
 XyPoint closestPointTo(XyPoint nother)
          Solves for the closest point to the segment.
 XyBounds getBounds()
          Returns an XyBounds object representing the rectangular extents of this shape.
 XyPoint getCenterPoint()
          Returns the midpoint, or center, of the segment.
 XyDirection getDirection()
          Returns the direction of the line segment, treating it as a vector from start to end.
 XyPoint getEndPoint()
          Returns the last point in the shape.
 double getLength()
          Returns the length of the path.
 XyLine getLine()
          The line this is a segment of.
 XyPoint getStartPoint()
          Returns the first point in the shape.
 java.util.Iterator<XyPoint> iterator()
           
 XyLineSegment scaledBy(double factor)
          Return a scaled version of the shape by multiplying all coordinates by the given factor in both dimensions.
 java.util.List<XyPoint> splitAtIntervals(double... intervals)
          Returns a list of points, determined by splitting this segment up using the sequence of intervals given.
 XyPolyline toPolyline()
          Same as XyPath.toPolyline(XyAngle) with a default angle granularity of 1/60 circle (6 degrees, pi/30 radians).
 XyPolyline toPolyline(XyAngle granularity)
          Converts this area to a polyline which approximates it in some fashion.
 XyVector toVector()
          Returns the vector equivalent for this line segment.
 XyShape transformedBy(XyTransform transform)
          Transform the shape using the given transform.
 XyLineSegment translatedBy(double offsetX, double offsetY)
          Translate the shape by adding all coordinates to the given x and y offsets.
 XyLineSegment withEndPoint(XyPoint newEnd)
          Returns a modified version of this line segment with the given end point.
 XyLineSegment withStartPoint(XyPoint newStart)
          Returns a modified version of this line segment with the given start point.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XyLineSegment

public XyLineSegment(XyPoint start,
                     XyPoint end)
Create a new line segment starting and ending at the given points.


XyLineSegment

public XyLineSegment(double startX,
                     double startY,
                     double endX,
                     double endY)
Create a new line segment with the given starting and ending coordinates.

Method Detail

withStartPoint

public XyLineSegment withStartPoint(XyPoint newStart)
Returns a modified version of this line segment with the given start point.


withEndPoint

public XyLineSegment withEndPoint(XyPoint newEnd)
Returns a modified version of this line segment with the given end point.


toVector

public XyVector toVector()
Returns the vector equivalent for this line segment. The vector is generated by subtracting the starting coordinates from the end coordinates.


closestPointTo

public XyPoint closestPointTo(XyPoint nother)
Solves for the closest point to the segment. This is either the perpendicular to the line the segment is part of (if the perpendicular is between the endpoints) or one of the endpoints (if not).


splitAtIntervals

public java.util.List<XyPoint> splitAtIntervals(double... intervals)
Returns a list of points, determined by splitting this segment up using the sequence of intervals given. These intervals will be repeated as needed for the length of the line. The results include the start and end of the segment and are appropriate for creating a polyline.


clippedTo

public XyLineSegment clippedTo(XyBounds bounds)
Clip this line to the given bounds. Returns null if the line does not intersect the bounds.


getLength

public double getLength()
Description copied from interface: XyPath
Returns the length of the path.

Specified by:
getLength in interface XyPath<XyLineSegment>
Returns:
geometric length of this shape

toPolyline

public XyPolyline toPolyline(XyAngle granularity)
Description copied from interface: XyPath
Converts this area to a polyline which approximates it in some fashion. For example, a curve can be converted to a polyline with enough segments to roughly approximate its shape. Paths consisting of linked line segments can be represented exactly.

The angle parameter allows a certain level of control of the output. Essentially it dictates the minimum angular granularity used to approximate curved paths. For example, an arc would be turned into a polyline by placing points around the arc's slice of a circle, separated by the given angle.

Specified by:
toPolyline in interface XyPath<XyLineSegment>

toPolyline

public XyPolyline toPolyline()
Description copied from interface: XyPath
Same as XyPath.toPolyline(XyAngle) with a default angle granularity of 1/60 circle (6 degrees, pi/30 radians).

Specified by:
toPolyline in interface XyPath<XyLineSegment>

getStartPoint

public XyPoint getStartPoint()
Description copied from interface: XyPath
Returns the first point in the shape.

Specified by:
getStartPoint in interface XyPath<XyLineSegment>

getEndPoint

public XyPoint getEndPoint()
Description copied from interface: XyPath
Returns the last point in the shape.

Specified by:
getEndPoint in interface XyPath<XyLineSegment>

iterator

public java.util.Iterator<XyPoint> iterator()
Specified by:
iterator in interface java.lang.Iterable<XyPoint>

getBounds

public XyBounds getBounds()
Description copied from interface: XyShape
Returns an XyBounds object representing the rectangular extents of this shape.

Specified by:
getBounds in interface XyShape<XyLineSegment>

scaledBy

public XyLineSegment scaledBy(double factor)
Description copied from interface: XyShape
Return a scaled version of the shape by multiplying all coordinates by the given factor in both dimensions. This resulting shape must be of the same class as the original.

Specified by:
scaledBy in interface XyShape<XyLineSegment>
Parameters:
factor - scaling factor
Returns:
scaled result

transformedBy

public XyShape transformedBy(XyTransform transform)
Description copied from interface: XyShape
Transform the shape using the given transform. Due to potential distortion the result may be a different type of shape than this one.

Specified by:
transformedBy in interface XyShape<XyLineSegment>
Parameters:
transform - affine transform to apply.
Returns:
transformed result

translatedBy

public XyLineSegment translatedBy(double offsetX,
                                  double offsetY)
Description copied from interface: XyShape
Translate the shape by adding all coordinates to the given x and y offsets. The resulting shape must be of the same class as the original.

Specified by:
translatedBy in interface XyShape<XyLineSegment>
Parameters:
offsetX - offset for the x dimension
offsetY - offset for the y dimension
Returns:
offset result

getLine

public XyLine getLine()
The line this is a segment of.

Returns:
line for this segment

getCenterPoint

public XyPoint getCenterPoint()
Returns the midpoint, or center, of the segment.


getDirection

public XyDirection getDirection()
Returns the direction of the line segment, treating it as a vector from start to end.