VectorPathMeasure Class

The chords of a path, computed once, to answer many questions about its length, its points and its directions: the position of a marker that moves along the path, or the point a pointer snaps to as it moves.

public sealed class VectorPathMeasure

Remarks

The answers are those of VectorPath.GetLength, VectorPath.TryGetPointAtDistance and VectorPath.TryGetClosestPoint, bit for bit: the path is measured by the chords it is drawn with, within 1/16 of a pixel of its curves, a closed contour includes its closing segment, and the contours follow each other without the gap between them. Those methods walk the whole path at each call; a measure walks it once, when it is created, then finds a point at a distance by a binary search, and the closest point by skipping the groups of chords that are farther than the nearest one found.

A path cannot change once it is built: a measure keeps answering for the path it was created from (VectorPathMeasure.Path), and is thread-safe. It holds 48 bytes per chord.

Constructors

NameDescription
VectorPathMeasure(VectorPath)Initializes a new instance of the VectorPathMeasure class: measures a path.

VectorPathMeasure(VectorPath)

VectorPathMeasure.cs

Initializes a new instance of the VectorPathMeasure class: measures a path.

public VectorPathMeasure(VectorPath path)

Parameters

path VectorPath
The path.

Exceptions

ArgumentNullException
path is null.

Properties

NameDescription
LengthGets the length of the path: what VectorPath.GetLength gives.
PathGets the path that is measured.

Gets the length of the path: what VectorPath.GetLength gives.

public double Length { get; }

Property value

double

Gets the path that is measured.

public VectorPath Path { get; }

Property value

VectorPath

Methods

NameDescription
TryGetClosestPoint(PointD, PointD, double)Finds the point of the path that is the nearest to a position, and its distance along the path.
TryGetPointAtDistance(double, PointD, PointD)Finds the point at a distance from the start of the path, and the direction of the path there.

TryGetClosestPoint(PointD, PointD, double)

VectorPathMeasure.cs

Finds the point of the path that is the nearest to a position, and its distance along the path.

public bool TryGetClosestPoint(PointD point, out PointD closest, out double distanceAlongPath)

Parameters

point PointD
The position.
closest PointD
The nearest point of the path.
distanceAlongPath double
The distance from the start of the path to that point, along the path, in pixels.

Returns

bool false when the path has no length: there is no point to find.

Exceptions

ArgumentException
A coordinate of point is not a finite number.

Remarks

The answer is the one of VectorPath.TryGetClosestPoint: when several points are as near, the first one along the path is given. The groups of chords whose box is farther than the nearest point found are skipped: the time grows with the number of chords near the position, and with the number of groups.

TryGetPointAtDistance(double, PointD, PointD)

VectorPathMeasure.cs

Finds the point at a distance from the start of the path, and the direction of the path there.

public bool TryGetPointAtDistance(double distance, out PointD point, out PointD tangent)

Parameters

distance double
The distance from the start of the path, in pixels.
point PointD
The point of the path at that distance.
tangent PointD
The direction of the path at that point, as a vector of length 1.

Returns

bool false when the path has no length: there is no point to find, and no direction.

Exceptions

ArgumentException
distance is not a number.

Remarks

The answer is the one of VectorPath.TryGetPointAtDistance: a distance before the start gives the first point, a distance beyond the end the last one, and at a corner the direction is the one of the segment that starts there. The time grows with the logarithm of the number of chords.