Skip to content

Instantly share code, notes, and snippets.

@dewittpe
Created September 30, 2019 16:57
Show Gist options
  • Save dewittpe/a1bf936c419a96fd06aa6c3e07dc718f to your computer and use it in GitHub Desktop.
Save dewittpe/a1bf936c419a96fd06aa6c3e07dc718f to your computer and use it in GitHub Desktop.
Padovan Sequence and sierpinski trianagles
library(ggplot2)
library(magrittr)
library(parallel)
# Padovan Sequence
# 1, 1, 1, 2, 2, 3, 4, ...
padovan <- function(n) {
n <- floor(n)
if (n <= 3) stop("n needs to be an integer greater than 3")
x <- integer(n)
x[1:3] <- c(1, 1, 1)
for( i in seq(4, n, by = 1) ) {
x[i] <- x[i - 2] + x[i - 3]
}
x
}
padovan(25)
sierpinski <- function(x, depth = 0) {
if (depth < 0 | depth > 10) stop()
if (depth == 0)
{
x[["tri"]] <- runif(1)
return(x)
} else {
va <- x[1, c("x", "y")]
vb <- x[2, c("x", "y")]
vc <- x[3, c("x", "y")]
mab <- data.frame(x = mean(c(va$x, vb$x)), y = mean(c(va$y, vb$y)))
mac <- data.frame(x = mean(c(va$x, vc$x)), y = mean(c(va$y, vc$y)))
mbc <- data.frame(x = mean(c(vb$x, vc$x)), y = mean(c(vb$y, vc$y)))
do.call(rbind,
list(
sierpinski(data.frame(x = c(va$x, mab$x, mac$x), y = c(va$y, mab$y, mac$y)), depth = depth - 1),
sierpinski(data.frame(x = c(mab$x, vb$x, mbc$x), y = c(mab$y, vb$y, mbc$y)), depth = depth - 1),
sierpinski(data.frame(x = c(mac$x, mbc$x, vc$x), y = c(mac$y, mbc$y, vc$y)), depth = depth - 1)
)
)
}
}
# starting at the origin
vertices <-
list(
v1 = data.frame(x = 0.0, y = 0.0),
v2 = data.frame(x = 0.5, y = sin(pi / 3)),
v3 = data.frame(x = 1.0, y = 0.0),
v4 = data.frame(x = 0.5, y = -sin(pi / 3)),
v5 = data.frame(x = -0.5, y = -sin(pi / 3))
)
vertices$v6$x <- vertices$v5$x - 1
vertices$v6$y <- vertices$v2$y
vertices$v7$x <- vertices$v5$x
vertices$v7$y <- vertices$v2$y + 2 * sin(pi / 3)
vertices$v8$x <- vertices$v7$x + 3
vertices$v8$y <- vertices$v7$y
vertices$v9$x <- vertices$v4$x + 4
vertices$v9$y <- vertices$v4$y
vertices$v10$x <- mean(c(vertices$v5$x, vertices$v9$x))
vertices$v10$y <- vertices$v9$y - 5 * sin(pi / 3)
vertices$v11$x <- vertices$v10$x - 7
vertices$v11$y <- vertices$v10$y
padovan(10)
vertices$v12$x <- vertices$v7$x - 9
vertices$v12$y <- vertices$v7$y
padovan(11)
vertices$v13$x <- mean(c(vertices$v12$x, vertices$v8$x))
vertices$v13$y <- vertices$v7$y + 12 * sin(pi / 3)
padovan(12)
vertices$v14$x <- vertices$v13$x + 16
vertices$v14$y <- vertices$v13$y
vertices <- dplyr::bind_rows(vertices, .id = "vertex")
triangles <-
list(
t001 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v1", "v2", "v3")), padovan = 1),
t002 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v1", "v3", "v4")), padovan = 1),
t003 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v1", "v4", "v5")), padovan = 1),
t004 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v5", "v6", "v2")), padovan = tail(padovan(4), 1)),
t005 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v6", "v2", "v7")), padovan = tail(padovan(5), 1)),
t006 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v7", "v3", "v8")), padovan = tail(padovan(6), 1)),
t007 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v8", "v9", "v4")), padovan = tail(padovan(7), 1)),
t008 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v5", "v9", "v10")), padovan = tail(padovan(8), 1)),
t009 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v6", "v10", "v11")), padovan = tail(padovan(9), 1)),
t010 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v7", "v11", "v12")), padovan = tail(padovan(10), 1)),
t011 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v12", "v13", "v8")), padovan = tail(padovan(11), 1)),
t012 = dplyr::mutate(dplyr::filter(vertices, .data$vertex %in% c("v13", "v9", "v14")), padovan = tail(padovan(12), 1))
)
triangles <-
triangles %>%
unname %>%
dplyr::bind_rows(., .id = "n") %>%
dplyr::mutate(n = as.numeric(n))
ggplot() +
theme_bw() +
# theme_dark() +
# theme_minimal() +
aes(x = x, y = y) +
geom_polygon(data = triangles, mapping = aes(group = n), color = "black") +
# geom_text(data = vertices, mapping = aes(label = vertex)) +
geom_text(data = dplyr::summarize(dplyr::group_by(triangles, n, padovan), x = mean(x), y = mean(y)),
mapping = aes(x = x, y = y, group = n, label = padovan), color = 'white') +
coord_equal() +
guides(fill = FALSE) +
theme(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank()
) +
ggtitle("Padovan Sequence")
mclapply(unique(triangles$n), function(nn) { sierpinski(dplyr::filter(triangles, n == nn), depth = 7) },
mc.cores = 10L) %>%
dplyr::bind_rows(., .id = "pad_seq") %>%
ggplot(.) +
theme_bw() +
aes(x = x, y = y, group = tri, fill = pad_seq) +
geom_polygon() +
coord_equal()+
guides(fill = FALSE) +
theme(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment