lattices.js
helps making 2-d symmetric square and hexagonal lattice objects with optional periodic boundary conditions, size and scale options and easy access to nodes, node neighbors and cell geometries.
This can be useful for programming visualizations and agent based simulations in d3js
.
Install the package as a node module:
npm install lattices
or clone this repository and install:
git clone https://github.com/dirkbrockmann/lattices.git
cd lattices
npm install
npm run build
View examples:
npm run examples
or start
http-server ./dist/
and open http://localhost:8080/examples/
in the browser.
Either load the package as a remote resource like so:
<script src="https://unpkg.com/lattices"></script>
<script>
const a = lattices.square(123)
</script>
Alternatively, use a local copy (dist/lattices.js
) and include it in your html
-file like so:
<script src="lattices.js"></script>
<script>
const sq = lattices.square(5).boundary("dirichlet")
</script>
If you want to use it as part of your own project as a module import @dirkbrockmann/lattices
like so:
import * as lattices from "lattices"
You can generate square and hexagonal lattices, both either with periodic boundary conditions or not (Dirichlet boundaries).
const N = 10
const sq = lattices.square(N)
creates a simple square lattice and stores it in sq
.
By default the lattice has periodic boundaries.
By default the lattice has a spatial scale
The (x,y)-coordinates of the lattice range from -L/2 to L/2 in both dimensions:
with
const N = 10
const hx = lattices.hex(N)
creates a hexagonal lattice stores it in hx
.
By default the lattice has periodic boundaries. Periodic boundary conditions are a bit tricky in hex lattices, but it can be worked out, see https://www.redblobgames.com/grids/hexagons/.
By default the lattice has a spatial scale
Both, square and hexagonal lattice have the following property fields. Say you have defined a lattice G = lattices.hex(10)
or G = lattices.square(10)
then
-
G.N
: is the linear node range$N$ (in this case 10) -
G.size
: is the number of nodes in the lattice -
G.L
: the linear physical extent -
G.type
: returns its type, i.e. "hexagonal" / "square"
Nodes can be accessed by G.nodes
. Each node in the array has x and y coordinates and a list of neighbors array. e.g.
G.nodes[i].x
: returns node i's x-coordinateG.nodes[i].y
: returns node i's y-coordinateG.nodes[i].neighbors
: returns an array of all of i's neighborsG.nodes[i].cell()
: returns a list of coordinates to draw the nodes cell
G.scale(s)
sets the spatial scale of the lattice to s
. The default value is 1. This effects the x and y coordinates of the nodes and their boundaries.
Without argument, the current scale of G
is returned.
G.boundary(["periodic"|"dirichlet"])
sets the type of boundary condition. Default is "periodic". For dirichlet
the nodes on the boundary
have a different number of neighbors.
Without an argument, returns the lattice's boundary type.
For a square lattice you can chose a node's neighborhood to have a neighborhood of the 8 surrounding lattice nodes or only the 4 nodes corresponding to above, below, left and right. Like so
const sq = lattices.square(10).hood("n4")
or
const sq = lattices.square(10).hood("n8")
Without arguments hood()
returns the neighborhood type.