Function Repository Resource:

LatticePointsArrangement

Source Notebook

Get lattice points in a variety of arrangements

Contributed by: Sander Huisman

ResourceFunction["LatticePointsArrangement"][arrangement,n]

generates the first n lattice points based on arrangement.

ResourceFunction["LatticePointsArrangement"][]

returns all the possible arrangements.

Details

The following arrangements can be chosen as arrangement:

Examples

Basic Examples (1) 

Create 20 points in a counter clock wise spiral starting in the eastern direction:

In[1]:=
ResourceFunction["LatticePointsArrangement"]["CCWSpiralEast", 20]
Out[1]=

Scope (2) 

Create some points based on zigzagging diagonally in the first two quadrants:

In[2]:=
pts = ResourceFunction["LatticePointsArrangement"][
   "DiagonalZigZagEastQ12", 100];

Visualize the points:

In[3]:=
ListPlot[pts, Joined -> True, PlotMarkers -> Automatic]
Out[3]=

LatticePointsArrangement without any arguments returns all the possible arrangements:

In[4]:=
arr = ResourceFunction["LatticePointsArrangement"][]
Out[4]=

Currently 160 arrangements are known:

In[5]:=
Length[arr]
Out[5]=

Applications (1) 

Create the Ulam prime spiral:

In[6]:=
pts = ResourceFunction["LatticePointsArrangement"]["CCWSpiralEast", 100000];
Graphics[{PointSize[Small], Point[pts[[Prime[Range[1, PrimePi[Length[pts]]]]]]]}]
Out[7]=

Neat Examples (1) 

Recreate the OEIS A316667 sequence of a horse moving on a spirally numbered board and moving to the lowest available unvisited square at each step:

In[8]:=
horsejump = Select[Tuples[Range[-2, 2], 2], Norm[#] == Sqrt[5] &];
In[9]:=
ShowRoute[output_Association] := Module[{colors},
   colors = (ColorData["Rainbow"] /@ Subdivide[Length[output["Coordinates"]] - 1.0]);
   Graphics[{Line[output["Coordinates"], VertexColors -> colors], Disk[Last@output["Coordinates"], 0.2]}]
   ];
In[10]:=
MakeMove[spiral_Association, visited_List] := Module[{poss, hj},
   poss = Table[Last[Last[visited]] + hj, {hj, horsejump}];
   poss = DeleteMissing[{spiral[#], #} & /@ poss, 1, \[Infinity]];
   poss = Select[poss, FreeQ[visited[[All, 2]], Last[#]] &];
   If[Length[poss] > 0,
    First[TakeSmallestBy[poss, First, 1]]
    ,
    Missing[]
    ]
   ];
In[11]:=
FindSequence[start_ : {0, 0}, grid_] := Module[{positions, j, next},
   positions = {{grid[start], start}};
   PrintTemporary[Dynamic[j]];
   Do[
    next = MakeMove[grid, positions];
    If[next =!= Missing[],
     AppendTo[positions, next]
     ,
     Break[];
     ]
    ,
    {j, \[Infinity]}
    ];
   <|"Coordinates" -> positions[[All, 2]], "Indices" -> positions[[All, 1]]|>
   ];
In[12]:=
grid = ResourceFunction["LatticePointsArrangement"]["CCWSpiralEast", 10000];
grid = Association[MapIndexed[#1 -> #2[[1]] &, grid]];
ShowRoute[FindSequence[{0, 0}, grid]]
Out[13]=

Publisher

SHuisman

Version History

  • 1.1.1 – 21 August 2023
  • 1.1.0 – 04 May 2022
  • 1.0.1 – 09 August 2021
  • 1.0.0 – 02 October 2019

Related Resources

License Information