Resources Numerical accuracy Calculation policy

Drawing audit: zero-size and duplicate objects

Finds lines of zero length, circles and arcs of zero radius, and objects that are exact copies of another, so they can be listed and erased.

How the precision is kept

Zero size is decided with a fixed limit of 1e-9 drawing units. Duplicates are decided by comparing every stored property as text, so two objects are duplicates only when every number is identical to the last bit.

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.

problems-cadcraft.js CadCraft 0.8.2 64-bit 2 calculations Reviewed

Tests bin/test-problems.mjs

Zero-size test

64-bit

Says whether a line has no length or a circle or arc no radius.

line: |p₂ − p₁| ≤ 1e-9; circle or arc: r ≤ 1e-9

The limit 1e-9 is in drawing units and is not scaled to the drawing: in a drawing whose real features are that small, real objects would be reported. Only x and y are measured for a line. A negative radius also counts as zero-size.

  • problems-cadcraft.js
  • lines 37 to 43
  • CadCraft 0.8.2
/** Is this entity a zero-length line / zero-radius circle or arc? */
export function isEmpty(e) {
  if (!e || typeof e !== 'object') return false;
  if (e.type === 'line') return num(e.x1) && num(e.y1) && num(e.x2) && num(e.y2) && Math.hypot(e.x2 - e.x1, e.y2 - e.y1) <= tol;
  if (e.type === 'circle' || e.type === 'arc') return num(e.r) && e.r <= tol;
  return false;
}

Duplicate test

Exact

A text key that two objects share only when they are the same shape on the same layer.

Numbers are compared through their JSON text, which gives back the same 64-bit number, so there is no tolerance: any difference in any stored number makes two objects different.

  • problems-cadcraft.js
  • lines 44 to 49
  • CadCraft 0.8.2
/** A string that two entities share only when they are the same shape on the same layer (the id never counts). */
export function shapeKey(e) {
  const o = {};
  for (const k of Object.keys(e).sort()) { if (k === 'id' || k === 'key' || k === 'handle') continue; o[k] = e[k]; }
  try { return JSON.stringify(o); } catch (err) { return null; }
}

In detail

Method
Length by hypot; duplicates by a key built from all properties except the id, key and handle.
Number types
64-bit numbers.
Tolerances
tol = 1e-9 drawing units, absolute: a line this short or shorter, or a radius this small or smaller, is zero-size.
Limits
The limit does not scale with the drawing's units or size. Two objects that differ in the last digit of one coordinate are not duplicates; a line drawn in the opposite direction is not a duplicate either.

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