This is a soft question about the best approach to design a function that creates MeshRegion
objects of various 2D lattices, e.g. kagome or hexagonal, all through one consistent interface.
A reasonable approach is to provide a unit cell as a Polygon
, along with translation vectors. This is what many solutions on this site use, e.g.
The problem is that MeshRegion
expects a set of points, then a set of polygons that refer to those points by index. Neighbouring cells of the lattice will share some of these points. Thus it is not simple to compute the translation of points: after translating the coordinates, we must check if some of the translated ones coincide with ones we already had. Doing this is also likely to result in a "messy" indexing of the points. Ideally, they should be indexed in a consistent order.
Here's an example to show what I mean, using hexTile
:
DiscretizeGraphics[
Graphics@Flatten@hexTile[3, 5],
MeshCellLabel -> {0 -> "Index"}
]
Other than the messy indexing, I am not happy to use DiscretizeGraphics
. It is an extremely general function which must handle lots of different inputs. Such functions in Mathematica are not always very robust. Will it always correctly identify points which are the same? What if the coordinates are floating point? Even ConvexHullMesh
won't always detect collinear points, not even with exact coordinates.
How would you approach this problem? There are several potential solutions. I am looking for "clean" ones which perform well and are not too complicated to implement.