[145] clampInside nur noch im Kern (Bindung grip_clamp_inside), TS-Doppelung entfernt; Fassade in src/core

This commit is contained in:
Claude committed 2026-11-09 13:02:30 +00:00
1 parent e7b4f8b2fe
commit cc6de499ba
5 files changed
+30 -27

No files matched your search

+6 -2
View File
@@ -1,6 +1,7 @@
// [103] Dachfenster bleiben beim Ziehen auf der Dachfläche (`clampInside`).
// [103]/[145] Dachfenster bleiben beim Ziehen auf der Dachfläche; `clampInside` rechnet der Kern
// (`opening_slide::clamp_inside`), dieselbe Klemmung wie beim Verschieben-Befehl.
import { describe, expect, it } from "vitest";
import { clampInside } from "./surfaceOpening";
import { clampInside } from "../core";
const sq = [{ x: 0, y: 0 }, { x: 4, y: 0 }, { x: 4, y: 3 }, { x: 0, y: 3 }];
@@ -14,4 +15,7 @@ describe("clampInside", () => {
expect(q.x).toBeGreaterThan(3.9);
expect(q.y).toBeCloseTo(1.5, 1);
});
it("zu wenige Ecken: unverändert", () => {
expect(clampInside({ x: 9, y: 9 }, sq.slice(0, 2))).toEqual({ x: 9, y: 9 });
});
});
+1 -24
View File
@@ -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;
}