Wolfram Research

Function Repository Resource:

LatticePointsArrangement (1.0.1) current version: 1.1.1 »

Source Notebook

Gives 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:
"CCWSpiralEast"
"CCWSpiralNorth"
"CCWSpiralWest"
"CCWSpiralSouth"
"CWSpiralEast"
"CWSpiralNorth"
"CWSpiralWest"
"CWSpiralSouth"
"CCWDiamondEast"
"CCWDiamondNorth"
"CCWDiamondWest"
"CCWDiamondSouth"
"CWDiamondEast"
"CWDiamondNorth"
"CWDiamondWest"
"CWDiamondSouth"
"ZigZagEastQ1"
"ZigZagNorthQ1"
"ZigZagWestQ2"
"ZigZagNorthQ2"
"ZigZagWestQ3"
"ZigZagSouthQ3"
"ZigZagEastQ4"
"ZigZagSouthQ4"
"ZigZagEastQ12"
"ZigZagWestQ12"
"ZigZagNorthQ23"
"ZigZagSouthQ23"
"ZigZagWestQ34"
"ZigZagEastQ34"
"ZigZagNorthQ14"
"ZigZagSouthQ14"
"ZigZagNorthEastQ12"
"ZigZagNorthWestQ12"
"ZigZagWestNorthQ23"
"ZigZagWestSouthQ23"
"ZigZagSouthEastQ34"
"ZigZagSouthWestQ34"
"ZigZagWestSouthQ14"
"ZigZagWestNorthQ14"
"ZigZagEastQ123"
"ZigZagSouthQ123"
"ZigZagNorthQ234"
"ZigZagEastQ234"
"ZigZagEastQ134"
"ZigZagNorthQ134"
"ZigZagEastQ124"
"ZigZagSouthQ124"
"ZigZagNorthWestQ123"
"ZigZagWestNorthQ123"
"ZigZagWestSouthQ234"
"ZigZagSouthWestQ234"
"ZigZagSouthEastQ134"
"ZigZagEastSouthQ134"
"ZigZagNorthEastQ124"
"ZigZagEastNorthQ124"
"ZigZagNorthEastQ123"
"ZigZagWestSouthQ123"
"ZigZagWestNorthQ234"
"ZigZagSouthEastQ234"
"ZigZagSouthWestQ134"
"ZigZagEastNorthQ134"
"ZigZagNorthWestQ124"
"ZigZagEastSouthQ124"
"DiagonalZigZagEastQ1"
"DiagonalZigZagNorthQ1"
"DiagonalZigZagNorthQ2"
"DiagonalZigZagWestQ2"
"DiagonalZigZagSouthQ3"
"DiagonalZigZagWestQ3"
"DiagonalZigZagEastQ4"
"DiagonalZigZagSouthQ4"
"DiagonalZigZagEastQ12"
"DiagonalZigZagSouthQ23"
"DiagonalZigZagNorthQ23"
"DiagonalZigZagEastQ34"
"DiagonalZigZagWestQ34"
"DiagonalZigZagNorthQ14"
"DiagonalZigZagWestQ12"
"DiagonalZigZagSouthQ14"
"DiagonalZigZagEastQ123"
"DiagonalZigZagSouthQ123"
"DiagonalZigZagNorthQ234"
"DiagonalZigZagEastQ234"
"DiagonalZigZagWestQ134"
"DiagonalZigZagNorthQ134"
"DiagonalZigZagWestQ124"
"DiagonalZigZagSouthQ124"
"CCWDiagonalQ1"
"CWDiagonalQ1"
"CCWDiagonalQ2"
"CWDiagonalQ2"
"CCWDiagonalQ3"
"CWDiagonalQ3"
"CWDiagonalQ4"
"CCWDiagonalQ4"
"CCWDiagonalQ12"
"CWDiagonalQ23"
"CCWDiagonalQ23"
"CWDiagonalQ34"
"CCWDiagonalQ34"
"CWDiagonalQ14"
"CWDiagonalQ12"
"CCWDiagonalQ14"
"CCWDiagonalQ123"
"CWDiagonalQ123"
"CCWDiagonalQ234"
"CWDiagonalQ234"
"CCWDiagonalQ134"
"CWDiagonalQ134"
"CCWDiagonalQ124"
"CWDiagonalQ124"
"CWCornerQ1"
"CCWCornerQ3"
"CWCornerQ2"
"CCWCornerQ4"
"CWCornerQ3"
"CCWCornerQ1"
"CWCornerQ4"
"CCWCornerQ2"
"CWCornerQ12"
"CCWCornerQ12"
"CCWCornerQ23"
"CWCornerQ23"
"CCWCornerQ34"
"CWCornerQ34"
"CCWCornerQ14"
"CWCornerQ14"
"CWCornerQ123"
"CCWCornerQ123"
"CWCornerQ234"
"CCWCornerQ234"
"CWCornerQ134"
"CCWCornerQ134"
"CWCornerQ124"
"CCWCornerQ124"

Examples

Basic Examples (1) 

Create 20 points in a counterclockwise 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 136 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]:=
ClearAll[ShowRoute, MakeMove, FindSequence]
horsejump = Select[Tuples[Range[-2, 2], 2], Norm[#] == Sqrt[5] &];
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]}]
  ]
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[]
   ]
  ]
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]]|>
  ]
grid = ResourceFunction["LatticePointsArrangement"]["CCWSpiralEast", 10000];
grid = Association[MapIndexed[#1 -> #2[[1]] &, grid]];
ShowRoute[FindSequence[{0, 0}, grid]]
Out[15]=

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