Skip to content

Commit

Permalink
format sources
Browse files Browse the repository at this point in the history
  • Loading branch information
pirxpilot committed Feb 7, 2024
1 parent c7fab28 commit e6ea3a7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 41 deletions.
10 changes: 5 additions & 5 deletions benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ suite('vis-why', function () {
// or switch to fixed number of iterations
// set('iterations', 500);

before(function() {
before(function () {
this.shortPolyline = require('../test/fixtures/short.json');
this.longPolyline = require('../test/fixtures/long.json');
});

bench('short', function() {
bench('short', function () {
simplify(this.shortPolyline, 5);
});

bench('long', function() {
bench('long', function () {
simplify(this.longPolyline, 10);
});

[1000, 5000, 10000, 30000].forEach(function(len) {
[1000, 5000, 10000, 30000].forEach(function (len) {
var polyline = usa.slice(-len);
bench('huge ' + len, function() {
bench('huge ' + len, function () {
simplify(polyline, len / 100);
});
});
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function calculate(poly, area) {
ts = { heap: heap(areaCompare, true) },
triangle,
trianglePrev,
a = poly[0], b, c = poly[1],
a = poly[0],
b, c = poly[1],
list = [];

// calculate areas
Expand Down Expand Up @@ -62,7 +63,7 @@ function eliminate(ts, limit, area) {
nextTriangle,
counter = ts.heap.size() - limit;

while(counter-- > 0) {
while (counter-- > 0) {
triangle = ts.heap.pop();
prevTriangle = triangle.prev;
nextTriangle = triangle.next;
Expand Down Expand Up @@ -91,7 +92,7 @@ function eliminate(ts, limit, area) {
function collect(triangle) {
var poly = [triangle.a];

while(true) {
while (true) {
poly.push(triangle.b);
if (!triangle.next) {
break;
Expand Down
110 changes: 77 additions & 33 deletions test/simplify.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
var { describe, it } = require('node:test');
var simplify = require('..');

describe('simplify', function() {
it('should not change short polylines', function() {
describe('simplify', function () {
it('should not change short polylines', function () {
simplify([]).should.have.length(0);
simplify([[0, 0], [1, 1]]).should.eql([[0, 0], [1, 1]]);
simplify([
[0, 0],
[1, 1]
]).should.eql([
[0, 0],
[1, 1]
]);
});

it('should work with small limits', function() {
simplify([[0, 0], [1, 1.5], [2, 2]], 2).should.eql([[0, 0], [2, 2]]);
it('should work with small limits', function () {
simplify([
[0, 0],
[1, 1.5],
[2, 2]
], 2).should.eql([
[0, 0],
[2, 2]
]);
});

it('should not change anything if limit is larger than poly length', function() {
simplify([[0, 0], [1, 1], [2, 2]], 4).should.eql([[0, 0], [1, 1], [2, 2]]);
it('should not change anything if limit is larger than poly length', function () {
simplify([
[0, 0],
[1, 1],
[2, 2]
], 4).should.eql([
[0, 0],
[1, 1],
[2, 2]
]);
});


it('should simplify longer polylines', function() {
it('should simplify longer polylines', function () {

var poly = [
[0, 0],
Expand All @@ -31,12 +52,17 @@ describe('simplify', function() {
[9, 9]
];

simplify(poly, 4).should.eql([[ 0, 0 ], [ 3, 3 ], [ 8, 5 ], [ 9, 9 ]]);
simplify(poly, 4).should.eql([
[0, 0],
[3, 3],
[8, 5],
[9, 9]
]);

});


it('should simplify longer polylines using custom properties', function() {
it('should simplify longer polylines using custom properties', function () {

function area(a, b, c) {
return Math.abs(
Expand All @@ -46,29 +72,29 @@ describe('simplify', function() {
}

var poly = [
{x: 0, y: 0, label: 'a'},
{x: 1, y: 0, label: 'b'},
{x: 1, y: 1, label: 'c'},
{x: 3, y: 1, label: 'd'},
{x: 3, y: 3, label: 'e'},
{x: 4, y: 4, label: 'f'},
{x: 5, y: 4, label: 'g'},
{x: 8, y: 5, label: 'h'},
{x: 8, y: 7, label: 'i'},
{x: 9, y: 9, label: 'j'}
{ x: 0, y: 0, label: 'a' },
{ x: 1, y: 0, label: 'b' },
{ x: 1, y: 1, label: 'c' },
{ x: 3, y: 1, label: 'd' },
{ x: 3, y: 3, label: 'e' },
{ x: 4, y: 4, label: 'f' },
{ x: 5, y: 4, label: 'g' },
{ x: 8, y: 5, label: 'h' },
{ x: 8, y: 7, label: 'i' },
{ x: 9, y: 9, label: 'j' }
];

simplify(poly, 4, area).should.eql([
{x: 0, y: 0, label: 'a'},
{x: 3, y: 3, label: 'e'},
{x: 8, y: 5, label: 'h'},
{x: 9, y: 9, label: 'j'}
{ x: 0, y: 0, label: 'a' },
{ x: 3, y: 3, label: 'e' },
{ x: 8, y: 5, label: 'h' },
{ x: 9, y: 9, label: 'j' }
]);

});


it('should keep the ends of the straight line', function() {
it('should keep the ends of the straight line', function () {

var poly = [
[-3, -3],
Expand All @@ -78,14 +104,20 @@ describe('simplify', function() {
[5, 5]
];

simplify(poly, 2).should.eql([[ -3, -3 ], [ 5, 5 ]]);
simplify(poly, 3).should.eql([[ -3, -3 ], [ 5, 5 ]]);
simplify(poly, 2).should.eql([
[-3, -3],
[5, 5]
]);
simplify(poly, 3).should.eql([
[-3, -3],
[5, 5]
]);
});

it('should remove duplicate stops', function () {
var poly = [
[-91.9655, 39.55001],
[-91.9655, 39.55001],
[-91.9655, 39.55001],
[-91.9655, 39.55001],
[-91.96581, 39.55024],
[-91.96596, 39.55041],
[-91.96599, 39.55053],
Expand All @@ -97,7 +129,7 @@ describe('simplify', function() {
];

simplify(poly, 9).should.eql([
[-91.9655, 39.55001],
[-91.9655, 39.55001],
[-91.96581, 39.55024],
[-91.96596, 39.55041],
[-91.96599, 39.55053],
Expand All @@ -109,15 +141,27 @@ describe('simplify', function() {
});


it('should simplify longer polyline', function() {
it('should simplify longer polyline', function () {
var poly = require('./fixtures/long.json');
var simplified = require('./fixtures/simplified-long.json');

simplify(poly, 40).should.eql(simplified);
});

it('should keep the first point', function () {
var poly = [[0, 0], [1, 0], [2, 0], [3, 1], [4, 2], [4, 4]];
simplify(poly, 4).should.eql([[0, 0], [2, 0], [4, 2], [4, 4]]);
var poly = [
[0, 0],
[1, 0],
[2, 0],
[3, 1],
[4, 2],
[4, 4]
];
simplify(poly, 4).should.eql([
[0, 0],
[2, 0],
[4, 2],
[4, 4]
]);
});
});

0 comments on commit e6ea3a7

Please sign in to comment.