The Minsky circle algorithm
nbickford.wordpress.com
nbickford.wordpress.com
y -= x >> 4;
x += y >> 4;
and is basically a very rough approximation of a step in the CORDIC[1] algorithm, which computes the sin and cos of an angle by rotating a vector; it's then quite natural that successive rotations would trace out an approximation to a circle.As such, many of the first programs of the PDP-1 were “Display hacks”: Programs using only a few lines of code, but when run, create intricate patterns on the screen.
The demoscene has carried on that tradition, and in particular the sub-512b categories produce very interesting graphics from tiny programs.
x = sin t , y = cos t
the derivative is
d(sin t) = (cos t) dt , d(cos t) = -(sin t) st
so it's interesting that it was "stumbled" upon.
Assuming deltat=16, we have:
y' = -x
x' = y
Which is solved with y=cos and x=sin or with x=-cos and y=sin.
The fact that one line adds and the other substracts may be explain why rounding errors will compensate each other over time, but we need a more detailed working of the PDP arithmetics to be really sure about that.
Actually, many CS students may recognize a prey-and-predator model, that creates very simply sinusoids that are dephased of pi/2. That is, if one is a sine, the other is a cosine.
I noticed this doesn't run on my iPhone (SE), because XOR is unsupported in GLES 2.0, so I hacked the shader up a bit to be compatible. (and because I'd never had the opportunity to implement XOR using integer arithmetic)
For whatever reason, I could not get my hacked ShaderToy version to work on my phone (possibly a Shadertoy uniform bug?), even though it compiled in the iPhone's browser without issue, and even though it works just fine on my laptop. A slightly modified version of my hacked shader works as expected on my phone in the Book of Shaders editor. I had to "guess" at a reasonable iFrame value by dividing u_time by 60.0, and since the BoS editor doesn't support "resetting time" without reloading, I made it "loop" with a hardcoded loop time of 9 seconds.
shadertoy: https://www.shadertoy.com/view/Mlscz2
book of shaders (code): http://thebookofshaders.com/edit.php?log=170917091706
book of shaders (player): http://player.thebookofshaders.com/?log=170917091706
David Mapes invented the same algorithm, also on the PDP-1, at Lawrence Livermore National Laboratory (LLNL). While Minsky came across this algorithm by accident, Mapes arrived there by design. See http://www.masswerk.at/minskytron/davidmapes.html
The third co-author “R.W. Gosper” is Bill Gosper https://en.wikipedia.org/wiki/Bill_Gosper, who discovered the glider gun and the hashlife algorithm (linked from the Wikipedia page).
sin(a+b)=sin(a)cos(b)+cos(a)sin(b)
cos(a+b)=cos(a)cos(b)-sin(a)sin(b)
But the mystery is that experimentally it's more correct than that. Why does it seem to join up with itself exactly after 360°, and not spiral slightly inward or outward due to accumulated error? Why does it stop working if you use a temporary variable so that x and y are updated simultaneously instead of alternately?
Now that I have read this article, I can recognize that code was the Minsky Circle Algorithm.
Fantastic!