From 4b13627fd716e97dc2665dbd21e4087470b82565 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Thu, 12 Mar 2015 10:18:39 +0100 Subject: [PATCH] Add new ol.geom.LineString#forEachSegment function --- src/ol/geom/linestring.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 3477285f3c..3a89e81de5 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -11,6 +11,7 @@ goog.require('ol.geom.flat.inflate'); goog.require('ol.geom.flat.interpolate'); goog.require('ol.geom.flat.intersectsextent'); goog.require('ol.geom.flat.length'); +goog.require('ol.geom.flat.segments'); goog.require('ol.geom.flat.simplify'); @@ -107,6 +108,23 @@ ol.geom.LineString.prototype.closestPointXY = }; +/** + * Iterate over each segment, calling the provided callback. + * If the callback returns a truthy value the function returns that + * value immediately. Otherwise the function returns `false`. + * + * @param {function(ol.Coordinate, ol.Coordinate): T} callback Function + * called for each segment. + * @return {T|boolean} Value. + * @template T + * @api + */ +ol.geom.LineString.prototype.forEachSegment = function(callback) { + return ol.geom.flat.segments.forEach(this.flatCoordinates, 0, + this.flatCoordinates.length, this.stride, callback); +}; + + /** * Returns the coordinate at `m` using linear interpolation, or `null` if no * such coordinate exists.