along method
Returns the Point at the distance
(in meters) along the LineString.
If the distance
is greater than the length of the LineString,
the last Point in the LineString will be returned.
If the distance
is less than 0, the first Point in the LineString will be returned.
Example:
LineString([Coordinate(1, 2), Coordinate(3, 4)]).along(100); // Point(Coordinate(2, 3))
Implementation
Point along(double distance) {
if (distance <= 0) {
return Point(coordinates.first);
} else if (distance >= length) {
return Point(coordinates.last);
} else if (coordinates.length < 2) {
return Point(coordinates.first);
}
if (coordinates.length > 2) {
double distanceLeft = distance;
for (int i = 0; i < coordinates.length - 1; i++) {
final c1 = coordinates[i];
final c2 = coordinates[i + 1];
final segmentLength = c1.distanceTo(c2);
if (distanceLeft <= segmentLength) {
final ratio = distanceLeft / segmentLength;
return Point(c1.interpolate(c2, ratio));
} else {
distanceLeft -= segmentLength;
}
}
}
final ratio = distance / length;
return Point(coordinates.first.interpolate(coordinates.last, ratio));
}