Skip to content

Commit

Permalink
fix: move ambient to suggests
Browse files Browse the repository at this point in the history
  • Loading branch information
nrennie committed Feb 19, 2023
1 parent 50539bc commit 4c30053
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ RoxygenNote: 7.2.3
Depends:
R (>= 4.1)
Imports:
ambient,
ape,
cowplot,
dplyr,
Expand All @@ -39,6 +38,7 @@ Imports:
tidyr,
treemapify
Suggests:
ambient,
deldir,
elevatr,
ggstream,
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## aRt 1.4.2

* Add `gradients()`
* Move {ambient} to Suggests

## aRt 1.4.1

Expand Down
80 changes: 42 additions & 38 deletions R/flow_fields.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,53 @@ flow_fields <- function(n = 10000,
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)
)
if (!requireNamespace("ambient")) {
stop("Please install {ambient} 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"))
return(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 4c30053

Please sign in to comment.