randomwalk = function(cluster, start, limiters) 
  {
    boundary = unique(c(cluster + 1, cluster - 1, cluster - 1i, cluster + 1i))
    present = start
    currentlimiter = 1
    nextlimiter = 2
    while(!(present %in% boundary)) 
      {
        direction = sample(c(1, -1, 1i, -1i), size = 1, replace = T)
        present = present + direction
        if(abs(present) < limiters[nextlimiter]) 
          {
            currentlimiter = nextlimiter
            nextlimiter = min(nextlimiter + 1, length(limiters))
          }
        if(abs(present) > limiters[currentlimiter]) 
          {
            present = present - (2 * direction)
          }
      }
    return(present)
  }
simulation_DLA <- function(x = c(0,0), radius = 100, iterations = 2500)
{
  seed = complex(real = x[1], imaginary = x[2])
  limiters = c(1.25 * radius, 0.625 * radius, 0.3125 * radius)
  cluster = c(seed)
  pointlist = unique(round(replicate(800,complex(modulus = radius, 
                                  argument = runif(1, min = -pi, max = pi)))))
  for(i in 1:iterations) 
  {
    start = sample(pointlist, size = 1, replace = T)
    newpoint = randomwalk(cluster, start, limiters)
    cluster = c(cluster, newpoint)
  }
  return(cluster)
}
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(viridis)
## Loading required package: viridisLite
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.3.3
library(gifski)
## Warning: package 'gifski' was built under R version 4.3.3
cluster <- simulation_DLA()
df <- data.frame(xval = Re(cluster), yval = Im(cluster), steps = 1:length(cluster))
anim_plot <- ggplot(data = df) + 
geom_point(mapping = aes(x = xval, y = yval), size = 0.9,show.legend = F,
             color = turbo(length(cluster))) + 
  theme(panel.background = element_rect(fill = "black"),
        panel.grid = element_blank(),plot.background = element_rect(fill = "black"))+
  transition_states(steps, transition_length = 2,, state_length = 1) + shadow_mark()
animate(anim_plot, fps = 3)

library(RColorBrewer)
f <- function(pal) brewer.pal(brewer.pal.info[pal, "maxcolors"],pal)
mycolor <- rev(f("RdYlBu"))
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- plot_ly(x = df$xval, y = df$yval, z = df$steps, size = 1, color = df$steps,type = "scatter3d", mode = "markers", colors = mycolor, height = 500, width = 700)
layout(p, paper_bgcolor = "#000000", plot_bgcolor = "#000000")