Little useless-useful R function – Psychedelic Square root with x11()

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Yes, the name of today’s function is wacky, because it gives you the flickering feeling when running it with x11() function. In addition is uses simple square root and cosine function to calculate the points in graph to give it psychedelic feeling.

With the following code you can generate the function to draw the “carpet” patterns.

Psychedelics <- function(number_iterations)
{
  for (i in 1:number_iterations){
      x <- seq(-5*pi, 5*pi, length.out = runif(1, i*2, i*2+1))
      y <- seq(-5*pi, 5*pi, length.out = i)
      # print(x) print(y)
      r <- sqrt(outer(x^2, y^2, "+"))
      # print(r)
      image(z = exp(-r/8)*cos(r^4),  col = brewer.pal(12,"Set3"), xaxt='n', yaxt='n', ann=FALSE)
      Sys.sleep(0.1)
        }
}

And after function is registered in environment, you can run:

## Run all together - when running on Unix / Windows
## screen size can be altered
  x11(width = 7,height = 7)
  Psychedelics(25)
  graphics.off()

The end result is shown below 🙂

Flickering effect with usage of x11() function for simple visualization

As always, the code is available at Github.

Happy R-ing!

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)