Skip to content

Instantly share code, notes, and snippets.

@dshemetov
Last active May 20, 2018 17:33
Show Gist options
  • Save dshemetov/b05a3034e35f3e66e20c2d33d5c39af0 to your computer and use it in GitHub Desktop.
Save dshemetov/b05a3034e35f3e66e20c2d33d5c39af0 to your computer and use it in GitHub Desktop.
(**First we make a function to obtain every spiral corner, then we interpolate between each corner to get the sides, sum together, and finally convert the bijection to an integer sequence.*))
up = {0, 1};
right = {1, 0};
down = -up;
left = -right;
directions = {up, right, down, left};
start = {0, 0};
length = 100;
corners = Table[
start + Sum[Ceiling[i/2] directions[[Mod[i, 4, 1]]], {i, 1, n}],
{n, 0, length}];
Interpolate[a_, b_] :=
Table[a + i (b - a)/Norm[b - a], {i, 1, Norm[b - a]}];
spiral = Join[
{{0, 0}},
Join @@
MapThread[Interpolate, {corners[[1 ;; -2]], corners[[2 ;;]]}]
];
ListLinePlot[spiral, PlotLabel -> "Spiral Bijection"]
FindFrog[f_, t_] := f[[1]] t + f[[2]];
frogSequence = MapThread[FindFrog, {spiral, Range[Length[spiral]]}];
ListPlot[frogSequence, PlotLabel -> "Integer Sequence"]
@dshemetov
Copy link
Author

dshemetov commented May 19, 2018

The output plots for convenience:
screenshot 2018-05-19 15 18 23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment