Skip to content

Commit 2e25230

Browse files
authored
Merge pull request #171 from varvedesign/feat/vectorcraft-followup
Offset Path: miter limit follows the join width
2 parents fd6da3e + 5e064b7 commit 2e25230

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ update, not for someone reading the commit log.
4242
write itself fails, the document stays modified, keeps its recovery point and
4343
says the work is still unsaved. Previously only the status line changed and
4444
closing could discard the document.
45+
- **Offset Path miter limit** — the limit was compared against the edge length
46+
instead of the join width, so long edges never clipped and acute spikes
47+
survived a miter limit of 4. It now follows the SVG/PostScript definition.
4548
- **Offset Path** — rounded joins now round only the outside of a corner, so an
4649
inward offset no longer scoops material out of every corner. Work is bounded
4750
by a chord tolerance instead of the entered distance, so a very large offset

‎docs/audits/vectorcraft/findings.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ than relabelled.
9494
point.
9595
- **Validation:** `packages/engine/src/pathOffset.test.ts` (16 cases, 4 new).
9696
Visual check recorded in `continuation.md`.
97+
- **Follow-up resolved:** the miter limit divided the miter length by the
98+
*edge length*, which is dimensionally wrong and scale-dependent: a long edge
99+
could never clip, so an acute spike survived `miterLimit=4`. It now uses the
100+
SVG/PostScript definition (distance from the original vertex to the miter tip
101+
over the offset distance), and a test proves the decision depends on the
102+
angle, not the edge length.
97103
- **Not covered:** VectorCraft's hang was inside their geometry library's
98104
sweep; Varve has no sweep phase, so the specific non-terminating
99105
intersection loop does not exist here. The bounded-work property is what

‎packages/engine/src/pathOffset.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,34 @@ describe('roundCorners', () => {
261261
}
262262
});
263263
});
264+
265+
describe('miter limit semantics', () => {
266+
/** Closed triangle with a ~5.7 degree apex at (50, 0): ratio ~20. */
267+
const spike = [
268+
{ x: 50, y: 0, handleIn: null, handleOut: null },
269+
{ x: 60, y: 200, handleIn: null, handleOut: null },
270+
{ x: 40, y: 200, handleIn: null, handleOut: null },
271+
];
272+
273+
it('clips an acute miter once the ratio exceeds the limit', () => {
274+
// ratio = 1 / sin(2.86 deg) ~ 20, so miterLimit 4 must bevel the corner
275+
// instead of letting the spike run past the offset edge.
276+
const result = offsetPath(spike, true, 10, 'miter', 4);
277+
expect(Math.min(...result.map((p) => p.y))).toBeGreaterThan(-10);
278+
});
279+
280+
it('keeps the same miter when the limit allows it', () => {
281+
const result = offsetPath(spike, true, 10, 'miter', 100);
282+
expect(Math.min(...result.map((p) => p.y))).toBeLessThan(-100);
283+
});
284+
285+
it('decides from the angle, not the edge length', () => {
286+
// Uniform scaling preserves the angles, so the decision must not change.
287+
// The pre-fix ratio divided by edge length, so longer edges never clipped.
288+
const scaled = spike.map((p) => ({ ...p, x: p.x * 5, y: p.y * 5 }));
289+
const short = offsetPath(spike, true, 10, 'miter', 4);
290+
const long = offsetPath(scaled, true, 10, 'miter', 4);
291+
expect(Math.min(...short.map((p) => p.y))).toBeGreaterThan(-10);
292+
expect(Math.min(...long.map((p) => p.y))).toBeGreaterThan(-10);
293+
});
294+
});

‎packages/engine/src/pathOffset.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,15 @@ export function offsetPath(
223223
: null;
224224

225225
if (int) {
226-
// Check miter ratio
227-
const edgeLen = vecLen(curr.bx - curr.ax, curr.by - curr.ay);
228-
const miterLen = vecLen(int[0] - curr.ax, int[1] - curr.ay);
229-
const ratio = edgeLen > 0 ? miterLen / edgeLen : 0;
226+
// Miter ratio, as SVG/PostScript define it: the distance from the
227+
// original vertex to the miter tip over the join width (the offset
228+
// distance). Dividing by the edge length instead made the ratio
229+
// dimensionally wrong and scale-dependent — a long edge could never
230+
// clip, so an acute spike survived miterLimit=4.
231+
const vx = curr.ax - curr.nx * distance;
232+
const vy = curr.ay - curr.ny * distance;
233+
const miterLen = vecLen(int[0] - vx, int[1] - vy);
234+
const ratio = miterLen / Math.abs(distance);
230235
// A miter beyond the limit is clipped to a bevel corner.
231236
result.push(
232237
ratio <= miterLimit

0 commit comments

Comments
 (0)