// Display geometry only; no script state or motion facts are derived here. export const INITIAL_UNITS_PER_PIXEL = .04; const validBox = (box: number[]) => box.length === 4 && box.every(Number.isFinite) && box[2] > 0 && box[3] > 0; export function viewportScale(box: number[], rect: {width: number; height: number}, previous: number): number { if (!validBox(box) || !Number.isFinite(rect.width) || !Number.isFinite(rect.height) || rect.width <= 0 || rect.height <= 0) return previous; const next = Math.max(box[2] / rect.width, box[3] / rect.height); return Number.isFinite(next) && next > 0 ? next : previous; } export function panBox(box: number[], dx: number, dy: number, scale: number): number[] { if (!validBox(box) || ![dx, dy, scale].every(Number.isFinite) || scale <= 0) return box; const next = [box[0] - dx * scale, box[1] - dy * scale, box[2], box[3]]; return validBox(next) ? next : box; } export function zoomBox(box: number[], worldWidth: number, factor: number, center?: [number, number]): number[] { if (!validBox(box) || ![worldWidth, factor].every(Number.isFinite) || worldWidth <= 0 || factor <= 0 || (center && !center.every(Number.isFinite))) return box; const width = Math.min(worldWidth * 2, Math.max(worldWidth / 12, box[2] * factor)); const ratio = width / box[2], cx = center?.[0] ?? box[0] + box[2] / 2, cy = center?.[1] ?? box[1] + box[3] / 2; const next = [cx - (cx - box[0]) * ratio, cy - (cy - box[1]) * ratio, width, box[3] * ratio]; return validBox(next) ? next : box; }