Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jul 6, 2024
1 parent 8cc56a7 commit e79209e
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions polylabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export default function polylabel(polygon, precision = 1.0, debug = false) {
const cellSize = Math.max(precision, Math.min(width, height));

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

// a priority queue of cells in order of their "potential" (max distance to polygon)
const cellQueue = new Queue(undefined, compareMax);
const cellQueue = new Queue(undefined, (a, b) => b.max - a.max);

// take centroid as the first best guess
let bestCell = getCentroidCell(polygon);
Expand Down Expand Up @@ -59,31 +59,26 @@ export default function polylabel(polygon, precision = 1.0, debug = false) {

while (cellQueue.length) {
// pick the most promising cell from the queue
const cell = cellQueue.pop();
const {max, x, y, h: ch} = cellQueue.pop();

// do not drill down further if there's no chance of a better solution
if (cell.max - bestCell.d <= precision) break;
if (max - bestCell.d <= precision) break;

// split the cell into four cells
h = cell.h / 2;
potentiallyQueue(cell.x - h, cell.y - h, h);
potentiallyQueue(cell.x + h, cell.y - h, h);
potentiallyQueue(cell.x - h, cell.y + h, h);
potentiallyQueue(cell.x + h, cell.y + h, h);
h = ch / 2;
potentiallyQueue(x - h, y - h, h);
potentiallyQueue(x + h, y - h, h);
potentiallyQueue(x - h, y + h, h);
potentiallyQueue(x + h, y + h, h);
}

if (debug) {
console.log(`num probes: ${numProbes}`);
console.log(`best distance: ${bestCell.d}`);
console.log(`num probes: ${numProbes}\nbest distance: ${bestCell.d}`);
}

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

function compareMax(a, b) {
return b.max - a.max;
const result = [bestCell.x, bestCell.y];
result.distance = bestCell.d;
return result;
}

function Cell(x, y, h, polygon) {
Expand Down Expand Up @@ -142,7 +137,6 @@ function getSegDistSq(px, py, a, b) {
let dy = b[1] - y;

if (dx !== 0 || dy !== 0) {

const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);

if (t > 1) {
Expand Down

0 comments on commit e79209e

Please sign in to comment.