Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. library(ggplot2)
  2. library(magrittr)
  3. library(parallel)
  4.  
  5. # Padovan Sequence
  6. # 1, 1, 1, 2, 2, 3, 4, ...
  7.  
  8. padovan <- function(n) {
  9. n <- floor(n)
  10. if (n <= 3) stop("n needs to be an integer greater than 3")
  11.  
  12. x <- integer(n)
  13. x[1:3] <- c(1, 1, 1)
  14.  
  15. for( i in seq(4, n, by = 1) ) {
  16. x[i] <- x[i - 2] + x[i - 3]
  17. }
  18. x
  19. }
  20.  
  21. padovan(25)
  22.  
  23.  
  24. sierpinski <- function(x, depth = 0) {
  25. if (depth < 0 | depth > 10) stop()
  26.  
  27. if (depth == 0)
  28. {
  29. x[["tri"]] <- runif(1)
  30. return(x)
  31. } else {
  32.  
  33. va <- x[1, c("x", "y")]
  34. vb <- x[2, c("x", "y")]
  35. vc <- x[3, c("x", "y")]
  36.  
  37. mab <- data.frame(x = mean(c(va$x, vb$x)), y = mean(c(va$y, vb$y)))
  38. mac <- data.frame(x = mean(c(va$x, vc$x)), y = mean(c(va$y, vc$y)))
  39. mbc <- data.frame(x = mean(c(vb$x, vc$x)), y = mean(c(vb$y, vc$y)))
  40.  
  41. do.call(rbind,
  42. list(
  43. sierpinski(data.frame(x = c(va$x, mab$x, mac$x), y = c(va$y, mab$y, mac$y)), depth = depth - 1),
  44. sierpinski(data.frame(x = c(mab$x, vb$x, mbc$x), y = c(mab$y, vb$y, mbc$y)), depth = depth - 1),
  45. sierpinski(data.frame(x = c(mac$x, mbc$x, vc$x), y = c(mac$y, mbc$y, vc$y)), depth = depth - 1)
  46. )
  47. )
  48.  
  49. }
  50. }
  51.  
  52.  
  53.  
  54.  
  55. # starting at the origin
  56. vertices <-
  57. list(
  58. v1 = data.frame(x = 0.0, y = 0.0),
  59. v2 = data.frame(x = 0.5, y = sin(pi / 3)),
  60. v3 = data.frame(x = 1.0, y = 0.0),
  61. v4 = data.frame(x = 0.5, y = -sin(pi / 3)),
  62. v5 = data.frame(x = -0.5, y = -sin(pi / 3))
  63. )
  64.  
  65. vertices$v6$x <- vertices$v5$x - 1
  66. vertices$v6$y <- vertices$v2$y
  67.  
  68. vertices$v7$x <- vertices$v5$x
  69. vertices$v7$y <- vertices$v2$y + 2 * sin(pi / 3)
  70.  
  71. vertices$v8$x <- vertices$v7$x + 3
  72. vertices$v8$y <- vertices$v7$y
  73.  
  74. vertices$v9$x <- vertices$v4$x + 4
  75. vertices$v9$y <- vertices$v4$y
  76.  
  77. vertices$v10$x <- mean(c(vertices$v5$x, vertices$v9$x))
  78. vertices$v10$y <- vertices$v9$y - 5 * sin(pi / 3)
  79.  
  80. vertices$v11$x <- vertices$v10$x - 7
  81. vertices$v11$y <- vertices$v10$y
  82.  
  83. padovan(10)
  84. vertices$v12$x <- vertices$v7$x - 9
  85. vertices$v12$y <- vertices$v7$y
  86.  
  87. padovan(11)
  88. vertices$v13$x <- mean(c(vertices$v12$x, vertices$v8$x))
  89. vertices$v13$y <- vertices$v7$y + 12 * sin(pi / 3)
  90.  
  91. padovan(12)
  92. vertices$v14$x <- vertices$v13$x + 16
  93. vertices$v14$y <- vertices$v13$y
  94.  
  95. vertices <- dplyr::bind_rows(vertices, .id = "vertex")
  96.  
  97. triangles <-
  98. list(
  99. t001 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v1", "v2", "v3")), padovan = 1),
  100. t002 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v1", "v3", "v4")), padovan = 1),
  101. t003 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v1", "v4", "v5")), padovan = 1),
  102. t004 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v5", "v6", "v2")), padovan = tail(padovan(4), 1)),
  103. t005 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v6", "v2", "v7")), padovan = tail(padovan(5), 1)),
  104. t006 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v7", "v3", "v8")), padovan = tail(padovan(6), 1)),
  105. t007 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v8", "v9", "v4")), padovan = tail(padovan(7), 1)),
  106. t008 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v5", "v9", "v10")), padovan = tail(padovan(8), 1)),
  107. t009 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v6", "v10", "v11")), padovan = tail(padovan(9), 1)),
  108. t010 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v7", "v11", "v12")), padovan = tail(padovan(10), 1)),
  109. t011 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v12", "v13", "v8")), padovan = tail(padovan(11), 1)),
  110. t012 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v13", "v9", "v14")), padovan = tail(padovan(12), 1))
  111. )
  112.  
  113. triangles <-
  114. triangles %>%
  115. unname %>%
  116. dplyr::bind_rows(., .id = "n") %>%
  117. dplyr::mutate(n = as.numeric(n))
  118.  
  119.  
  120. ggplot() +
  121. theme_bw() +
  122. # theme_dark() +
  123. # theme_minimal() +
  124. aes(x = x, y = y) +
  125. geom_polygon(data = triangles, mapping = aes(group = n), color = "black") +
  126. # geom_text(data = vertices, mapping = aes(label = vertex)) +
  127. geom_text(data = dplyr::summarize(dplyr::group_by(triangles, n, padovan), x = mean(x), y = mean(y)),
  128. mapping = aes(x = x, y = y, group = n, label = padovan), color = 'white') +
  129. coord_equal() +
  130. guides(fill = FALSE) +
  131. theme(
  132. axis.title = element_blank(),
  133. axis.text = element_blank(),
  134. axis.ticks = element_blank(),
  135. axis.line = element_blank(),
  136. panel.border = element_blank(),
  137. panel.grid = element_blank()
  138. ) +
  139. ggtitle("Padovan Sequence")
  140.  
  141.  
  142. mclapply(unique(triangles$n), function(nn) { sierpinski(dplyr::filter(triangles, n == nn), depth = 7) },
  143. mc.cores = 10L) %>%
  144. dplyr::bind_rows(., .id = "pad_seq") %>%
  145. ggplot(.) +
  146. theme_bw() +
  147. aes(x = x, y = y, group = tri, fill = pad_seq) +
  148. geom_polygon() +
  149. coord_equal()+
  150. guides(fill = FALSE) +
  151. theme(
  152. axis.title = element_blank(),
  153. axis.text = element_blank(),
  154. axis.ticks = element_blank(),
  155. axis.line = element_blank(),
  156. panel.border = element_blank(),
  157. panel.grid = element_blank()
  158. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement