Resources Numerical accuracy Calculation policy

Pasted picture size

Sizes a pasted picture, or a copied area pasted at its true size, in drawing units.

How the precision is kept

A picture's scale is one 64-bit division, drawing width ÷ pixel width, stored as the picture's two axis vectors. A copied area's size in millimeters is turned into drawing units with a factor built from the drawing's units. Pixel counts are whole numbers.

Calculations under this policy

3 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.

pastepic.js CadCraft 0.8.2 64-bit 3 calculations Reviewed

Tests bin/test-cadcraft.mjs

Picture scale

64-bit

Drawing units per pixel of a pasted picture.

units per pixel = W ÷ pixel width

One 64-bit division; the result is stored as both axis vectors (ux and vy), so pixels are square. With no width given the picture is one drawing unit per pixel. The fade value is rounded to a whole percent between 0 and 100.

  • pastepic.js
  • lines 57 to 60
  • CadCraft 0.8.2
const W = width > 0 ? width : rec.w;
const px = W / rec.w;
const f = Math.max(0, Math.min(100, Math.round(fade) || 0));
const pic = { file: name, x: 0, y: 0, ux: px, uy: 0, vx: 0, vy: px, w: rec.w, h: rec.h, ...(f ? { fade: f } : {}), ...(transparent ? { transparent: true } : {}) };

Drawing units per millimeter

64-bit

The factor that turns a size in millimeters into the drawing's units.

units per mm = units per point × 72 ÷ 25.4

unitScale (pdfmodel.js) gives drawing units per PDF point from the drawing's units, for example 25.4 ÷ 72 for millimeters and 1 ÷ 72 for inches. It is multiplied by 72 and then divided by 25.4, three rounded operations in all.

  • pastepic.js
  • line 104
  • CadCraft 0.8.2
export const unitsPerMm = (doc) => unitScale(doc) * 72 / 25.4;

Size of a copied area

64-bit

The width and height a copied area takes in the drawing, on paper or at its real size.

size = size in mm × units per mm

The area's width and height in millimeters (areaclip.js sizeMm) are each multiplied once by the factor above. Without a scale on the copied sheet the real size is not known and nothing is returned.

  • pastepic.js
  • lines 106 to 111
  • CadCraft 0.8.2
export function areaSize(doc, clip, use = 'real') {
  const mm = sizeMm(clip, { use });
  if (!mm) return null;
  const k = unitsPerMm(doc);
  return [mm[0] * k, mm[1] * k];
}

In detail

Method
units per pixel = width ÷ pixel width. Copied area: size in mm × (drawing units per PDF point × 72 ÷ 25.4).
Number types
64-bit numbers; pixel counts are integers.

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