diff --git a/DESCRIPTION b/DESCRIPTION index d4f2ff2..7d9309b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,6 @@ Imports: grDevices, igraph, lwgeom, - particles, patchwork, purrr, raster, @@ -44,6 +43,7 @@ Suggests: elevatr, MetBrewer, metR, + particles, PrettyCols, rayshader, rcartocolor, diff --git a/NEWS.md b/NEWS.md index d8fd43f..de33463 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,7 +2,7 @@ * Add `shatter()` * Fix missing namespace in `polygons()` -* Move {rayshader}, {deldir}, {elevatr} from Imports to Suggests +* Move {rayshader}, {deldir}, {elevatr}, {metR}, {particles} from Imports to Suggests * Add {PrettyCols} to Suggests * Lines in `lines()` are now centre aligned * Remove {magrittr} from Imports and use native pipe diff --git a/R/flow_fields.R b/R/flow_fields.R index 24c02e2..f505c64 100644 --- a/R/flow_fields.R +++ b/R/flow_fields.R @@ -22,48 +22,53 @@ flow_fields <- function(n = 10000, line_col = c("#edf8fb", "#bfd3e6", "#9ebcda", "#8c96c6", "#8c6bb1", "#88419d", "#6e016b"), bg_col = "lightgrey", s = 1234) { - set.seed(s) - grid <- ambient::long_grid(seq(1, 10, length.out = granularity), - seq(1, 10, length.out = granularity)) |> - dplyr::mutate( - x1 = .data$x + ambient::gen_simplex(x = .data$x, y = .data$y, frequency = x_freq), - y1 = .data$y + ambient::gen_simplex(x = .data$x, y = .data$y, frequency = y_freq) - ) + # check if {particles} loaded + if (!requireNamespace("particles")) { + stop("Please install {particles} to use this function.") + } else { + set.seed(s) + grid <- ambient::long_grid(seq(1, 10, length.out = granularity), + seq(1, 10, length.out = granularity)) |> + dplyr::mutate( + x1 = .data$x + ambient::gen_simplex(x = .data$x, y = .data$y, frequency = x_freq), + y1 = .data$y + ambient::gen_simplex(x = .data$x, y = .data$y, frequency = y_freq) + ) - curl <- ambient::curl_noise(ambient::gen_perlin, x = grid$x1, y = grid$y1) - grid$angle <- atan2(curl$y, curl$x) - atan2(grid$y1, grid$x1) + curl <- ambient::curl_noise(ambient::gen_perlin, x = grid$x1, y = grid$y1) + grid$angle <- atan2(curl$y, curl$x) - atan2(grid$y1, grid$x1) - field <- as.matrix(grid, grid$x, value = grid$angle) + field <- as.matrix(grid, grid$x, value = grid$angle) - sim <- tidygraph::create_empty(n) |> - particles::simulate(alpha_decay = 0, setup = particles::aquarium_genesis(vel_max = 0)) |> - particles::wield(particles::reset_force, xvel = 0, yvel = 0) |> - particles::wield(particles::field_force, angle = field, vel = 0.1, xlim = c(-5, 5), ylim = c(-5, 5)) |> - particles::evolve(100, particles::record) + sim <- tidygraph::create_empty(n) |> + particles::simulate(alpha_decay = 0, setup = particles::aquarium_genesis(vel_max = 0)) |> + particles::wield(particles::reset_force, xvel = 0, yvel = 0) |> + particles::wield(particles::field_force, angle = field, vel = 0.1, xlim = c(-5, 5), ylim = c(-5, 5)) |> + particles::evolve(100, particles::record) - traces <- data.frame(do.call(rbind, lapply(sim$history, particles::position))) - names(traces) <- c("x", "y") + traces <- data.frame(do.call(rbind, lapply(sim$history, particles::position))) + names(traces) <- c("x", "y") - traces <- - traces |> - dplyr::mutate(particle = rep(1:n, 100)) |> - dplyr::group_by(.data$particle) |> - dplyr::mutate(colour = sample(line_col, 1, replace = TRUE)) + traces <- + traces |> + dplyr::mutate(particle = rep(1:n, 100)) |> + dplyr::group_by(.data$particle) |> + dplyr::mutate(colour = sample(line_col, 1, replace = TRUE)) - p <- ggplot2::ggplot() + - ggplot2::geom_path(data = traces, - mapping = ggplot2::aes(x = .data$x, - y = .data$y, - group = .data$particle, - colour = .data$colour), - size = 0.3, - alpha = alpha) + - ggplot2::coord_cartesian(expand = FALSE) + - ggplot2::scale_color_identity(guide = "none") + - ggplot2::theme_void() + - ggplot2::theme(panel.background = ggplot2::element_rect(fill = bg_col, colour = bg_col), - plot.background = ggplot2::element_rect(fill = bg_col, colour = bg_col), - legend.position = "none", - plot.margin = ggplot2::unit(c(0, 0, 0, 0), "cm")) - p + p <- ggplot2::ggplot() + + ggplot2::geom_path(data = traces, + mapping = ggplot2::aes(x = .data$x, + y = .data$y, + group = .data$particle, + colour = .data$colour), + size = 0.3, + alpha = alpha) + + ggplot2::coord_cartesian(expand = FALSE) + + ggplot2::scale_color_identity(guide = "none") + + ggplot2::theme_void() + + ggplot2::theme(panel.background = ggplot2::element_rect(fill = bg_col, colour = bg_col), + plot.background = ggplot2::element_rect(fill = bg_col, colour = bg_col), + legend.position = "none", + plot.margin = ggplot2::unit(c(0, 0, 0, 0), "cm")) + return(p) + } }