Frame grid in drawing coordinates
64-bitThe frame's matrix, its inverse and the box of the whole paper in the drawing.
det = m₀·m₃ − m₁·m₂; inverse = adjugate ÷ det
The inverse is the closed form for a 2 by 3 matrix. Only a determinant of exactly zero is rejected, so a frame scaled to almost nothing is inverted with large factors. The paper box is the minimum and maximum of the four transformed corners.
/** A frame insert's grid in world coordinates: {cols, x0, x1, y0, y1, box: [x0,y0,x1,y1] (the whole paper), m, inv}. */
export function frameGrid(e, blk, meta = readMeta(blk)) {
if (!meta || meta.role !== 'frame') return null;
const n = (k, d) => (Number.isFinite(+meta[k]) ? +meta[k] : d);
const cols = Math.max(1, Math.min(100, Math.round(n('cols', 10))));
const m = insertM(e, blk);
const det = m[0] * m[3] - m[1] * m[2];
if (!det) return null;
const inv = [m[3] / det, -m[1] / det, -m[2] / det, m[0] / det, (m[2] * m[5] - m[3] * m[4]) / det, (m[1] * m[4] - m[0] * m[5]) / det];
const corners = [[0, 0], [n('w', 420), 0], [n('w', 420), n('h', 297)], [0, n('h', 297)]].map(([x, y]) => apply(m, x, y));
const xs = corners.map((p) => p.x), ys = corners.map((p) => p.y);
return { cols, gx0: n('gx0', 20), gx1: n('gx1', 410), gy0: n('gy0', 45), gy1: n('gy1', 282), m, inv, box: [Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys)] };
}