Skip to content

Instantly share code, notes, and snippets.

@TobCap
Last active June 23, 2017 07:07
Show Gist options
  • Save TobCap/ad709a0d4d286485a7a084df54ce9849 to your computer and use it in GitHub Desktop.
Save TobCap/ad709a0d4d286485a7a084df54ce9849 to your computer and use it in GitHub Desktop.
# http://qiita.com/HikaruR/items/c67b6c9714c51d70333c
# accessed on 2017-06-23
elem_n <- function(n, ...) {
dots <- list(...)
unlist(lapply(dots, `[`, n))
}
polygon_y123 <- function(y1, y2, y3, ...) {
polygon(x = elem_n(1, y1, y2, y3), y = elem_n(2, y1, y2, y3), ...)
}
Sierpinski.gasket2 <- function(depth,
y1 = c(0, 1),
y2 = c(-1, -1),
y3 = c(1, -1)
) {
if (depth > 15) {
stop("may be too deep to draw chart")
}
plot.new()
plot.window(xlim = range(elem_n(1, y1, y2, y3)),
ylim = range(elem_n(2, y1, y2, y3)))
# most-out boader
polygon_y123(y1, y2, y3, col = "gray")
(function(d, y1, y2, y3) {
if (d == 0) {
return(invisible())
}
y12 = (y1 + y2) / 2
y23 = (y2 + y3) / 2
y13 = (y1 + y3) / 2
polygon_y123(y12, y13, y23, col = "white")
Recall(d - 1, y1, y12, y13)
Recall(d - 1, y12, y2, y23)
Recall(d - 1, y13, y23, y3)
invisible()
})(depth, y1, y2, y3)
}
Sierpinski.gasket2(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment