Skip to content

Commit 3dd3c3c

Browse files
Natalia Kowalczykpirxpilot
Natalia Kowalczyk
authored andcommitted
fix elimination of empty triangles
first point of triangle that is eliminated should always be kept - we want to always keep the first and last points of the simplified polyline
1 parent c628cc8 commit 3dd3c3c

File tree

4 files changed

+388
-4
lines changed

4 files changed

+388
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
components
22
build
33
node_modules
4+
.project
5+
.externalToolBuilders

index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ function calculate(poly) {
2020
ts = { heap: heap(areaCompare, true) },
2121
triangle,
2222
trianglePrev,
23-
a = poly[0], b = poly[1], c,
23+
a = poly[0], b, c = poly[1],
2424
list = [];
2525

2626
// calculate areas
2727
for (i = 2; i < poly.length; i++) {
28+
b = c;
2829
c = poly[i];
2930
triangle = {
3031
a: a,
@@ -35,11 +36,10 @@ function calculate(poly) {
3536
prev: trianglePrev,
3637
_heapIndex: 0
3738
};
38-
a = b;
39-
b = c;
4039
if (!triangle.area) {
4140
continue;
4241
}
42+
a = b;
4343
if (trianglePrev) {
4444
trianglePrev.next = triangle;
4545
}

test/simplify.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@ describe('simplify', function() {
6565
[-91.96509, 39.55763]
6666
];
6767

68-
simplify(poly, 9).should.have.length(7);
68+
simplify(poly, 9).should.eql([
69+
[-91.9655, 39.55001],
70+
[-91.96581, 39.55024],
71+
[-91.96596, 39.55041],
72+
[-91.96599, 39.55053],
73+
[-91.96588, 39.55320],
74+
[-91.96581, 39.55578],
75+
[-91.96573, 39.55764],
76+
[-91.96509, 39.55763]
77+
]);
6978
});
7079

7180

@@ -75,4 +84,9 @@ describe('simplify', function() {
7584

7685
simplify(poly, 40).should.eql(simplified);
7786
});
87+
88+
it('should keep the first point', function () {
89+
var poly = [[0, 0], [1, 0], [2, 0], [3, 1], [4, 2], [4, 4]];
90+
simplify(poly, 4).should.eql([[0, 0], [2, 0], [4, 2], [4, 4]]);
91+
});
7892
});

0 commit comments

Comments
 (0)