A carpenter has a list of 60 rectangles and a stack of 2800 × 2070 mm boards. "How many boards do I buy?" sounds like textbook bin packing, until you add the constraints of a real saw. The board optimiser of SyntaxFlow is a plain JavaScript file with no DOM and no dependencies. Here is what is in it and why.
The constraint that defines the problem: guillotine cuts
A beam saw or a sliding table saw cannot cut a hole in the middle of a board. Every cut runs from one edge of the piece on the saw to the opposite edge. A layout that respects this is called guillotine. A pinwheel arrangement (four rectangles around a central one) can be denser on paper and cannot be cut with a saw at all.
So the optimiser produces only layouts a saw can cut. There is a separate mode for CNC routers, which cut round each part; more on it at the end.
Kerf and trim, with one trick
The blade removes material: the kerf, typically 3 to 4 mm. Factory edges are rough, so the sheet loses a trim strip on its edges. A naive implementation subtracts a kerf after each part and gets the edges wrong. The engine uses a cleaner convention:
// every part is placed as (length + kerf) × (width + kerf)
var L = I(p.len) + K, W = I(p.wid) + K;
// into a usable area enlarged by one kerf in each direction
var UL = I(SL) - 2*I(trim) + K,
UW = I(SW) - 2*I(trimW) + K;
A part that touches the far edge needs no cut there, and every other boundary gets exactly one kerf. Inflating both parts and sheet by one kerf makes that rule fall out with no special cases. The trim can differ on the long edges (trimW) and the ends (trim), because some shops only square the ends.
Floating point is the other trap. The engine counts the decimals in every input and works in integers (F = 10^min(2, maxDecimals)), so sums never end in ...9999999.
Grain
On a veneered or wood-grain melamine board, the grain runs along the sheet's length. A part marked "grain" keeps its length along the sheet; anything else may turn 90°. On a plain sheet every part may turn. The check is a few lines:
var lock = grainSheet && !!p.grain;
var straight = L <= UL && W <= UW, across = W <= UL && L <= UW;
var fits = straight || (!lock && across);
I ran it: a 2000 × 2700 mm part on a 2800 × 2070 sheet fits only turned. With the grain flag it is refused with reason: "grain" instead of "size"; without it, it is placed and listed in totals.turned, so the page can say the part runs across the grain. Telling "too big" from "too big for this grain" saves a phone call.
The packing method
The core is guillotine bin packing in the style of Jylänki's survey "A Thousand Ways to Pack the Bin". The sheet is a list of free rectangles. To place a part you pick a free rectangle where it fits, put it in a corner, and split the leftover into two new free rectangles with one straight cut. Three decisions define a variant:
- Order of the parts. By area, longest side, shortest side, perimeter, length or width, largest first.
- Fit rule. Which free rectangle and orientation wins: best area fit, best short-side fit, best long-side fit.
- Split rule. After placing, does the cut run the full width or the full height? Six rules, from "shorter leftover axis" to "longer leftover axis" and area-based ones.
var FITS = ["baf", "bssf", "blsf"];
var SPLITS = ["slas", "llas", "minas", "maxas", "sas", "las"];
function place(sheet, b, it, split){
var fr = sheet.free[b.f];
var dw = fr.w - b.w, dh = fr.h - b.h, horizontal;
switch(split){
case "slas": horizontal = dw <= dh; break;
case "llas": horizontal = dw > dh; break;
// ... four more rules
}
var A, B;
if(horizontal){ A = { x: fr.x + b.w, y: fr.y, w: dw, h: b.h };
B = { x: fr.x, y: fr.y + b.h, w: fr.w, h: dh }; }
else { A = { x: fr.x + b.w, y: fr.y, w: dw, h: fr.h };
B = { x: fr.x, y: fr.y + b.h, w: b.w, h: dh }; }
}
Both children of the split are rectangles that a saw can reach with one more straight cut, which is what keeps every layout guillotine by construction.
Why one greedy pass fails
Six orderings × three fit rules × six split rules is 108 layouts. The optimiser runs all of them and keeps the best: fewest sheets first, and among ties the one that leaves the most of the last sheet whole (a big usable offcut beats the same area scattered).
Why not pick the best rule and run only that? Because no rule wins on every list: which one does depends on the particular numbers, and you cannot know in advance.
I measured this on the real file. I generated 1000 random lists (8 to 17 part types, 1 to 4 of each, on a 2800 × 2070 sheet, kerf 3, trim 10) and solved each in two ways: the quick mode, which the page offers for very long lists and which runs only 4 of the 108 layouts, and the full search. The full search bought fewer sheets in 149 of the 1000 lists. One of the smallest, 8 part types and 20 parts:
P0 1050×450 ×4 P1 1450×550 ×1 P2 1450×550 ×2 P3 650×300 ×1
P4 450×550 ×3 P5 1300×200 ×2 P6 1500×350 ×4 P7 750×600 ×3
quick (4 layouts): 3 sheets full (108 layouts): 2 sheets
The full search reaches 2 sheets, equal to the area lower bound: a third of the purchase saved. This experiment is mine, with a pseudo-random generator, not a benchmark on real jobs. The code's header mentions a script that measures utilisation on 222 real lists; I have not run it, so I quote nothing from it.
The honest part: a lower bound
A heuristic gives an answer but does not say how far from optimal it is. The engine returns, next to the number of sheets, a cheap lower bound from area alone:
lowerBound: Math.ceil((need - rackNeed) / (UL * UW) - 1e-9)
where need is the total area of all parts, each inflated by the kerf. In my 1000 lists the answer equalled the lower bound in 696. In the other 304, either the heuristic is a sheet off or the bound is too optimistic (rectangles never tile perfectly), and the numbers alone do not say which. Still, when plan and bound agree there is nothing left to look for.
Two refinements
Offcuts on hand. The engine opens the shop's leftovers before any new sheet, and keeps that answer only if it buys fewer sheets than the same job without them; otherwise good stock is just turned into smaller pieces.
CNC router mode. With no edge-to-edge constraint, the free space is kept as all maximal empty rectangles (overlapping) and a part may go in any of them. A free layout is kept only if it saves a sheet; otherwise the guillotine plan stays, since a saw can cut it too.
What I would take from this
- Model the machine first. "Guillotine" is a property of the saw, not of the algorithm.
- Inflate parts and sheet by one kerf and the edge rules disappear.
- Run many cheap heuristics and keep the best, rather than agonising over one.
- Always print a lower bound next to a heuristic answer.
You can try the optimiser at syntaxflow.io/cutting/; it takes a pasted list (length, width, quantity, grain) and shows the layouts per sheet.
I'm the author of SyntaxFlow, the set of workshop calculators this article is about.
Written with the help of an AI assistant; the code, the figures and the claims were checked against the repository before publishing.
Top comments (0)