The grid pilot
Vex came from a layout engine. This pilot writes one layout strategy, the grid, as one Vex program.
The grid layout
Section titled “The grid layout”The grid layout puts the items in rows, from left to right:
- The cell is the size of the largest item. An item without a size counts as 0, and the cell is at least 1 by 1.
- The configuration can set the number of columns. Without it, the number of columns is the number of cells that fit in the width of the bounds. The limit is the square root of the number of items, rounded up.
- Each item starts at the top-left corner of its cell. An item without a size gets the size of the cell.
One formula for each item
Section titled “One formula for each item”The Vex program evaluates at each item. Two values come from all the items: the cell and the number of items. The axis each reads them. The slot of the item comes from an extension, like ROW() in a spreadsheet.
The math of the width and the math of the height are the same. Thus the program calculates them once, with a vector of Vec2Domain:
- The op
vec2makes a vector from two numbers, and the opsxandygive its components. reduce("max")gives the component-wise maximum of the vectors. The opmaxof a list takes only numbers.- An op of
Vec2lifts a number to a vector with that number twice. Thusmax(1)makes the cell at least 1 by 1.
const cell = root.start(0).each((t) => t.from("width").ifError(0)._.vec2(t.from("height").ifError(0))) .reduce("max")._.max(1);const pitch = cell._.add(gap);const count = root.start(0).each((t) => t).count();const cols = root.start(bounds.width + gap)._.divide(pitch._.x())._.floor()._.max(1) ._.min(count._.sqrt()._.ceil())._.max(1);const i = root.start(root.ext<number>("slot"));const corner = i._.mod(cols)._.vec2(i._.divide(cols)._.floor())._.multiply(pitch);
root.start(root.rec({ left: corner._.x(), top: corner._.y(), width: root.from("width").ifError(cell._.x()), height: root.from("height").ifError(cell._.y()),}));The corner of an item is its column and its row, as one vector, times the pitch. Only the number of columns uses one dimension: it comes from the width of the bounds. Vectors tells more about this style.
One test has four items of 100 by 100, in bounds of 400 by 400, with a gap of 10:
import { gridVex } from "@vex/pilots";
const items = [{ width: 100, height: 100 }, { width: 100, height: 100 }, { width: 100, height: 100 }, { width: 100, height: 100 }];gridVex(items, { width: 400, height: 400 }, { gap: 10 }).map((r) => [r.left, r.top]); // => [[0, 0], [110, 0], [0, 110], [110, 110]]Try it
Section titled “Try it”Change the sliders. The chip compares the rectangles of the Vex program with the rectangles of a plain TypeScript implementation of the same rules.
7 rectangles from a program of 211 nodes. A dashed item has no size, so it gets the size of the cell.
What the pilot shows
Section titled “What the pilot shows”- It fits. The grid layout is two reductions and one formula for each item. The axes and the domains of numbers and vectors express it without new parts. The only addition is the extension
slot. - It is equal. A property test gives random items, bounds and configurations to both implementations, and compares the rectangles.
- It is slower. The program evaluates the two reductions again at each item, so its cost grows with the square of the number of items. A sheet with one column for each reduction, or the
compilestep of the plan, removes this cost. - Not each layout fits. A packed layout places each item after the items before it. That is an ordered fold with a state, not a formula for each item. Vex does not try to express it.