Move ol.geom.flatLinearRingIsClockwise into ol.geom.flat

This commit is contained in:
Tom Payne
2013-11-12 12:44:13 +01:00
parent 857c28a88b
commit 0aa0cfd54b
3 changed files with 29 additions and 29 deletions

View File

@@ -173,6 +173,31 @@ ol.geom.flat.linearRingContainsXY =
}; };
/**
* @param {Array.<number>} 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.<number>} flatCoordinates Flat coordinates. * @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset. * @param {number} offset Offset.

View File

@@ -262,31 +262,6 @@ ol.geom.RawMultiLineString;
ol.geom.RawMultiPolygon; ol.geom.RawMultiPolygon;
/**
* @param {Array.<number>} 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.<number>} flatCoordinates Flat coordinates. * @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset. * @param {number} offset Offset.
@@ -299,7 +274,7 @@ ol.geom.orientFlatLinearRings =
var i, ii; var i, ii;
for (i = 0, ii = ends.length; i < ii; ++i) { for (i = 0, ii = ends.length; i < ii; ++i) {
var end = ends[i]; var end = ends[i];
var isClockwise = ol.geom.flatLinearRingIsClockwise( var isClockwise = ol.geom.flat.linearRingIsClockwise(
flatCoordinates, offset, end, stride); flatCoordinates, offset, end, stride);
var reverse = i === 0 ? !isClockwise : isClockwise; var reverse = i === 0 ? !isClockwise : isClockwise;
if (reverse) { if (reverse) {

View File

@@ -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() { it('identifies clockwise rings', function() {
var flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0]; 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); flatCoordinates, 0, flatCoordinates.length, 2);
expect(isClockwise).to.be(true); expect(isClockwise).to.be(true);
}); });
it('identifies anti-clockwise rings', function() { it('identifies anti-clockwise rings', function() {
var flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3]; 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); flatCoordinates, 0, flatCoordinates.length, 2);
expect(isClockwise).to.be(false); expect(isClockwise).to.be(false);
}); });