by Andrie de Vries
A few days ago I watched a YouTube video of a TEDx presentation "The surprising beauty of mathematics" by Jonathan Matte at TEDxGreensFarmsAcademy.
In this presentation, Jonathan speaks eloquently about his love for mathematics, and specifically about a way of generating the Archimedes spiral using a series of embedded squares.
A brief Google search led me to Wolfram implementation, but nothing in R, so I set about replicating this image using R.
First I had to brush up my rusty knowledge of matrix math, specifically how to rotate a matrix. Then I had to use pythagoras theorem to calculate how much to scale each square to fit precisely inside its parent. A little fiddling with base graphics, and about 40 lines of code later, and I have this:
Here is the code to create the spiral, as well as a simple shiny app that allows you to tune the parameters:
In the first block of code one can replace the for-loop at the end with recursion using a function such as:
iterplt<-function(x,n) { if (n>0){
polygon(x[,1], x[,2], border="lightblue",col=cols[nsteps -n+1]);
return(iterplt((x * scale) %*% rotate(angle),n-1))}
}
...
plot.new()
par(mai = rep(0.1, 4))
plot(x = range, y = range, type = "n", bty = "n", xaxt = "n",yaxt="n", xlab = NA, ylab=NA)
iterplt(X,nsteps)
Where the functions, functions, values and parameters are as originally defined.
Posted by: Douglas Skinner | July 13, 2015 at 10:04
Very nice, thank you.
Andrie
Posted by: Andrie de Vries | July 13, 2015 at 14:30