[145] clampInside nur noch im Kern (Bindung grip_clamp_inside), TS-Doppelung entfernt; Fassade in src/core
This commit is contained in:
5 files changed
+30
-27
No files matched your search
@@ -4,6 +4,7 @@
|
||||
|
||||
import type { Project } from "../model/types";
|
||||
import { documentFor } from "../core/document";
|
||||
import { clampInside } from "../geometry/gripDrag";
|
||||
|
||||
type V3 = [number, number, number];
|
||||
type P = { x: number; y: number };
|
||||
@@ -63,27 +64,3 @@ export function surfaceOpeningGripPatch(
|
||||
const r = Math.round(v * 1000) / 1000;
|
||||
return index === 1 ? { width: r } : { height: r };
|
||||
}
|
||||
|
||||
/** Punkt im Umriss lassen: ausserhalb → nächster Randpunkt, 5 cm nach innen gerückt (zum Schwerpunkt der Ecken). */
|
||||
export function clampInside(pt: P, ring: readonly P[]): P {
|
||||
let inside = false;
|
||||
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
||||
const a = ring[i], b = ring[j];
|
||||
if (a.y > pt.y !== b.y > pt.y && pt.x < ((b.x - a.x) * (pt.y - a.y)) / (b.y - a.y) + a.x) inside = !inside;
|
||||
}
|
||||
if (inside) return { x: pt.x, y: pt.y };
|
||||
let best = { x: ring[0].x, y: ring[0].y };
|
||||
let bestD = Infinity;
|
||||
for (let i = 0; i < ring.length; i++) {
|
||||
const a = ring[i], b = ring[(i + 1) % ring.length];
|
||||
const dx = b.x - a.x, dy = b.y - a.y;
|
||||
const l2 = dx * dx + dy * dy;
|
||||
const t = l2 > 0 ? Math.max(0, Math.min(1, ((pt.x - a.x) * dx + (pt.y - a.y) * dy) / l2)) : 0;
|
||||
const q = { x: a.x + dx * t, y: a.y + dy * t };
|
||||
const dd = (q.x - pt.x) ** 2 + (q.y - pt.y) ** 2;
|
||||
if (dd < bestD) { bestD = dd; best = q; }
|
||||
}
|
||||
const cx = ring.reduce((s, q) => s + q.x, 0) / ring.length, cy = ring.reduce((s, q) => s + q.y, 0) / ring.length;
|
||||
const l = Math.hypot(cx - best.x, cy - best.y);
|
||||
return l > 0.05 ? { x: best.x + ((cx - best.x) / l) * 0.05, y: best.y + ((cy - best.y) / l) * 0.05 } : best;
|
||||
}
|
||||
Reference in new issue
Block a user