class Geometry · Computations

Grids

Computations on grids: creating grids (equal cells, from weights or from size wishes), finding the cell of a point, binning and counting points, the cells and lines as shapes, focus + context, and carrying points and shapes from one grid to another.

Cells are half-open: a point on a line belongs to the cell to the right of it (or below it), except on the last line, which belongs to the last cell. Points outside the grid have no cell (−1).

Static methods

binstatic

static bin(grid: Grid, points: Sequence<Vector> | VectorsLike, result?: GridCell[] | null): GridCell[]

Returns the cell of every point (binning).

grid
The grid.
points
The points; plain vertices such as [x, y] work too.
result optional
Optional list to store the result in; its cells are reused.

Returns One cell per point, in the order of the points; row and column −1 for points outside.

cellstatic

static cell(grid: Grid, cell: GridCell, result?: Box | null): Box

Returns a cell of the grid as a box.

grid
The grid.
cell
The cell.
result optional
Optional box to store the result in.

Returns The box of the cell; all coordinates NaN if the cell is not in the grid.

cellAtstatic

static cellAt(grid: Grid, index: number, result?: GridCell | null): GridCell

Returns the cell at a position in the order of cells: row by row from the top left.

grid
The grid.
index
The position, from 0 to rows × columns − 1.
result optional
Optional cell to store the result in.

Returns The cell, or row and column −1 if the index is outside.

cellOfstatic

static cellOf(grid: Grid, point: Point | VectorLike, result?: GridCell | null): GridCell

Returns the cell that contains a point.

The grid is not reactive: in Reactive.do, list what the grid is computed from (for example the box) and the point as senders.

grid
The grid.
point
The point.
result optional
Optional cell to store the result in.

Returns The cell, or row and column −1 if the point is outside the grid.

cellsstatic

static cells(grid: Grid, result?: Box[] | null): Box[]

Returns all cells of the grid as boxes, row by row from the top left.

With result, its boxes are reused and written to (so visuals made from them follow); boxes are added or removed at the end to match the number of cells, so for a fixed pool of visuals that is larger than the grid, use cell per cell instead (with cellAt). The grid is not reactive: in Reactive.do, list what the grid is computed from (for example the box) as senders.

grid
The grid.
result optional
Optional list to store the result in.

Returns One box per cell: rows × columns boxes.

columnOfstatic

static columnOf(grid: Grid, x: Real | NumberLike): number

Returns the column that contains an x position.

grid
The grid.
x
The x position.

Returns The column, or −1 if x is outside the grid or NaN.

columnsstatic

static columns(grid: Grid): number

Returns the number of columns of the grid (vertical lines − 1, at least 0).

grid
The grid.

Returns The number of columns.

containsCellstatic

static containsCell(grid: Grid, cell: GridCell): boolean

Determines whether a cell belongs to the grid, for example a result of cellOf or bin.

grid
The grid.
cell
The cell.

Returns True if row and column are within the grid; false for −1 ("no cell") and anything outside.

containsPointstatic

static containsPoint(grid: Grid, point: Point | VectorLike): boolean

Determines whether a point lies in the grid (on its border counts as inside).

grid
The grid.
point
The point.

Returns True if the point is in a cell; false outside, for NaN, or if the grid has no cells.

countsstatic

static counts(grid: Grid, points: Sequence<Vector> | VectorsLike, result?: number[][] | null): number[][]

Counts the points in every cell.

grid
The grid.
points
The points (plain vertices such as [x, y] work too); points outside the grid are not counted.
result optional
Optional array to store the result in.

Returns The counts as counts[row][column].

dividedstatic

static divided(box: Box | ShapeLike, columns: Integer | NumberLike, rows: Integer | NumberLike, result?: Grid | null): Grid

Creates a grid that divides a box into equal columns and rows.

box
The area the grid covers.
columns
The number of columns; less than 1 gives no vertical lines.
rows
The number of rows; less than 1 gives no horizontal lines.
result optional
Optional grid to store the result in; its line arrays are overwritten.

Returns The grid.

fitstatic

static fit(box: Box | ShapeLike, columns: GridTrack[], rows: GridTrack[], result?: Grid | null): Grid

Creates a grid over a box from size wishes per column and row, like CSS flex items.

Each track starts at its preferred size. Free space is shared in proportion to grow; missing space is taken in proportion to shrink × the preferred size (grow: 0 alone doesn't stop a track from shrinking; a fixed track has grow: 0, shrink: 0). Tracks that reach their min or max stay there and the rest is shared again. If the minimum sizes are larger than the box (or the maximum sizes smaller), the grid ends after (or before) the edge of the box.

box
The area the grid covers.
columns
One size wish per column.
rows
One size wish per row.
result optional
Optional grid to store the result in; its line arrays are overwritten.

Returns The grid.

focusstatic

static focus(grid: Grid, cell: GridCell, factor: Real | NumberLike, falloff: Real | NumberLike, result?: Grid | null): Grid

Enlarges one column and one row of a grid, and their neighbours less and less (focus + context, fisheye).

The focused column becomes factor times as wide; the factor falls off linearly over falloff columns on each side. The outer lines stay where they are, so the other columns get narrower. The same for the row. A row or column of −1 leaves that direction unchanged (so the result of cellOf for a point outside leaves the grid as it is); a factor that is not positive leaves both unchanged. To focus the cell under a pointer, use focusAt: after focusing, the lines move, and the pointer can end up in a neighbouring cell.

grid
The grid to start from.
cell
The cell whose column and row are enlarged; −1 for a direction that stays unchanged.
factor
How much the focused column and row grow (1 = unchanged, 3 = three times).
falloff
Over how many neighbouring columns/rows the enlargement fades out (0 = only the focused one).
result optional
Optional grid to store the result in; it may be grid itself.

Returns The focused grid.

focusAtstatic

static focusAt(grid: Grid, point: Point | VectorLike, factor: Real | NumberLike, falloff: Real | NumberLike, result?: Grid | null): Grid

Enlarges the column and row under a point, so that the point lies in the enlarged cell of the result (fisheye under the pointer).

Focusing moves the lines, so the cell under a point in grid is not always the cell under it after focusing. This picks, per direction, the cell that contains the point in the focused grid (half-open cells, as in cellOf), so the enlarged cell stays under the pointer while it moves. A point outside the grid leaves that direction unchanged. Same factor and falloff as focus.

grid
The grid to start from (not focused).
point
The point, for example the pointer.
factor
How much the focused column and row grow (1 = unchanged, 3 = three times).
falloff
Over how many neighbouring columns/rows the enlargement fades out.
result optional
Optional grid to store the result in; it may be grid itself.

Returns The focused grid.

fromWeightsstatic

static fromWeights(box: Box | ShapeLike, columnWeights: number[], rowWeights: number[], result?: Grid | null): Grid

Creates a grid over a box whose column widths and row heights are proportional to weights.

For example a Marimekko chart: column widths from the totals per group. Negative and NaN weights count as 0; if all weights of a direction are 0, its cells are equal. An empty list gives no lines in that direction.

box
The area the grid covers.
columnWeights
One weight per column.
rowWeights
One weight per row.
result optional
Optional grid to store the result in; its line arrays are overwritten.

Returns The grid.

horizontalLinesstatic

static horizontalLines(grid: Grid, result?: Segment[] | null): Segment[]

Returns the horizontal lines of the grid from top to bottom, each from the left to the right line.

With result, its segments are reused (see lines).

grid
The grid.
result optional
Optional list to store the result in.

Returns rows + 1 segments; none if the grid has no vertical lines.

indexstatic

static index(grid: Grid, cell: GridCell): number

Returns the position of a cell in the order of cells: row by row from the top left.

row × columns + column. For another order, compute the cell first, for example from the bottom left: Grids.cellAt(grid, i), then use Grids.rows(grid) − 1 − cell.row as the row.

grid
The grid.
cell
The cell.

Returns The index, or −1 if the cell is not in the grid.

lerpstatic

static lerp(a: Grid, b: Grid, t: Real | NumberLike, result?: Grid | null): Grid

Interpolates between two grids line by line, for example to animate a change of the grid.

If a direction has a different number of lines in the two grids, there is nothing to interpolate: it takes the lines of a for t < 0.5 and those of b otherwise. Eased: animate a Real t from 0 to 1 (Animate.real.eased) and call lerp in a Reactive.do on t. Follow (no fixed duration, the goal may change on the way): when the goal changes, copy the current grid into a, set the goal as b, set t to 0 and Animate.real.follow(t, 1). Each frame t moves a fraction alpha (of RvgFollowOptions) of its remaining way, so every line does too: the same motion as following each line on its own.

a
The grid at t = 0.
b
The grid at t = 1.
t
The interpolation parameter.
result optional
Optional grid to store the result in; it may be a or b.

Returns The interpolated grid.

linesstatic

static lines(grid: Grid, result?: Segment[] | null): Segment[]

Returns the lines of the grid as segments: the vertical ones from left to right, then the horizontal ones from top to bottom, each across the whole grid.

For drawing the grid. With result, its segments are reused and written to (so segment visuals passed in follow); segments are added or removed at the end. A grid without lines in one direction has no lines in the other either (they would have no length).

grid
The grid.
result optional
Optional list to store the result in.

Returns columns + 1 vertical, then rows + 1 horizontal segments.

mapstatic

static map(from: Grid, to: Grid, point: Point | VectorLike, result?: Point | null): Point

Carries a point from one grid to another: the same cell, at the same fraction of its width and height.

Draw on a regular grid and map onto a focused or weighted one to distort the drawing with the grid. Both grids need the same number of lines in each direction; outside the grid, the first or last column (row) is extended.

from
The grid the point is given in.
to
The grid to carry it to.
point
The point.
result optional
Optional point to store the result in.

Returns The mapped point; NaN coordinates in a direction where the line counts differ.

mapShapestatic

static mapShape(from: Grid, to: Grid, shape: Shape | ShapeLike, result?: Shape | null): Shape

Carries every vertex of a shape from one grid to another (see map).

Only the vertices move; edges stay straight. Split long edges that cross several cells first, so the shape bends with the grid.

from
The grid the shape is given in.
to
The grid to carry it to.
shape
The shape.
result optional
Optional shape to store the result in; it may be shape itself.

Returns The mapped shape.

nearestLinestatic

static nearestLine(lines: number[], value: Real | NumberLike): number

Returns the index of the line nearest to a value, for example which tick a dragged handle snapped to.

Pass grid.x with an x position or grid.y with a y position. Positions outside give the first or last line; on a tie, the lower line. Same choice as snap, so Grids.nearestLine(grid.x, p.x) is the vertical line Grids.snap(grid, p) moved to.

lines
The sorted line positions of one direction.
value
The position.

Returns The index into lines, or −1 if there are no lines or the value is NaN.

rowOfstatic

static rowOf(grid: Grid, y: Real | NumberLike): number

Returns the row that contains a y position.

grid
The grid.
y
The y position.

Returns The row, or −1 if y is outside the grid or NaN.

rowsstatic

static rows(grid: Grid): number

Returns the number of rows of the grid (horizontal lines − 1, at least 0).

grid
The grid.

Returns The number of rows.

snapstatic

static snap(grid: Grid, point: Point | VectorLike, result?: Point | null): Point

Moves a point to the nearest crossing of grid lines.

The nearest vertical and the nearest horizontal line are found separately; points outside the grid go to the nearest edge. For a uniform grid through the origin, Points.snapToGrid is the shorter call.

grid
The grid.
point
The point to snap.
result optional
Optional point to store the result in.

Returns The nearest crossing; NaN coordinates if the grid has no lines in that direction.

uniformstatic

static uniform(box: Box | ShapeLike, cellSize: Real | NumberLike, result?: Grid | null): Grid

Creates a grid over a box with square cells of the given size, starting at the top-left corner.

The last line in each direction is the edge of the box, so the last column and row can be narrower than cellSize. A cell size that is not positive, or so small that a direction would get more than a million cells, gives a grid without lines.

box
The area the grid covers.
cellSize
The width and height of a cell.
result optional
Optional grid to store the result in; its line arrays are overwritten.

Returns The grid.

verticalLinesstatic

static verticalLines(grid: Grid, result?: Segment[] | null): Segment[]

Returns the vertical lines of the grid from left to right, each from the top to the bottom line.

With result, its segments are reused (see lines).

grid
The grid.
result optional
Optional list to store the result in.

Returns columns + 1 segments; none if the grid has no horizontal lines.