Hexagonal binning of points of the plane, d3-hexbin's lattice (spec/08 §10).
iex> {:ok, [cell]} = Macaw.Draw.Layout.Hexbin.bin([{1, 1}, {-1, 2}], 20)
iex> {cell.x, cell.y, cell.count, cell.points}
{0.0, 0.0, 2, [0, 1]}It is pixel binning: the Frame bins positions after projection or scaling, never data
values. For a circumradius r, columns are dx = 2r · sin(π/3) apart and rows
dy = 1.5r, every odd row offset by half a column; a point belongs to the cell whose
center is nearest: row = round(y / dy), col = round(x / dx − offset), and a point
in the band where two rows' hexagons meet goes to the other row's cell when its center
is nearer, d3-hexbin's correction measured in the plane's units. The hexagons
are pointy-topped, as d3-hexbin draws them. A cell keeps the input positions of its
points, so two equal points in different rows stay two rows. This is visualize's
Visualize.Layout.Hexbin with Visualize.Chart.Transform.Geo hexbin/2.
Summary
Functions
The non-empty cells of the lattice over points, {x, y} each, in lattice order, row
then column (§10). A point with a coordinate that is not a number (a nil) is skipped,
its position in no cell's points. A radius that is not a positive number is
{[:radius], {:type, :positive_number}}.
The columns of the cells (§10). :cells: one row per cell, "row" (nil: a cell is no
input row), "x", "y", "count", "path" (its hexagon at radius). :points: one
row per binned input point in input order, "row", "cx", "cy", "count".
The closed hexagon of radius at the center (§10): vertices at the angles
π/3 · i − π/6 for i in 0..5, measured clockwise on screen from +x, the first at
−30° (the upper right), pointy-topped as d3-hexbin's.
Types
@type cell() :: %{ x: float(), y: float(), count: pos_integer(), points: [non_neg_integer()] }
A non-empty cell: its center, its count and its points' input positions, ascending.
Functions
@spec bin([{number() | nil, number() | nil}], number()) :: {:ok, [cell()]} | Macaw.Draw.Layout.error()
The non-empty cells of the lattice over points, {x, y} each, in lattice order, row
then column (§10). A point with a coordinate that is not a number (a nil) is skipped,
its position in no cell's points. A radius that is not a positive number is
{[:radius], {:type, :positive_number}}.
@spec columns([cell()], atom() | String.t(), number()) :: %{required(String.t()) => [term()]} | Macaw.Draw.Layout.error()
The columns of the cells (§10). :cells: one row per cell, "row" (nil: a cell is no
input row), "x", "y", "count", "path" (its hexagon at radius). :points: one
row per binned input point in input order, "row", "cx", "cy", "count".
@spec hexagon(number(), number(), number()) :: Macaw.Draw.IR.Path.t() | Macaw.Draw.Layout.error()
The closed hexagon of radius at the center (§10): vertices at the angles
π/3 · i − π/6 for i in 0..5, measured clockwise on screen from +x, the first at
−30° (the upper right), pointy-topped as d3-hexbin's.