Exploit known sense of rings to simplify area calculation
This commit is contained in:
@@ -271,11 +271,11 @@ ol.geom.flat.linearRingArea = function(flatCoordinates, offset, end, stride) {
|
||||
for (; offset < end; offset += stride) {
|
||||
var x2 = flatCoordinates[offset];
|
||||
var y2 = flatCoordinates[offset + 1];
|
||||
twiceArea += x1 * y2 - y1 * x2;
|
||||
twiceArea += y1 * x2 - x1 * y2;
|
||||
x1 = x2;
|
||||
y1 = y2;
|
||||
}
|
||||
return Math.abs(twiceArea / 2);
|
||||
return twiceArea / 2;
|
||||
};
|
||||
|
||||
|
||||
@@ -383,13 +383,7 @@ ol.geom.flat.linearRingsArea = function(flatCoordinates, offset, ends, stride) {
|
||||
var i, ii;
|
||||
for (i = 0, ii = ends.length; i < ii; ++i) {
|
||||
var end = ends[i];
|
||||
var linearRingArea =
|
||||
ol.geom.flat.linearRingArea(flatCoordinates, offset, end, stride);
|
||||
if (i === 0) {
|
||||
area += linearRingArea;
|
||||
} else {
|
||||
area -= linearRingArea;
|
||||
}
|
||||
area += ol.geom.flat.linearRingArea(flatCoordinates, offset, end, stride);
|
||||
offset = end;
|
||||
}
|
||||
return area;
|
||||
|
||||
@@ -163,7 +163,7 @@ describe('ol.geom.flat', function() {
|
||||
|
||||
it('calculates the area with holes', function() {
|
||||
var area = ol.geom.flat.linearRingsArea(
|
||||
[0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 1, 2, 2, 2, 2, 1], 0, [8, 16], 2);
|
||||
[0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2], 0, [8, 16], 2);
|
||||
expect(area).to.be(8);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user