Plot dragon curve in R

I found a code to make a sequence for n itterations of the dragons code.

mountain <- 1
Valley <- -1

curve <- function(n) {
  if (n == 1) {
    return(Valley)
  }
  if (n >= 1) {
    folds <- c(curve(n - 1), Valley, rev(-curve(n - 1)))  
    return(folds)
  }
}

Now I would like to turn the sequence this creates into coordinates, and then plot the graph. Every 1 should resemble a fold 90 degree left, and -1 a 90 degree fold right. I have no idea how to do this however. Could anybody help?

This is the question:

Create a function that turns a sequence of +1’s and -1’s into coordinates. Plot the coordinates to recreate the side-view of a piece of paper when every fold is held at a 90-degree angle.

HINT 1: Notice that if you move along the line, +1 means a turn to the left and -1 a turn to the right. You can model this by moving one unit in the coordinate plane, changing direction, moving one unit in the new direction, etc.

HINT 2: If you are familiar with complex numbers, they make this subquestion a lot easier. In particular, notice that turning counter-clockwise is the same as multiplying by i in the complex plane, while turning clockwise is the same as multiplying by −i. Complex numbers can be created in R using complex



Read more here: https://stackoverflow.com/questions/66278399/plot-dragon-curve-in-r

Content Attribution

This content was originally published by Ilse Jansen at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: