Arc and circle as straight segments
64-bitThe ground segments for one traced line, arc or circle.
aₖ = a₀ + sweep × k ÷ n; pₖ = [cx + r·cos aₖ, cy + r·sin aₖ, 0]
Each angle is computed from the start angle and the index, not by adding a step repeatedly. A point shared by two segments is computed by the same expression for both, so the two are identical. A sweep of zero or less has 2π added.
/** Ground segments [[x, y, 0], [x, y, 0]] for one traced entity (lines; arcs and circles as short straight runs). */
export function segmentsOf(e) {
if (e.type === 'line') return [[[e.x1, e.y1, 0], [e.x2, e.y2, 0]]];
const full = e.type === 'circle', a0 = full ? 0 : e.a0;
let sweep = full ? Math.PI * 2 : e.a1 - e.a0;
if (sweep <= 0) sweep += Math.PI * 2;
const n = Math.max(2, Math.ceil(sweep / (Math.PI * 2 / ARC_SEGS)));
const pt = (k) => { const a = a0 + (sweep * k) / n; return [e.cx + e.r * Math.cos(a), e.cy + e.r * Math.sin(a), 0]; };
const out = [];
for (let k = 0; k < n; k++) out.push([pt(k), pt(k + 1)]);
return out;
}