A mathy Javacript utility library for working with hexacubic grids functionally
Install via yarn
or npm
:
yarn add hexacubic
npm install hexacubic -S
const H = require('hexacubic')
import H from 'hexacubic'
Takes a hexacube center point and returns an array of center points of its neighbor hexacubes.
hexacube (Point)
: The center point of the hexacube.
([Point])
: Returns an array of center points of the neighbor hexacubes of the hexacube.
const origin = [0, 0, 0];
H.centerToNeighbors(origin);
// =>
// [
// [1, 0, -1],
// [-1, 0, 1],
// [0, 1, -1],
// [0, -1, 1],
// [1, -1, 0],
// [-1, 1, 0]
//]
Creates a predicate function that evaluates if hexacubeB
is a neighbor of hexacubeA
.
hexacubeA (Point)
: The center point of the source hexacube.hexacubeB (Point)
: The center point of the target hexacube.
(Boolean)
: Returnstrue
if source and target hexacubes are neighbors, elsefalse
.
const homer = [0, 0, 0];
const flanders = [1, 0, -1];
H.isNeighborOf(homer)(flanders);
// => true
const patty = [10, -5, -5];
H.isNeighborOf(homer)(patty);
// => false
const selma = patty;
H.isNeighborOf(patty)(selma);
// => false
Takes a hexacube center point and returns its corner points.
hexacube (Point)
: The center point of the hexacube.
([Point])
: Returns an array of corner points of the hexacube.
const origin = [0, 0, 0];
H.centerToCorners(origin);
// =>
//[
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ],
// [ -0.6666666666666666, 0.3333333333333333, 0.3333333333333333 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ],
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ]
// ]
Takes a hexacube center point and returns its edges.
hexacube (Point)
: The center point of the hexacube.
([Edge])
: Returns an array of corner point pairs representing the edges of the hexacube.
const origin = [0, 0, 0];
H.centerToEdges(origin);
// =>
// [
// [
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ]
// ],
// [
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ],
// [ -0.6666666666666666, 0.3333333333333333, 0.3333333333333333 ]
// ],
// [
// [ -0.6666666666666666, 0.3333333333333333, 0.3333333333333333 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ]
// ],
// [
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ],
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ]
// ],
// [
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ]
// ],
// [
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ],
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ]
// ]
// ]
Takes a set of hexacube center points returns all the edges. (deduped)
hexacubes ([Point])
: The array of center points of the hexacubes.
([Edge])
: Returns an array of corner point pairs, or edges, of every hexacube.
const origin = [0, 0, 0];
const originAndNeighbors = [
origin,
...H.centerToNeighbors(origin)
]
H.centersToEdges(originAndNeighbors);
// =>
// [
// [
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ]
// ],
// [
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ],
// [ -0.6666666666666666, 0.3333333333333333, 0.3333333333333333 ]
// ],
// [
// [ -0.6666666666666666, 0.3333333333333333, 0.3333333333333333 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ]
// ],
// [
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ],
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ]
// ],
// [
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ],
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ]
// ],
// [
// [ -0.3333333333333333, 0.6666666666666666, -0.3333333333333333 ],
// [ 0.3333333333333333, 0.3333333333333333, -0.6666666666666666 ]
// ]
// ... 36 more edges (6 edges for each neighbor of origin)
// ]
If you want all the edges with duplicates instead, use flatMap
with .centerToEdges()
:
import _ from "lodash/fp"
const edges = _.flatMap(H.centerToEdges)(originAndNeighbors)
Takes a set of hexacube center points and returns the edges of its boundary.
hexacubes ([Point])
: The array of the center points of the hexacubes.
([Edge])
: Returns an array of the edges of the boundary around the cluster(s) of hexacubes.
const origin = [0, 0, 0];
const originAndNeighbors = [
origin,
...H.centerToNeighbors(origin)
]
H.centersToBounds(originAndNeighbors);
// =>
// [
// ... 18 edges total (3 edges on boundary for each neighbor of origin)
// ]
Accumlates an edge if it is a boundary edge with respect to a set of edges
const edges = H.centersToEdges(hexacubes);
const bounds = edges.reduce(H.accumlateBounds);
Projects a hexacubic point isometrically to a plane of view, where x-axis is the horizontal axis and y-axis and z-axis are the diagonal axis
H.toProjection(origin)
// => [0, 0]
H.centerToCorners(origin)
.map(_.compose(customTransform, H.toProjection))
.map(([x,y]) => <Point x={x} y={y}/>)
// =>
const customProjectionOf = _.compose(customTransform, H.toProjection)
H.centerToEdges(origin)
.map(edge => edge.map(customProjectionOf))
.map(([[Ax,Ay],[Bx,By]]) => <Line Ax={Ax} Ay={Ay} Bx={Bx} By={By}/>)
// =>
options:
size (Number)
- The projection scalar.offset ([Number])
- The projection translated.
Takes an array of hexacubic points and returns the midpoint, or median point.
const origin = [0, 0, 0];
const neighborOfOrigin = [0, 1, -1];
H.toMidpoint([origin, neighborOfOrigin])
// => [0, .5, -.5]
const neighborsOfOrigin = H.centerToNeighbors(origin);
H.toMidpoint(neighborsOfNeighbor);
// => [0, 0, 0]
Creates a function that calculates the distance of hexacubeB
from hexacubeA
.
const origin = [0, 0, 0];
const neighborOfOrigin = [0, 1, -1];
H.distanceFrom(origin)(neighborOfOrigin)
// => 1
const distanceFromOrigin = H.distanceFrom(origin)
const elsewhere = [5, -5, 0]
distanceFromOrigin(elsewhere)
// => 5
Takes a set of hexacube center points and returns a set of one or more polygons (one for each bound cluster).
hexacubes ([Point])
: The array of the center points of the hexacubes.
([Polygon])
: Returns an array of the polygon(s) of the bound cluster(s) of hexacubes.
const origin = [0, 0, 0];
H.centersToPolygons([origin]);
// =>
// [
// ... 1 polygon with 6 points
// ]
const originAndNeighbors = [
origin,
...H.centerToNeighbors(origin)
]
H.centersToPolygons(originAndNeighbors);
// =>
// [
// ... 1 polygon with 18 points
// ]