Add ol.geom.flat.linearRingArea
This commit is contained in:
@@ -257,6 +257,28 @@ ol.geom.flat.lineStringLength = function(flatCoordinates, offset, end, stride) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||||
|
* @param {number} offset Offset.
|
||||||
|
* @param {number} end End.
|
||||||
|
* @param {number} stride Stride.
|
||||||
|
* @return {number} Area.
|
||||||
|
*/
|
||||||
|
ol.geom.flat.linearRingArea = function(flatCoordinates, offset, end, stride) {
|
||||||
|
var twiceArea = 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];
|
||||||
|
twiceArea += x1 * y2 - y1 * x2;
|
||||||
|
x1 = x2;
|
||||||
|
y1 = y2;
|
||||||
|
}
|
||||||
|
return Math.abs(twiceArea / 2);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||||
* @param {number} offset Offset.
|
* @param {number} offset Offset.
|
||||||
|
|||||||
@@ -127,6 +127,20 @@ describe('ol.geom.flat', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('ol.geom.flat.linearRingArea', function() {
|
||||||
|
|
||||||
|
it('calcaultes the area of a triangle', function() {
|
||||||
|
var area = ol.geom.flat.linearRingArea([0, 0, 0.5, 1, 1, 0], 0, 6, 2);
|
||||||
|
expect(area).to.be(0.5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calculates the area of a unit square', function() {
|
||||||
|
var area = ol.geom.flat.linearRingArea([0, 0, 0, 1, 1, 1, 1, 0], 0, 8, 2);
|
||||||
|
expect(area).to.be(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('ol.geom.flat.linearRingIsClockwise', function() {
|
describe('ol.geom.flat.linearRingIsClockwise', function() {
|
||||||
|
|
||||||
it('identifies clockwise rings', function() {
|
it('identifies clockwise rings', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user