org.apache.batik.ext.awt.geom

Class PathLength

public class PathLength extends Object

Utilitiy class for length calculations of paths.

PathLength is a utility class for calculating the length of a path, the location of a point at a particular length along the path, and the angle of the tangent to the path at a given length.

It uses a FlatteningPathIterator to create a flattened version of the Path. This means the values returned are not always exact (in fact, they rarely are), but in most cases they are reasonably accurate.

Nested Class Summary
protected static classPathLength.PathSegment
A single path segment in the flattened version of the path.
protected static classPathLength.SingleSegmentPathIterator
A {@link PathIterator} that returns only the next path segment from another {@link PathIterator}.
Field Summary
protected booleaninitialised
Whether this path been flattened yet.
protected Shapepath
The path to use for calculations.
protected floatpathLength
Cached copy of the path length.
protected int[]segmentIndexes
Array where the index is the index of the original path segment and the value is the index of the first of the flattened segments in {@link #segments} that corresponds to that original path segment.
protected Listsegments
The list of flattened path segments.
Constructor Summary
PathLength(Shape path)
Creates a new PathLength object for the specified {@link Shape}.
Method Summary
floatangleAtLength(int index, float proportion)
Returns the slope of the path at the specified length.
floatangleAtLength(float length)
Returns the slope of the path at the specified length.
intfindUpperIndex(float length)
Returns the index of the path segment that bounds the specified length along the path.
floatgetLengthAtSegment(int index)
Returns the length at the start of the segment given by the specified index.
intgetNumberOfSegments()
Returns the number of segments in the path.
ShapegetPath()
Returns the path to use for calculations.
protected voidinitialise()
Flattens the path and determines the path length.
floatlengthOfPath()
Returns the length of the path used by this PathLength object.
Point2DpointAtLength(int index, float proportion)
Returns the point that is the given proportion along the path segment given by the specified index.
Point2DpointAtLength(float length)
Returns the point that is at the given length along the path.
intsegmentAtLength(float length)
Returns the index of the segment at the given distance along the path.
voidsetPath(Shape v)
Sets the path to use for calculations.

Field Detail

initialised

protected boolean initialised
Whether this path been flattened yet.

path

protected Shape path
The path to use for calculations.

pathLength

protected float pathLength
Cached copy of the path length.

segmentIndexes

protected int[] segmentIndexes
Array where the index is the index of the original path segment and the value is the index of the first of the flattened segments in {@link #segments} that corresponds to that original path segment.

segments

protected List segments
The list of flattened path segments.

Constructor Detail

PathLength

public PathLength(Shape path)
Creates a new PathLength object for the specified {@link Shape}.

Parameters: path The Path (or Shape) to use.

Method Detail

angleAtLength

public float angleAtLength(int index, float proportion)
Returns the slope of the path at the specified length.

Parameters: index The segment number proportion The proportion along the given segment

Returns: the angle in radians, in the range [-{@link Math#PI}, {@link Math#PI}].

angleAtLength

public float angleAtLength(float length)
Returns the slope of the path at the specified length.

Parameters: length The length along the path

Returns: the angle in radians, in the range [-{@link Math#PI}, {@link Math#PI}].

findUpperIndex

public int findUpperIndex(float length)
Returns the index of the path segment that bounds the specified length along the path.

Parameters: length The length along the path

Returns: The path segment index, or -1 if there is not such segment

getLengthAtSegment

public float getLengthAtSegment(int index)
Returns the length at the start of the segment given by the specified index.

getNumberOfSegments

public int getNumberOfSegments()
Returns the number of segments in the path.

getPath

public Shape getPath()
Returns the path to use for calculations.

Returns: Path used in calculations.

initialise

protected void initialise()
Flattens the path and determines the path length.

lengthOfPath

public float lengthOfPath()
Returns the length of the path used by this PathLength object.

Returns: The length of the path.

pointAtLength

public Point2D pointAtLength(int index, float proportion)
Returns the point that is the given proportion along the path segment given by the specified index.

pointAtLength

public Point2D pointAtLength(float length)
Returns the point that is at the given length along the path.

Parameters: length The length along the path

Returns: The point at the given length

segmentAtLength

public int segmentAtLength(float length)
Returns the index of the segment at the given distance along the path.

setPath

public void setPath(Shape v)
Sets the path to use for calculations.

Parameters: v Path to be used in calculations.

Copyright B) 2007 Apache Software Foundation. All Rights Reserved.