Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Lattice points in a square in zigzag order from the bottom-left to the top-right corner
| ResourceFunction["SquareZigzagPoints"][n] gets all the points in the square with opposite corners {1,1} to {n,n} in zigzag order. | 
This shows how the points are ordered in the simplest case:
| In[1]:= | ![ResourceFunction["SquareZigzagPoints"]@2](https://www.wolframcloud.com/obj/resourcesystem/images/3eb/3eb567df-1eb5-4921-93fb-c2e59e9f9270/794394c474c1ae7b.png) | 
| Out[1]= |  | 
| In[2]:= | ![With[{n = 2},
 Graphics[{
   Arrowheads[Large], {LightGray, Map[Arrow, Transpose[{Most@#, Rest@#}] &@
      ResourceFunction["SquareZigzagPoints"]@n]},
   Red, PointSize[.03], Point[Join @@ Table[{x, y}, {x, n}, {y, n}]]
   }, Axes -> True, AxesOrigin -> {0, 0}]
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/3eb/3eb567df-1eb5-4921-93fb-c2e59e9f9270/698ec040e50e4fd0.png) | 
| Out[2]= |  | 
A larger example:
| In[3]:= | ![Graphics[{
  Arrowheads[Medium], Map[Arrow, Transpose[{Most@#, Rest@#}] &@
    ResourceFunction["SquareZigzagPoints"]@11]}
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/3eb/3eb567df-1eb5-4921-93fb-c2e59e9f9270/2df7aaee9e883f8b.png) | 
| Out[3]= |  | 
This starts at the bottom right:
| In[4]:= | ![br = With[{n = 3}, {n + 1, 0} + {-1, 1} # & /@ ResourceFunction["SquareZigzagPoints"][n]]](https://www.wolframcloud.com/obj/resourcesystem/images/3eb/3eb567df-1eb5-4921-93fb-c2e59e9f9270/49a9ed3e507d42f5.png) | 
| Out[4]= |  | 
This shows the path:
| In[5]:= | ![Graphics[{
  Arrowheads[Medium], Map[Arrow, Transpose[{Most@br, Rest@br}]]
  }]](https://www.wolframcloud.com/obj/resourcesystem/images/3eb/3eb567df-1eb5-4921-93fb-c2e59e9f9270/606514e4fe45ab3d.png) | 
| Out[5]= |  | 
This starts horizontally at the bottom right:
| In[6]:= | ![hbr = With[{n = 3}, {n + 1, 0} + {-1, 1} Reverse@# & /@ ResourceFunction["SquareZigzagPoints"][n]]](https://www.wolframcloud.com/obj/resourcesystem/images/3eb/3eb567df-1eb5-4921-93fb-c2e59e9f9270/6c601d4400e9b50c.png) | 
| Out[6]= |  | 
This shows the path:
| In[7]:= | ![Graphics[{
  Arrowheads[Medium], Map[Arrow, Transpose[{Most@hbr, Rest@hbr}]]
  }]](https://www.wolframcloud.com/obj/resourcesystem/images/3eb/3eb567df-1eb5-4921-93fb-c2e59e9f9270/6301e132d793e253.png) | 
| Out[7]= |  | 
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License