Animation basics for a vacation

August 5, 2012

(This article was first published on R snippets, and kindly contributed to R-bloggers)

Since I have a vacation this time I decided to implement some entertaining graphics. I have chosen to animate a Cassini oval.
The task is can be accomplished using polar equation:

The implementation of the animation is given by the following code:

xy <- function(angle, a, b) {
  ca <- cos(2 * angle)
  r2 <- a ^ 2 * ca + sqrt(a ^ 4 * ca ^ 2 (a ^ 4 b ^ 4))
  c(sqrt(r2) * cos(angle), sqrt(r2) * sin(angle))
go <- function() {
  angle <- seq(0, 2 * pi, len = 1000)
  par(mar = rep(0,4))
  b <- 1 + 0.5 * seq(0, 1, len = 31) ^ 3
  b <- c(b, 1.5, rev(b))
  for (i in b) {
    coord <- t(sapply(angle, xy, a = 1, b = i))
    plot(coord, type = “l”,
      xlim = c(-2, 2), ylim = c(-1.25, 1.25))
    polygon(coord, col = “gray”)
ani.options(interval = 0.1)

Parameter a is fixed to 1 and b changes from 1 to 1.5. In order to achieve smooth animation the sequence defining changes of b is not uniform but is more dense near 1.

And here is the result:

To leave a comment for the author, please follow the link and comment on their blog: R snippets. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


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)