From 0aa0cfd54b65b0e94b398c308fe90a478c7da45a Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 12 Nov 2013 12:44:13 +0100 Subject: [PATCH] Move ol.geom.flatLinearRingIsClockwise into ol.geom.flat --- src/ol/geom/flatgeom.js | 25 +++++++++++++++++++++++++ src/ol/geom/geometry.js | 27 +-------------------------- test/spec/ol/geom/flatgeom.test.js | 6 +++--- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index d9bf1489d8..de64c9aab4 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -173,6 +173,31 @@ ol.geom.flat.linearRingContainsXY = }; +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {boolean} Is clockwise. + */ +ol.geom.flat.linearRingIsClockwise = + function(flatCoordinates, offset, end, stride) { + // http://tinyurl.com/clockwise-method + // https://github.com/OSGeo/gdal/blob/trunk/gdal/ogr/ogrlinearring.cpp + var edge = 0; + var x1 = flatCoordinates[end - stride]; + var y1 = flatCoordinates[end - stride + 1]; + for (; offset < end; offset += stride) { + var x2 = flatCoordinates[offset]; + var y2 = flatCoordinates[offset + 1]; + edge += (x2 - x1) * (y2 + y1); + x1 = x2; + y1 = y2; + } + return edge > 0; +}; + + /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index 4a1f5e7946..6b08a90098 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -262,31 +262,6 @@ ol.geom.RawMultiLineString; ol.geom.RawMultiPolygon; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {boolean} Is clockwise. - */ -ol.geom.flatLinearRingIsClockwise = - function(flatCoordinates, offset, end, stride) { - // http://tinyurl.com/clockwise-method - // https://github.com/OSGeo/gdal/blob/trunk/gdal/ogr/ogrlinearring.cpp - var edge = 0; - var x1 = flatCoordinates[end - stride]; - var y1 = flatCoordinates[end - stride + 1]; - for (; offset < end; offset += stride) { - var x2 = flatCoordinates[offset]; - var y2 = flatCoordinates[offset + 1]; - edge += (x2 - x1) * (y2 + y1); - x1 = x2; - y1 = y2; - } - return edge > 0; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. @@ -299,7 +274,7 @@ ol.geom.orientFlatLinearRings = var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - var isClockwise = ol.geom.flatLinearRingIsClockwise( + var isClockwise = ol.geom.flat.linearRingIsClockwise( flatCoordinates, offset, end, stride); var reverse = i === 0 ? !isClockwise : isClockwise; if (reverse) { diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index d66d695b4b..04bd728c07 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -54,18 +54,18 @@ describe('ol.geom.flat', function() { }); - describe('ol.geom.flatLinearRingIsClockwise', function() { + describe('ol.geom.flat.linearRingIsClockwise', function() { it('identifies clockwise rings', function() { var flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0]; - var isClockwise = ol.geom.flatLinearRingIsClockwise( + var isClockwise = ol.geom.flat.linearRingIsClockwise( flatCoordinates, 0, flatCoordinates.length, 2); expect(isClockwise).to.be(true); }); it('identifies anti-clockwise rings', function() { var flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3]; - var isClockwise = ol.geom.flatLinearRingIsClockwise( + var isClockwise = ol.geom.flat.linearRingIsClockwise( flatCoordinates, 0, flatCoordinates.length, 2); expect(isClockwise).to.be(false); });