Skip to content

Commit

Permalink
Merge pull request #1 from datavis-tech/publish
Browse files Browse the repository at this point in the history
Publish
  • Loading branch information
curran authored Jun 15, 2020
2 parents 7e9b63e + 8275b91 commit f13067c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polylabel",
"version": "1.0.5",
"name": "@datavis-tech/polylabel",
"version": "1.2.0",
"description": "A JS library for finding optimal label position inside a polygon",
"main": "polylabel.js",
"scripts": {
Expand Down
12 changes: 9 additions & 3 deletions polylabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ function polylabel(polygon, precision, debug) {
var cellSize = Math.min(width, height);
var h = cellSize / 2;

if (cellSize === 0) return [minX, minY];
if (cellSize === 0) {
var degeneratePoleOfInaccessibility = [minX, minY];
degeneratePoleOfInaccessibility.distance = 0;
return degeneratePoleOfInaccessibility;
}

// a priority queue of cells in order of their "potential" (max distance to polygon)
var cellQueue = new Queue(undefined, compareMax);
Expand Down Expand Up @@ -73,7 +77,9 @@ function polylabel(polygon, precision, debug) {
console.log('best distance: ' + bestCell.d);
}

return [bestCell.x, bestCell.y];
var poleOfInaccessibility = [bestCell.x, bestCell.y];
poleOfInaccessibility.distance = bestCell.d;
return poleOfInaccessibility;
}

function compareMax(a, b) {
Expand Down Expand Up @@ -107,7 +113,7 @@ function pointToPolygonDist(x, y, polygon) {
}
}

return (inside ? 1 : -1) * Math.sqrt(minDistSq);
return minDistSq === 0 ? 0 : (inside ? 1 : -1) * Math.sqrt(minDistSq);
}

// get polygon centroid
Expand Down
20 changes: 15 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,38 @@ var water2 = require('./fixtures/water2.json');

test('finds pole of inaccessibility for water1 and precision 1', function (t) {
var p = polylabel(water1, 1);
t.same(p, [3865.85009765625, 2124.87841796875]);
t.same(p, Object.assign([3865.85009765625, 2124.87841796875], {
distance: 288.8493574779127
}));
t.end();
});

test('finds pole of inaccessibility for water1 and precision 50', function (t) {
var p = polylabel(water1, 50);
t.same(p, [3854.296875, 2123.828125]);
t.same(p, Object.assign([3854.296875, 2123.828125], {
distance: 278.5795872381558
}));
t.end();
});

test('finds pole of inaccessibility for water2 and default precision 1', function (t) {
var p = polylabel(water2);
t.same(p, [3263.5, 3263.5]);
t.same(p, Object.assign([3263.5, 3263.5], {
distance: 960.5
}));
t.end();
});

test('works on degenerate polygons', function (t) {
var p = polylabel([[[0, 0], [1, 0], [2, 0], [0, 0]]]);
t.same(p, [0, 0]);
t.same(p, Object.assign([0, 0], {
distance: 0
}));

p = polylabel([[[0, 0], [1, 0], [1, 1], [1, 0], [0, 0]]]);
t.same(p, [0, 0]);
t.same(p, Object.assign([0, 0], {
distance: 0
}));

t.end();
});

0 comments on commit f13067c

Please sign in to comment.