hmm
This commit is contained in:
parent
112e6bf86b
commit
f2d0306d96
13 changed files with 1434 additions and 516 deletions
|
|
@ -95,6 +95,61 @@ export function sampledNetworkCells(paths, step = 2, sea = null) {
|
|||
return cells;
|
||||
}
|
||||
|
||||
export function pathEndpoints(paths) {
|
||||
const endpoints = [];
|
||||
for (let pathId = 0; pathId < (paths || []).length; pathId++) {
|
||||
const path = paths[pathId];
|
||||
if (!path || path.length < 2) continue;
|
||||
endpoints.push({ x: path[0][0], y: path[0][1], pathId });
|
||||
const end = path[path.length - 1];
|
||||
endpoints.push({ x: end[0], y: end[1], pathId });
|
||||
}
|
||||
return endpoints;
|
||||
}
|
||||
|
||||
export function nearestNetworkPoint(source, targets, radius, options = {}) {
|
||||
if (!source || !targets?.length) return null;
|
||||
const excludeSamePath = options.excludeSamePath !== false;
|
||||
const excludePath = options.excludePath;
|
||||
let best = null;
|
||||
let bestD = radius + 1;
|
||||
for (const target of targets) {
|
||||
if (excludePath != null && target.pathId === excludePath) continue;
|
||||
if (excludeSamePath && source.pathId != null && target.pathId === source.pathId) continue;
|
||||
const d = Math.hypot(source.x - target.x, source.y - target.y);
|
||||
if (d > 0.01 && d < bestD) {
|
||||
bestD = d;
|
||||
best = target;
|
||||
}
|
||||
}
|
||||
return best ? { target: best, d: bestD, ...best } : null;
|
||||
}
|
||||
|
||||
export function splitPathToValidCells(path, isValid, minCells = 2) {
|
||||
const chunks = [];
|
||||
let current = [];
|
||||
function pushPoint(x, y) {
|
||||
if (!isValid(x, y)) {
|
||||
if (current.length >= minCells) chunks.push(current);
|
||||
current = [];
|
||||
return;
|
||||
}
|
||||
if (!current.length || current[current.length - 1][0] !== x || current[current.length - 1][1] !== y) current.push([x, y]);
|
||||
}
|
||||
for (let k = 0; k < (path?.length || 0); k++) {
|
||||
const a = path[k];
|
||||
const b = path[Math.min(k + 1, path.length - 1)];
|
||||
const steps = Math.max(1, Math.ceil(Math.hypot(b[0] - a[0], b[1] - a[1])));
|
||||
for (let s = 0; s <= steps; s++) {
|
||||
if (k > 0 && s === 0) continue;
|
||||
const t = s / steps;
|
||||
pushPoint(Math.round(a[0] + (b[0] - a[0]) * t), Math.round(a[1] + (b[1] - a[1]) * t));
|
||||
}
|
||||
}
|
||||
if (current.length >= minCells) chunks.push(current);
|
||||
return chunks;
|
||||
}
|
||||
|
||||
export function pathCumulativeLengths(path) {
|
||||
const cum = [0];
|
||||
for (let k = 1; k < (path?.length || 0); k++) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue