Public
Edited
Dec 2, 2023
Paused
Importers
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function distanceGrid(maze, startPos) {
let [sRow, sCol] = [0, 0];
if (startPos !== undefined) {
[sRow, sCol] = startPos;
} else if (maze.srt) {
[sRow, sCol] = [maze.getStart().row, maze.getStart().col];
}

const distances = maze.grid.map((row) => Array(row.length).fill(Infinity));
const startRow = maze.getStart(sRow, sCol).row;
const startCol = maze.getStart(sRow, sCol).col;

// Breadth first search from the startng position
let queue = [maze.grid[startRow][startCol]];
distances[startRow][startCol] = 0;
while (queue.length > 0) {
const c = queue.shift();
for (let linked of c.links) {
if (distances[linked.row]?.[linked.col] === Infinity) {
distances[linked.row][linked.col] = distances[c.row][c.col] + 1;
queue.push(linked);
}
}
}
return distances;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function composeLongest(m, opt) {
const defaults = { r: new Renderer(300, 300), cShortest: composeShortest };
opt = Object.assign({}, opt, defaults);

const furthest = (ds) => {
let [maxD, maxPos] = [ds[0][0], [0, 0]];
for (let row = 0; row < m.nRows; row++) {
for (let col = 0; col < m.grid[row].length; col++) {
if (ds[row][col] > maxD) {
maxD = ds[row][col];
maxPos = [row, col];
}
}
}
return maxPos;
};

// Find the longest distance from some arbitrary start point:
const cellA = furthest(distanceGrid(m, [0, 0]));
const cellB = furthest(distanceGrid(m, cellA));
return opt.cShortest(m, { ...opt, start: cellA, end: cellB });
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more