Skip to content

Vectors

A layout calculates a width and a height, a left and a top, a column and a row. Often the math of the two dimensions is the same. Then write it once, with a vector. Two expressions for the two dimensions double the code, and the two copies can become different.

  • When you calculate the same value for x and y, use one Vec2, not two numbers.
  • Keep a dimension separate only where its math is different. For example, the number of columns of a grid comes from the width only.
  • When the components have names, or when there are more than two, use an NDVector.

The grid pilot shows the rule in a real layout.

Need Builder IR
A vector from two numbers x._.vec2(y) app("vec2", x, y)
A component of a vector v._.x() and v._.y() app("x", v) and app("y", v)
A vector of the data a field with a Vec2 value ref("size")
A literal new Vec2(1, 2) lit(new Vec2(1, 2))

The op vec2 is an op of NumDomain, and its receiver is the x component. Put Vec2Domain in the domains of the program too. It has the ops of the result, and it checks the result. A component that is not finite gives #NUM!.

In the JSON form of a program, a literal vector is { "$vex": "domain", "domain": "Vec2", "value": [1, 2] }.

The ops add, subtract, multiply, divide, min and max of Vec2Domain work on each component. A number argument of these ops becomes a vector with that number twice. Thus size._.max(1) makes a size of at least 1 by 1, and cell._.add(gap) adds the gap in the two dimensions.

floor, ceil, round, abs, sign, clamp, scale, negate and select work on each component too. dot, length, distance, area, sum, minComponent and maxComponent give a number. The domains page lists each op.

The reductions sum(), mean(), min() and max() of a list take only numbers. A list of vectors needs reduce with an op of the domain. The op of each item comes from the domain of that item.

  • reduce("max") gives the component-wise maximum, and reduce("min") gives the component-wise minimum.
  • reduce("add") gives the sum. An empty list gives the identity of the op, for example (0, 0) for add.
  • max has no identity, so an empty list gives an error.

The cell of a grid is the component-wise maximum of the sizes of the items, at least 1 by 1:

const items = vex(NumDomain, Vec2Domain).over(space.array([{ w: 10, h: 20 }, { w: 30, h: 5 }]));
const cell = items.start(0).each((t) => t.from("w")._.vec2(t.from("h"))).reduce("max")._.max(1);
cell.at("0"); // => some(new Vec2(30, 20))

The corner of the item in column 1 and row 0 is the vector (1, 0) times the cell and the gap:

const items = vex(NumDomain, Vec2Domain).over(space.array([{ w: 10, h: 20 }, { w: 30, h: 5 }]));
const cell = items.start(0).each((t) => t.from("w")._.vec2(t.from("h"))).reduce("max")._.max(1);
const corner = items.start(1)._.vec2(0)._.multiply(cell._.add(10));
corner.at("0"); // => some(new Vec2(40, 0))
corner._.x().at("0"); // => some(40)

A stack, a span or a row of keys and values has a main axis and a cross axis. The axis dir is the vector (1, 0) for a row and (0, 1) for a column. Four ops of Vec2Domain let one program serve the two directions:

Op Value
v._.along(dir) The component of v along dir: dot(v, dir)
v._.across(dir) The component of v on the other axis: dot(v, swap(dir))
dir._.compose(main, cross) The vector with main along dir and cross on the other axis
mask._.select(a, b) The component of a where mask is not 0, else the component of b

The content of a stack is the sum of the sizes along dir, and the largest size across dir. For a column, the width is the largest width, and the height is the sum of the heights. One program gives the two stacks:

const parts = space.tree(
{ column: { dir: new Vec2(0, 1) }, row: { dir: new Vec2(1, 0) }, a: { size: new Vec2(60, 18) }, b: { size: new Vec2(145, 90) }, c: { size: new Vec2(60, 18) }, d: { size: new Vec2(145, 90) } },
{ a: "column", b: "column", c: "row", d: "row" },
);
const stacks = vex(NumDomain, Vec2Domain).over(parts);
const dir = stacks.from("dir");
const sizes = stacks.start(0).children((c) => c.from("size"));
const content = dir._.compose(sizes.reduce("add")._.along(dir), sizes.reduce("max")._.across(dir));
content.at("column"); // => some(new Vec2(145, 108))
content.at("row"); // => some(new Vec2(205, 90))

A component of a Vec2 must be finite, so a bound cannot be infinite. For an open bound, use max or min for one side, and select for one component. The mask (0, 1) bounds only the height, and the width has no bound:

const parts = space.tree({ column: { dir: new Vec2(0, 1) }, a: { size: new Vec2(60, 18) }, b: { size: new Vec2(145, 90) } }, { a: "column", b: "column" });
const stacks = vex(NumDomain, Vec2Domain).over(parts);
const sizes = stacks.start(0).children((c) => c.from("size"));
const content = stacks.from("dir")._.compose(sizes.reduce("add")._.along("dir"), sizes.reduce("max")._.across("dir"));
const capped = stacks.start(new Vec2(0, 1))._.select(content._.min(100), content);
capped.at("column"); // => some(new Vec2(145, 100))

With a mask of bounds, a 0 means no bound: hi._.select(v._.min(hi), v) bounds each component of v by hi, except where hi has a 0.

A force separation pushes two boxes apart along the axis of their smallest overlap, with the sign of their gap. Two ops of Vec2Domain give the parts of the push:

Op Value
v._.sign() The sign of each component: 1, -1 or 0
v._.minAxis() (1, 0) if x <= y, else (0, 1). A tie gives (1, 0)

minAxis compares the signed values. For the sizes, write v._.abs()._.minAxis(). A component NaN gives #NUM!.

const pairs = vex(NumDomain, Vec2Domain).over(space.array([
{ gap: new Vec2(3, 1), half: new Vec2(5, 8) },
{ gap: new Vec2(1, -6), half: new Vec2(5, 8) },
]));
const gap = pairs.from("gap");
const overlap = pairs.from("half")._.subtract(gap._.abs());
const push = overlap._.minAxis()._.multiply(gap._.sign())._.multiply(overlap);
push.at("0"); // => some(new Vec2(2, 0))
push.at("1"); // => some(new Vec2(0, -2))

The sign of 0 is 0, so a gap of 0 on that axis gives no push. If two boxes have the same center, give the gap a direction first.

emitModule of @mark1russell7/vex-compiler prints a program as a module without Vex. A module has no import, so it holds a Vec2 as plain { x, y } data, the data of toJSON. Each op of Vec2Domain gives the bits of the interpreter. A module takes plain data or Vec2 values, and it gives plain data. Put NumDomain before Vec2Domain. XYDomain is a second domain of plain { x, y } data:

  • The op xy of NumDomain makes a value of XYDomain. Its declaration returns: "domain:XY" gives the module the kind of each vector.
  • No op of XYDomain lifts a number. Write max(xy(1, 1)), not max(1).
  • A module has reduce with a literal op name. reduce("max") gives the component-wise maximum.
  • Put XYDomain before NumRecordDomain, because a record of numbers accepts the same objects.
import { app, axes, fold, lit, ref, v } from "@mark1russell7/vex";
import { emitModule } from "@mark1russell7/vex-compiler/module";
import { BoolDomain, NumDomain, XYDomain } from "@mark1russell7/vex-domains";
const size = app("xy", ref("w"), ref("h"));
const cell = app("max", fold(axes.all, { c: app("xy", lit(0), lit(0)) }, { c: app("max", v("c"), size) }, v("c")), app("xy", lit(1), lit(1)));
const m = emitModule(cell, { domains: [NumDomain, XYDomain, BoolDomain], schema: { fields: { w: "number", h: "number" } } });
m.ok; // => true

InsetsDomain keeps the four sides of an inset as plain data: { startX, startY, endX, endY }. The op insets of NumDomain makes a value. start and end give the leading pair and the trailing pair as XY values, so a module can copy them. startVec2 and endVec2 give the same pairs as Vec2 values, but a module refuses them.

A number in the argument of add becomes four equal sides. Thus insets(0, 0, 0, 0).add(pad) takes a number or a value, with no test of the type. A box inset adds start to the corner, and it subtracts start and end from the size. scale(-1) gives the outset.

import { app, evaluate, lit, ref, space } from "@mark1russell7/vex";
import { InsetsDomain, NumDomain, XYDomain } from "@mark1russell7/vex-domains";
const domains = [NumDomain, XYDomain, InsetsDomain];
const sides = app("add", app("insets", lit(0), lit(0), lit(0), lit(0)), ref("pad"));
const corner = app("add", ref("at"), app("start", sides));
const size = app("subtract", app("subtract", ref("size"), app("start", sides)), app("end", sides));
const boxes = space.array([{ at: { x: 10, y: 20 }, size: { x: 100, y: 50 }, pad: 8 }]);
evaluate(corner, { space: boxes, origin: "0", domains }); // => { ok: true, value: { x: 18, y: 28 } }
evaluate(size, { space: boxes, origin: "0", domains }); // => { ok: true, value: { x: 84, y: 34 } }

An NDVector has a name for each component, for example width and height. Its ops work on the union of the names, and a component that one vector does not have counts as 0.

  • NumRecordDomain makes a vector from a record of numbers: toNDVector.
  • get gives a component. In the builder, a bare text argument is the name of a field, so write get(root.lit("width")).
  • add, subtract, multiply, min, max, scale and dot work on each component.
const sizes = vex(NumDomain, NDVectorDomain, NumRecordDomain).over(space.array([{ w: 10, h: 20 }, { w: 30, h: 5 }]));
const largest = sizes.start(0).each((t) => sizes.start(sizes.rec({ width: t.from("w"), height: t.from("h") }))._.toNDVector()).reduce("max");
largest._.get(sizes.lit("width")).at("0"); // => some(30)

A vector is one example of a value with its own ops. When a program uses a value with its own ops and laws, a domain gives the value one name. Examples are a rectangle with union and intersect, an interval, or an amount of money with a currency. The law checker of @mark1russell7/vex-testkit tests the laws that each op claims. Refer to the law arena.