Skip to content

Commit

Permalink
move particles to suggests
Browse files Browse the repository at this point in the history
  • Loading branch information
nrennie committed Feb 14, 2023
1 parent bcada46 commit 3f0ec48
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Imports:
grDevices,
igraph,
lwgeom,
particles,
patchwork,
purrr,
raster,
Expand All @@ -44,6 +43,7 @@ Suggests:
elevatr,
MetBrewer,
metR,
particles,
PrettyCols,
rayshader,
rcartocolor,
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 43 additions & 38 deletions R/flow_fields.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 3f0ec48

Please sign in to comment.