Graphics pass: calm space backdrop, refined BH/stars, label solver
This commit is contained in:
82
web/src/lib/galaxyLabelLayout.ts
Normal file
82
web/src/lib/galaxyLabelLayout.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import type { Vec2 } from "./galaxyLayout";
|
||||
|
||||
export type LabelPlacement = {
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
|
||||
const CHAR_W = 6.4;
|
||||
const LABEL_H = 14;
|
||||
const LABEL_PAD = 6;
|
||||
|
||||
function labelWidth(name: string): number {
|
||||
const n = name.length > 16 ? 15 : name.length;
|
||||
return n * CHAR_W;
|
||||
}
|
||||
|
||||
function dist(a: LabelPlacement, b: LabelPlacement): number {
|
||||
return Math.hypot(a.x - b.x, a.y - b.y);
|
||||
}
|
||||
|
||||
/** Radial outward labels with simple bbox separation (lock mode). */
|
||||
export function layoutLockLabels(
|
||||
starPositions: Vec2[],
|
||||
names: string[],
|
||||
starR: number,
|
||||
): LabelPlacement[] {
|
||||
const vaultPad = 52;
|
||||
const labels: LabelPlacement[] = starPositions.map((p, i) => {
|
||||
const ang = Math.atan2(p.y, p.x);
|
||||
const base = starR + 20;
|
||||
return {
|
||||
x: p.x + Math.cos(ang) * base,
|
||||
y: p.y + Math.sin(ang) * base,
|
||||
};
|
||||
});
|
||||
|
||||
for (let iter = 0; iter < 28; iter++) {
|
||||
for (let i = 0; i < labels.length; i++) {
|
||||
const p = starPositions[i];
|
||||
const r = dist(labels[i], { x: 0, y: 0 });
|
||||
if (r < vaultPad) {
|
||||
const ang = Math.atan2(labels[i].y, labels[i].x);
|
||||
labels[i].x = Math.cos(ang) * vaultPad;
|
||||
labels[i].y = Math.sin(ang) * vaultPad;
|
||||
}
|
||||
|
||||
for (let j = i + 1; j < labels.length; j++) {
|
||||
const wi = labelWidth(names[i]);
|
||||
const wj = labelWidth(names[j]);
|
||||
const dx = labels[j].x - labels[i].x;
|
||||
const dy = labels[j].y - labels[i].y;
|
||||
const needX = (wi + wj) / 2 + LABEL_PAD;
|
||||
const needY = LABEL_H + LABEL_PAD;
|
||||
if (Math.abs(dx) < needX && Math.abs(dy) < needY) {
|
||||
const push = (needX - Math.abs(dx) + needY - Math.abs(dy)) * 0.35;
|
||||
const ang = Math.atan2(dy, dx) || 0;
|
||||
labels[i].x -= Math.cos(ang) * push;
|
||||
labels[i].y -= Math.sin(ang) * push;
|
||||
labels[j].x += Math.cos(ang) * push;
|
||||
labels[j].y += Math.sin(ang) * push;
|
||||
}
|
||||
}
|
||||
|
||||
const outward = Math.atan2(
|
||||
labels[i].y - starPositions[i].y,
|
||||
labels[i].x - starPositions[i].x,
|
||||
);
|
||||
const minDist = starR + 16;
|
||||
const dStar = dist(labels[i], starPositions[i]);
|
||||
if (dStar < minDist) {
|
||||
labels[i].x = starPositions[i].x + Math.cos(outward) * minDist;
|
||||
labels[i].y = starPositions[i].y + Math.sin(outward) * minDist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return labels;
|
||||
}
|
||||
|
||||
export function shortenOrbitName(name: string): string {
|
||||
return name.length > 16 ? `${name.slice(0, 14)}…` : name;
|
||||
}
|
||||
Reference in New Issue
Block a user