Wolfram Research

Function Repository Resource:

SquareZigzagPoints

Source Notebook

Lattice points in a square in zigzag order from the bottom-left to the top-right corner

Contributed by: George Beck

ResourceFunction["SquareZigzagPoints"][n]

gets all the points in the square with opposite corners {1,1} to {n,n} in zigzag order.

Details and Options

The square has side length n-1.
In zigzag order, the points form a path N, SE, E, NW, N, ….

Examples

Basic Examples

This shows how the points are ordered in the simplest case:

In[1]:=
ResourceFunction["SquareZigzagPoints"]@2
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}]
 ]
Out[2]=

A larger example:

In[3]:=
Graphics[{
  Arrowheads[Medium], Map[Arrow, Transpose[{Most@#, Rest@#}] &@
    ResourceFunction["SquareZigzagPoints"]@11]}
 ]
Out[3]=

Scope

This starts at the bottom right:

In[4]:=
br = With[{n = 3}, {n + 1, 0} + {-1, 1} # & /@ ResourceFunction["SquareZigzagPoints"][n]]
Out[4]=

This shows the path:

In[5]:=
Graphics[{
  Arrowheads[Medium], Map[Arrow, Transpose[{Most@br, Rest@br}]]
  }]
Out[5]=

This starts horizontally at the bottom right:

In[6]:=
hbr = With[{n = 3}, {n + 1, 0} + {-1, 1} Reverse@# & /@ ResourceFunction["SquareZigzagPoints"][n]]
Out[6]=

This shows the path:

In[7]:=
Graphics[{
  Arrowheads[Medium], Map[Arrow, Transpose[{Most@hbr, Rest@hbr}]]
  }]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information