#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Prepare the directory for output images
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
root_dir <- here::here("vignettes/")
png_dir <- file.path(root_dir, "png")
unlink(list.files(png_dir, pattern = "*.png", full.names = TRUE))
steps <- 600; advance <- 6
# steps <- 60; advance <- 60
# steps <- 120; advance <- 30
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# (1) advance the simulation (2) plot the circles (3) Repeat.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (i in seq_len(steps)) {
cm$advance(advance)
circles <- cm$get_circles()
polys <- cm$get_polygons()
if (i %% 10 == 0) {
message(i)
# print(polys)
}
p <- ggplot() +
geom_point(data = circles, aes(x, y, size = I(2.845*r)), colour = 'black') +
geom_polygon(data = polys , aes(xvertex, yvertex), fill = '#445566') +
geom_segment(data = segments_df, aes(x = x1, y = y1, xend = x2, yend = y2), colour = 'lightblue') +
coord_fixed(xlim = c(-70, 70), ylim = c(0, 100)) +
theme_void() +
theme(legend.position = 'none')
outfile <- sprintf("vignettes/png/%04i.png", i)
ggsave(outfile, p, width = 7, height = 7)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ffmpeg/gifsicle to create animations
# - create mp4 from PNG files (use in vignettes)
# - create gif from mp4 (use in github readme)
# - simplify gif with gifsicle
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
root_name <- "plop-polygon"
mp4_name <- paste0(root_dir, "/anim/", root_name, ".mp4")
tmp_name <- tempfile(fileext = ".gif")
gif_name <- paste0(root_dir, "/anim/", root_name, ".gif")
system(glue::glue("ffmpeg -y -framerate 20 -pattern_type glob -i '{png_dir}/*.png' -c:v libx264 -pix_fmt yuv420p -s 800x800 '{mp4_name}'"))
# system(glue::glue("ffmpeg -y -i '{mp4_name}' -filter_complex 'fps=30,scale=800:-1:flags=lanczos,split [o1] [o2];[o1] palettegen [p]; [o2] fifo [o3];[o3] [p] paletteuse' '{tmp_name}'"))
# system(glue::glue("gifsicle -O99 -o '{gif_name}' -k 128 '{tmp_name}'"))