# # ################## The old toy: Spirograph (from Hasbro). ###################### # # See here: # http://en.wikipedia.org/wiki/Spirograph # and here: # http://www.math.psu.edu/dlittle/java/parametricequations/spirograph/index.html # and here: # http://www.hasbro.com/shop/details.cfm?R=B3E17AC0-5056-900B-103C-1885EF37623B:en_US library(RColorBrewer) x.fun <- function(p,R,r,t){ x <- (R + r)*cos(t) + p*cos((R+r)*t/r) return(x) } y.fun <- function(p,R,r,t){ y <- (R + r)*sin(t) + p*sin((R+r)*t/r) return(y) } # Initial conditions: p <- 60 # Position R <- 58 # Radius of circle 1. r <- 62 # Radius of circle 2. X <- seq(-200,200, by = 1) Y <- seq(-200,200, by = 1) plot(X, Y, xlab = "X", ylab = "Y", xlim = c(-200,200), ylim = c(-200,200), col = "white") lines(x = c(-220,220), y = c(0,0), lwd = 1, col = "gray") lines(x = c(0,0), y = c(-220,220), lwd = 1, col = "gray") my.col <- brewer.pal(9,"Blues") # See bottom of script for other color palette choices. my.col <- my.col[9:1] # Starts dark and transitions to white. mc <- 1 # Changes the color. z <- 965 # Approximately one revolution; given: p = 60, R = 58, r = 62 from above. mc.n <- z # Determines when the color changes. n <- 10708 T <- 1 for (i in 1:n){ new.x <- x.fun(p, R, r, t = T) new.y <- y.fun(p, R, r, t = T) par(new = T) plot(new.x, new.y, xlab = "X", ylab = "Y", xlim = c(-200,200), ylim = c(-200,200), col = my.col[mc]) T <- T + .1 if(i == mc.n){mc.n <- mc.n + z + (mc*2); mc <- mc + 1} if(mc > 9){mc <- 1} Sys.sleep(.01) if(i == n){print("DONE")} } # Feel free to change p, R, r (in the initial conditions above); but beware, minor # changes can drastically change the pattern. ################################################################################ # THE COLOR WHEEL.... Thank you to Cynthia Brewer, Mark Harrower, and those at # Pennsylvania State University for developing this very handy package (RColorBrewer). help(brewer.pal) # The sequential palette names are # Blues BuGn BuPu GnBu Greens Greys Oranges OrRd PuBu PuBuGn PuRd # Purples RdPu Reds YlGn YlGnBu YlOrBr YlOrRd probs <- rep(1,9) probs <- probs/(sum(probs)) pie(probs, col = my.col, main = "Blues") sum(probs) # To see all 18 of the "sequential palettes" run the following. mi.colores <- c("Blues", "BuGn", "BuPu", "GnBu", "Greens", "Greys", "Oranges", "OrRd", "PuBu", "PuBuGn", "PuRd", "Purples", "RdPu", "Reds", "YlGn", "YlGnBu", "YlOrBr", "YlOrRd") for(i in 1:18){ my.col <- brewer.pal(09, mi.colores[i]) my.col <- my.col[9:1] Label <- mi.colores[i] pie(probs, col = my.col, main = Label) Sys.sleep(2.5) } # End; Nov. 29, 2011.