Resources Numerical accuracy Calculation policy

Viewports on a sheet

Works out the size and center of a viewport drawn on a sheet and the scale that fits a named view into it.

How the precision is kept

A viewport's width and height are absolute differences of the two picked corners and its center is their mean, in 64-bit paper units. The scale for a named view is the smaller of two quotients and is stored unrounded; what is printed is a scale label.

Calculations under this policy

2 calculations in 1 source file. The code is read from the released app when this page loads. It is the calculation people run, not a copy kept for this page.

commands.js CadCraft 0.8.2 64-bit 2 calculations Reviewed

Tests bin/test-cadcraft.mjs

Viewport from two corners

64-bit

The size and center of a new viewport, and what it shows.

w = |Δx|; h = |Δy|; center = (a + b) ÷ 2

Width and height are absolute coordinate differences and the center is the mean of the corners. A side under 1e-9 paper units (absolute) is refused. The model point and the scale the viewport starts with come from sheets.js aimViewport.

  • commands.js
  • lines 3383 to 3388
  • CadCraft 0.8.2
const w = Math.abs(b.x - a.x), h = Math.abs(b.y - a.y);
if (w < 1e-9 || h < 1e-9) { c.print(t('cmd.mview.viewport_needs_width_height')); return; }
const aim = aimViewport(c.doc, lay.paper.unit, w, h, modelExtents(c.doc));
if (!c.doc.layers.has('Viewports')) c.doc.addLayer({ name: 'Viewports', color: 8, plot: false });
const frz = [...c.doc.layers.values()].filter((l) => l.newVpFrozen).map((l) => l.name);   // layers frozen in new viewports (VP.2)
c.doc.change('MVIEW', () => c.doc.add({ type: 'viewport', layer: 'Viewports', color: null, x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, w, h, cx: aim.cx, cy: aim.cy, s: aim.s, ...(frz.length ? { frz } : {}) }));

Scale that fits a named view

64-bit

The viewport scale at which a saved view of the model fills the viewport.

s = min(viewport w ÷ view w, viewport h ÷ view h)

Two divisions and the smaller result, stored unrounded as the viewport's scale. The scale that is printed is a label from sheets.js scaleLabel.

  • commands.js
  • line 3445
  • CadCraft 0.8.2
const s = Math.min(vp.w / named.w, vp.h / named.h);

In detail

Method
Box from two corners; fit scale = min(width ratio, height ratio).
Number types
64-bit numbers.
Tolerances
1e-9 paper units, absolute: a viewport narrower or lower than this is refused.

Approach: 64-bit, direct

Every value is a 64-bit number from input to result. Formulas are closed form or a direct solve, and every comparison uses a stated tolerance.

Every policy that takes this approach