Move ol.geom.flatLinearRingIsClockwise into ol.geom.flat
This commit is contained in:
@@ -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 {number} offset Offset.
|
||||
|
||||
@@ -262,31 +262,6 @@ ol.geom.RawMultiLineString;
|
||||
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 {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) {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user