Files
superGeometry 55f654073c A variable renamed everywhere; a distance between two lines; a sketch pattern that follows a count; fill patterns follow variables (asked for, of the gaps a user finds in week two: "fix them all").
- Variables > Rename: each variable's name in a box. A new name typed, every formula that used the old one says the new - other variables, steps' sizes, sketch dimensions, points placed by it, a pattern's count - and nothing changes size. Two can swap names at once; a name taken, a unit or a function is refused in plain words. (A variable could only be deleted and made again, and what used it lost its link.) tests/renamevariable.test.ts, 14 checks: the flange built on variables, three of them renamed, is the same flange and still follows them.
- Sketch: two lines picked that run the same way have a distance - shown in its box, typed, kept as a Distance constraint that holds them parallel and that far apart. (It was point to point and point to line only.) tests/linedistance.test.ts, 8 checks.
- A sketch pattern whose Count is typed with a variable or a formula is kept with the sketch: its copies are made again when the count comes to another number - "n" holes round a circle, floor(len / 10 mm) slots in a row. A count below 1 is said in plain words and the sketch left as it was. (A sketch pattern was its copies and nothing more.) tests/sketchrepeat.test.ts, 15 checks.
- Fill patterns are made again when a variable or a dimension resizes their shape, not only when the sketch is changed in the sketch tool.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-23 10:08:17 +02:00

62 lines
3.6 KiB
TypeScript

// Run: npx tsx tests/linedistance.test.ts
// A distance between two lines of a sketch: they run the same way, that far apart - measured here from the lines
// themselves. Offered for two lines that are (all but) parallel; two that cross have an angle, not a distance.
import type { SketchConstraint, SketchEntity, Vec2 } from "../src/doc/types";
import { applicable, solve } from "../src/sketch/constraints";
let failed = 0;
const check = (name: string, ok: boolean, info: unknown = "") => {
if (!ok) failed++;
console.log(ok ? "ok " : "FAIL", name, info);
};
const line = (id: string, a: Vec2, b: Vec2): SketchEntity => ({ id, kind: "line", a, b }) as SketchEntity;
type L = Extract<SketchEntity, { kind: "line" }>;
const apart = (es: SketchEntity[]) => {
const [l, m] = es as L[];
const d = [l.b[0] - l.a[0], l.b[1] - l.a[1]];
const e = [m.b[0] - m.a[0], m.b[1] - m.a[1]];
const L1 = Math.hypot(d[0], d[1]);
const off = (p: Vec2) => Math.abs((p[0] - l.a[0]) * d[1] - (p[1] - l.a[1]) * d[0]) / L1;
return { ends: [off(m.a), off(m.b)], turn: Math.abs(d[0] * e[1] - d[1] * e[0]) / (L1 * Math.hypot(e[0], e[1])) };
};
const A = line("a", [0, 0], [40, 0]);
const B = line("b", [5, 12], [35, 12]);
check("offered for two lines, not for a line and a circle", applicable("distance", [], [A, B]) && !applicable("distance", [], [A, { id: "c", kind: "circle", center: [0, 0], radius: 3 } as SketchEntity]));
for (const want of [20, 5, 0.5, 137]) {
const c: SketchConstraint = { id: "d", type: "distance", curves: ["a", "b"], value: want };
const r = solve([A, B], [c]);
const got = apart(r.entities);
check(`set to ${want}: both ends of the second that far from the first, still parallel`, r.ok && got.ends.every((x) => Math.abs(x - want) < 1e-6) && got.turn < 1e-8, `${got.ends.map((x) => x.toFixed(6))} turn ${got.turn.toExponential(1)}`);
}
// A slanted pair, and one a little out of parallel: brought parallel, and to the distance.
{
const P = line("p", [0, 0], [30, 30]);
const Q = line("q", [10, 0], [38, 29]);
const r = solve([P, Q], [{ id: "d", type: "distance", curves: ["p", "q"], value: 8 }]);
const got = apart(r.entities);
check("a slanted pair a little out of parallel, set to 8: parallel and 8 apart", r.ok && got.ends.every((x) => Math.abs(x - 8) < 1e-6) && got.turn < 1e-8, `${got.ends.map((x) => x.toFixed(6))}`);
}
// With the first line held: only the second moves.
{
const r = solve([A, B], [{ id: "d", type: "distance", curves: ["a", "b"], value: 30 }], { fixed: new Set(["a"]) });
const a = r.entities[0] as L;
check("the first held: it stays, the second goes to 30", r.ok && a.a[0] === 0 && a.a[1] === 0 && a.b[0] === 40 && a.b[1] === 0 && apart(r.entities).ends.every((x) => Math.abs(x - 30) < 1e-6));
}
// Beside other constraints: a rectangle's two sides held 15 apart by it, its other sizes free.
{
const es = [line("l1", [0, 0], [50, 0]), line("l2", [0, 20], [50, 20]), line("v", [0, 0], [0, 20])];
const cs: SketchConstraint[] = [
{ id: "h", type: "horizontal", curves: ["l1"] },
{ id: "j1", type: "coincident", pts: [{ e: "v", p: "a" }, { e: "l1", p: "a" }] },
{ id: "j2", type: "coincident", pts: [{ e: "v", p: "b" }, { e: "l2", p: "a" }] },
{ id: "d", type: "distance", curves: ["l1", "l2"], value: 15 },
];
const r = solve(es, cs);
const v = r.entities[2] as L;
check("joined by an upright between them: the two 15 apart, the upright still joined to both", r.ok && apart(r.entities).ends.every((x) => Math.abs(x - 15) < 1e-6) && Math.hypot(v.b[0] - (r.entities[1] as L).a[0], v.b[1] - (r.entities[1] as L).a[1]) < 1e-6);
}
console.log(failed ? `${failed} failed` : "all passed");
process.exit(failed ? 1 : 0);