22
$\begingroup$

Is there a function that can create hexagonal grid?

We have square grid graph, where we can specify m*n dimensions:

GridGraph[{m, n}]

We have triangular grid graph (which works only for argument n up to 10 - for unknown reason):

GraphData[{"TriangularGrid", n}, "Graph"]

I can not find a function that would generate a hexagonal grid graph. I would like it like it is with GridGraph something like HexagonalGridGraph[{m,n,o}] where m,n,o are dimensions m*n*o of planar graph - or other way said - "lengths" of the sides of the graph.

I can make my own code, I am asking just in case there already exist implemented function.

UPDATE:

What I mean by m*n*o hexagonal grid is for example this 3*5*7 hexagonal grid:

enter image description here

My code for producing it is very long and cumbersome so I will not upload it unless I can make it simpler.

$\endgroup$
5
  • 9
    $\begingroup$ HexagonalGridGraph resource function $\endgroup$
    – LouisB
    Oct 8, 2020 at 11:20
  • $\begingroup$ @C.E. You're right, that was a bad fit. Well, I recalled that this question had been asked already several times and just picked the first hit in search results. Was a bad idea. I am sorry. $\endgroup$ Oct 8, 2020 at 11:39
  • $\begingroup$ @LouisB: Nice but it lacks generality because we can have m*n*o hexagonal grids, the function can produce only m*n*1 grids. See image I uploaded. $\endgroup$ Oct 8, 2020 at 12:39
  • 2
    $\begingroup$ Possible duplicate: mathematica.stackexchange.com/questions/230017/… $\endgroup$
    – expression
    Oct 8, 2020 at 13:34
  • $\begingroup$ @LouisB So.. Anyone CAN create their own ResourceFunction[] .. and Wolfram will never make define HexagonalGridGraph[] mathworld.wolfram.com/HexagonalGridGraph.html ? $\endgroup$
    – p._phidot_
    Apr 3 at 7:27

6 Answers 6

17
$\begingroup$

With IGraph/M:

IGMeshGraph@IGLatticeMesh["Hexagonal", {6, 4}]

enter image description here

We can also crop it to a hexagon:

IGMeshGraph@IGLatticeMesh["Hexagonal", Polygon@CirclePoints[10, 6]]

enter image description here

It can also generate many other kinds of lattices, not just hexagonal.

$\endgroup$
3
  • $\begingroup$ Can it also make 3x5x7 hexagonal grid? Like IGMeshGraph@IGLatticeMesh["Hexagonal", {3, 5, 7}]? Because it is possible to have not only "rectangular hexagonal grid" but also "hexagonal hexagonal grid", where we can have three pairs of sides of distinct length. Like 3x5x7x3x5x7. $\endgroup$ Oct 8, 2020 at 12:03
  • $\begingroup$ See image I uploaded - can it do such a grid? $\endgroup$ Oct 8, 2020 at 12:41
  • $\begingroup$ @azerbajdzan You can crop an infinite lattice it to any shape, you just have to specify the shape: szhorvat.net/mathematica/IGDocumentation/#iglatticemesh $\endgroup$
    – Szabolcs
    Oct 8, 2020 at 13:47
16
$\begingroup$

Here is my generalization of the code from link provided by @LouisB:

HexagonalGridGraph2[{wide1_Integer?Positive, wide2_Integer?Positive, 
   wide3_Integer?Positive}, opts : OptionsPattern[Graph]] := 
 Module[{cells, edges, vertices}, 
  cells = 
   Flatten[Table[
     CirclePoints[{Sqrt[3] (1 j + k - 2 ) + Sqrt[3] (1 j + l - 2 ), 
       3 k - 2 - 3 l}, {2, \[Pi]/2}, 6], {j, wide1}, {k, wide2}, {l, 
      wide3}], 2];
  edges = Union[Sort /@ Flatten[Partition[#, 2, 1, 1] & /@ cells, 1]];
  vertices = Union[Flatten[edges, 1]];
  IndexGraph[
   Graph[UndirectedEdge @@@ edges, opts, 
    VertexCoordinates -> Thread[vertices -> vertices]]]]

And here are some examples:

Sort /@ Tuples[Range[4], {3}] // Union;
Partition[
  Rasterize /@ (HexagonalGridGraph2[#, PlotLabel -> #, 
       ImageSize -> {100, 100}] & /@ %), 5];
ImageAssemble[%]

enter image description here

$\endgroup$
1
  • $\begingroup$ Instead of Sort /@ Tuples[Range[4], {3}] // Union, look into the IntegerPartitions function $\endgroup$ Oct 11, 2020 at 12:20
15
$\begingroup$

We can generate the vertex coordinates using a slightly modified version of azerbajdan's cells and use them with NearestNeighborGraph:

ClearAll[vCoords]
vCoords = DeleteDuplicates @ Flatten[
   Table[CirclePoints[{(2 j + k + l - 4) Sqrt[3] , 3 k - 2 - 3 l}, {2, π/2}, 6], 
     {j, #}, {k, #2}, {l, #3}], 3] &;

ClearAll[hexGridGraph]
hexGridGraph = Module[{v = vCoords @@ #},
   NearestNeighborGraph[v, ##2, VertexCoordinates -> v]] &;

Examples:

hexGridGraph[{3, 5, 7}, 
  VertexLabels -> Placed["Index", Center], 
  VertexSize -> .7, 
  VertexStyle -> White, 
  VertexLabelStyle -> 8, 
  ImageSize -> 400]

enter image description here

args = Sort /@ Tuples[Range[4], {3}] // Union;

hexGridGraph[#, PlotLabel -> #, ImageSize -> {100, 100}] & /@ args // 
    Multicolumn[#, 5] &

enter image description here

$\endgroup$
13
$\begingroup$

Edit-4

Besides of type {m,n,o},here we want to find the type {n[1],n[2],n[3],n[4],n[5],n[6]}. Simple calculate, for example

Solve[Array[n, 6] . CirclePoints[{0, 0}, {1, 0}, 6] == 0, 
 Array[n, 6], PositiveIntegers]

We can find that it satisfy two equations.

{n[1] - n[4] + n[2] - n[5] == 0, n[2] - n[5] + n[3] - n[6] == 0}

( so {m,n,o,m,n,o} always satisfy this relation)

e[1] = AngleVector[0];
e[3] = AngleVector[2 π/3];
e[2] = e[1] + e[3];
e[4] = -e[1];
e[5] = -e[2];
e[6] = -e[3];
sol = Simplify[
   SolveValues[Array[n, 6] . Array[e, 6] == {0, 0}, Array[n, 6], 
     PositiveIntegers][[1]], Array[C, 5] ∈ PositiveIntegers];
type = sol /. Thread[Array[C, 5] -> RandomInteger[{1, 10}, 5]]
bd = Accumulate@
   Catenate[MapThread[ConstantArray, {Array[e, 6], type - 1}]];
reg = BoundaryMeshRegion[bd, 
   Line /@ {##, #1} & @@ Partition[Range@Length@bd, 2, 1, 1]];
allpts = Tuples[Range[0, 2 Max@type], 2] . {e[1], e[3]};
pts = Pick[allpts, RegionMember[reg]@allpts];
Graphics[{EdgeForm[Blue], FaceForm[], 
  RegularPolygon[#, {1/Sqrt[3], π/2}, 6] & /@ pts, Red, Point@bd}]

{12, 10, 11, 15, 7, 14}

enter image description here

Edit-3

{m, n, o} = {3, 5, 7};
 Graphics[{EdgeForm[Blue], FaceForm[], 
  RegularPolygon[#, {1, 0}, 
     6] & /@ (Sqrt[
      3] SolveValues[{0 <= x <= o - 1, 0 <= y <= n - 1, 
        0 <= z <= m - 1, x == 0 || y == 0 || z == 0}, {x, y, z}, 
       Integers] . CirclePoints[{1, Pi/6}, 3])}]
{x, y, z} = {7, 5, 3};
bases = CirclePoints[{1, 30 Degree}, 3];
coordinates = 
  Catenate[{Tuples[{Range[0, x - 1], Range[0, y - 1], {0}}], 
    Tuples[{Range[1, x - 1], {0}, Range[1, z - 1]}], 
    Tuples[{{0}, Range[0, y - 1], Range[1, z - 1]}]}];
Graphics[{EdgeForm[Blue], FaceForm[], 
  RegularPolygon[#, {1, 0}, 6] & /@ (Sqrt[3]*coordinates . bases)}]

enter image description here

Edit-2

The ideal comes from 3D.

Graphics3D[Cuboid[], BoxRatios -> {5, 7, 3}, Boxed -> False, 
 ViewProjection -> "Orthographic", ViewPoint -> {2.0, -1.7, 2.0}]

enter image description here

{eX, eY, eZ} = CirclePoints[{1, 30 Degree}, 3];
{x, y, z} = {7, 5, 3};
(*{x,y,z}={8,8,8};*)
pXY = Sqrt[3] Tuples[{Range[x] - 1, Range[y - 1]}] . {eX, eY};
pYZ = Sqrt[3] Tuples[{Range[y] - 1, Range[z - 1]}] . {eY, eZ};
pZX = Sqrt[3] Tuples[{Range[z] - 1, Range[x - 1]}] . {eZ, eX};
Graphics[{EdgeForm[White], Red, RegularPolygon[#, {1, 0}, 6] & /@ pXY,
   Green, RegularPolygon[#, {1, 0}, 6] & /@ pYZ, Blue, 
  RegularPolygon[#, {1, 0}, 6] & /@ pZX, Black, Point[pXY], 
  PointSize[Medium], Point[pYZ], PointSize[Large], Point[pZX]}]

enter image description here

Edit-1

If we introduce three coordinates {x1,y1,z1} and three bases e1,e2,e3 instead of just two coordinates and two bases, the construction of the type {m, n, o} = {3, 5, 7} is relatively easy.

{m, n, o} = {3, 5, 7};
eM = AngleVector[90 Degree];
eN = AngleVector[150 Degree];
eO = AngleVector[30 Degree];
pts = Sqrt[3] Tuples[Range /@ {m, n, o}] . {eM, eN, eO} // Union;
Graphics[{EdgeForm[White], 
  Table[RegularPolygon[p, {1, 0}, 6], {p, pts}], Red, Point[pts]}]

enter image description here

Graphics[{EdgeForm[Blue], FaceForm[], 
  Table[RegularPolygon[p, {1, 0}, 6], {p, pts}], Red, Point[pts], 
  Riffle[{Red, Green, Blue}, Arrow[{{0, 0}, 2 #}] & /@ {eM, eN, eO}]}]

enter image description here

$\endgroup$
6
$\begingroup$

You can make a hexagonal grid using only MMA built in functions. You may adapt the code to your liking:

c3 = Cos[30 Degree]; s3 = Sin[30 Degree];
del1 = {Sqrt[c3^2 + Sqrt[(1 + s3^2)^2 + c3^3]], c3} // N;
del2 = {-Sqrt[c3^2 + Sqrt[(1 + s3^2)^2 + c3^3]], c3} // N;
del3 = {0, 2 c3};
trans[del_] := Map[(del + #) &, hex, {2}];
n = 3;
grid = Flatten[
   Table[trans[i1 del1 + i2 del2 + i3 del3], {i1, n}, {i2, n}, {i3, 
     n}], 2];
Graphics[Line /@ grid]

enter image description here

$\endgroup$
4
$\begingroup$

There is a resource function that makes hexagonal graphs: HexagonalGridGraph. (Contributed by WRI.)

enter image description here

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.