Farbfetzen / fractalplotr

Make beautiful fractals with R
Other
0 stars 0 forks source link

ulam-warburton_cellular-automaton #19

Open Farbfetzen opened 5 years ago

Farbfetzen commented 5 years ago

Call the function uwca or ulam_warburton (make those names aliases).

plot_cells <- function(m) {
    m <- abs(m - 1)  # change 0 to 1 and 1 to 0
    grid::grid.newpage()
    grid::grid.raster(m, interpolate = FALSE)
}

n <- 64
sidelength <- 2 * n - 1
m <- matrix(0, nrow = sidelength, ncol = sidelength)
m[ceiling(sidelength/2), ceiling(sidelength/2)] <- 1
for (i in seq_len(n - 1)) {
    left <- cbind(m[, -1], 0)
    right <- cbind(0, m[, -sidelength])
    top <- rbind(m[-1, ], 0)
    bottom <- rbind(0, m[-sidelength, ])
    new <- left + right + top + bottom + m
    new[new > 1] <- 0
    m <- m + new
    m[m > 1] <- 1
}
plot_cells(new)
plot_cells(m)
Farbfetzen commented 5 years ago

https://www.youtube.com/watch?v=_UtCli1SgjI

In the video Sloane says there is a simple formula for the number of squares after n generations. Use this to initialize the matrix with an approximate upper bound. Afterwards cut off the excess.

Also the whole thing is symmetric so maybe I could speed it up by only iterating over a quarter.