17 lines
616 B
JavaScript
17 lines
616 B
JavaScript
|
|
import { hash2 } from "./random.js";
|
||
|
|
|
||
|
|
export function pickEntities(candidates, { max = 99, minDistance = 5, threshold = 0, seed = 0, jitter = 0.04 } = {}) {
|
||
|
|
const sorted = candidates
|
||
|
|
.filter((p) => Number.isFinite(p.score) && p.score >= threshold)
|
||
|
|
.map((p) => ({ ...p, score: p.score + hash2(p.x, p.y, seed + 54321) * jitter }))
|
||
|
|
.sort((a, b) => b.score - a.score);
|
||
|
|
|
||
|
|
const out = [];
|
||
|
|
for (const candidate of sorted) {
|
||
|
|
if (out.every((p) => Math.hypot(p.x - candidate.x, p.y - candidate.y) >= minDistance)) {
|
||
|
|
out.push(candidate);
|
||
|
|
if (out.length >= max) break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|