Little useless-useful R functions – Dragon curve

[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.

Let’s play with some dragons. Dragons from the Jurassic park or the board game dungeon and dragons.

The algorithm is a fractal curve of Hausdorff dimension 2. One starts with one segment. In each iteration the number of segments is doubled by taking each segment as the diagonal of a square and replacing it by half the square (90 degrees). Alternating and doing the left and right function / direction to complement in order to get the shape.

Assume that we start with just a horizontal line (called Dn) as a segment. Since we created two dragons, we will introduce another line (called Cn, which is a derivate of Dn+1). The Dn+1 is obtained from Dn as follows:
1) Translate Dn, moving its end point to the origin.
2) Multiply the translated copy by √1/2.
3) Rotate the result of previous step by −45◦ degrees and call the result Cn.
3) Rotate Cn by −90◦ degrees and join this rotated copy to the end of Cn to get Dn+1.

Translate this to the function, we get the following IFS (iterated function system).

# Function to generate points using IFS
generate_dragon_curve <- function(iterations) {
  # Initial point
  points <- complex(real = 0, imaginary = 0)
  colors <- c() # Vector to store colors

  # two iterated functions; each for own direction
  f1 <- function(z) {      (1 + 1i) * z / 2 }
  f2 <- function(z) {  1 - (1 - 1i) * z / 2 }
  
  # iterate to generate points
  for (i in 1:iterations) {
    new_points <- vector("complex", length = length(points) * 2)
    new_colors <- vector("character", length = length(points) * 2)
    for (j in 1:length(points)) {
      new_points[2 * j - 1] <- f1(points[j])
      new_colors[2 * j - 1] <- ifelse(i %% 2 == 1, "blue", "red") # Alternating colors
      new_points[2 * j] <- f2(points[j])
      new_colors[2 * j] <- ifelse(i %% 2 == 1, "red", "blue") # Alternating colors
    }
    points <- new_points
    colors <- c(colors, new_colors)
  }
  
  return(list(points = points, colors = colors))
}

Once we have the points, we need to plot the points with corresponding numbers.

plot_dragon_curve <- function(iterations) {
  result <- generate_dragon_curve(iterations)
  points <- result$points
  colors <- result$colors
  plot(Re(points), Im(points), type = "p", pch = ".", col = colors, asp = 1, labels=FALSE, yaxt="n", xaxt="n",
       xlab = "", ylab = "", main = paste("Heighway Dragon Curve with", iterations, "iterations"))
}

And lastly, execute the function (in this case with 15 iterations):

plot_dragon_curve(15)

This plots the dragon curve.

As always, the complete code is available on GitHub in  Useless_R_function repository. The sample file in this repository is here (filename: Two_dragon_fractal.R). Check the repository for future updates.

Happy R-coding and stay healthy!

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)