@@ -26,49 +26,53 @@ flow_fields <- function(n = 10000,
26
26
if (! requireNamespace(" particles" )) {
27
27
stop(" Please install {particles} to use this function." )
28
28
} else {
29
- set.seed(s )
30
- grid <- ambient :: long_grid(seq(1 , 10 , length.out = granularity ),
31
- seq(1 , 10 , length.out = granularity )) | >
32
- dplyr :: mutate(
33
- x1 = .data $ x + ambient :: gen_simplex(x = .data $ x , y = .data $ y , frequency = x_freq ),
34
- y1 = .data $ y + ambient :: gen_simplex(x = .data $ x , y = .data $ y , frequency = y_freq )
35
- )
29
+ if (! requireNamespace(" ambient" )) {
30
+ stop(" Please install {ambient} to use this function." )
31
+ } else {
32
+ set.seed(s )
33
+ grid <- ambient :: long_grid(seq(1 , 10 , length.out = granularity ),
34
+ seq(1 , 10 , length.out = granularity )) | >
35
+ dplyr :: mutate(
36
+ x1 = .data $ x + ambient :: gen_simplex(x = .data $ x , y = .data $ y , frequency = x_freq ),
37
+ y1 = .data $ y + ambient :: gen_simplex(x = .data $ x , y = .data $ y , frequency = y_freq )
38
+ )
36
39
37
- curl <- ambient :: curl_noise(ambient :: gen_perlin , x = grid $ x1 , y = grid $ y1 )
38
- grid $ angle <- atan2(curl $ y , curl $ x ) - atan2(grid $ y1 , grid $ x1 )
40
+ curl <- ambient :: curl_noise(ambient :: gen_perlin , x = grid $ x1 , y = grid $ y1 )
41
+ grid $ angle <- atan2(curl $ y , curl $ x ) - atan2(grid $ y1 , grid $ x1 )
39
42
40
- field <- as.matrix(grid , grid $ x , value = grid $ angle )
43
+ field <- as.matrix(grid , grid $ x , value = grid $ angle )
41
44
42
- sim <- tidygraph :: create_empty(n ) | >
43
- particles :: simulate(alpha_decay = 0 , setup = particles :: aquarium_genesis(vel_max = 0 )) | >
44
- particles :: wield(particles :: reset_force , xvel = 0 , yvel = 0 ) | >
45
- particles :: wield(particles :: field_force , angle = field , vel = 0.1 , xlim = c(- 5 , 5 ), ylim = c(- 5 , 5 )) | >
46
- particles :: evolve(100 , particles :: record )
45
+ sim <- tidygraph :: create_empty(n ) | >
46
+ particles :: simulate(alpha_decay = 0 , setup = particles :: aquarium_genesis(vel_max = 0 )) | >
47
+ particles :: wield(particles :: reset_force , xvel = 0 , yvel = 0 ) | >
48
+ particles :: wield(particles :: field_force , angle = field , vel = 0.1 , xlim = c(- 5 , 5 ), ylim = c(- 5 , 5 )) | >
49
+ particles :: evolve(100 , particles :: record )
47
50
48
- traces <- data.frame (do.call(rbind , lapply(sim $ history , particles :: position )))
49
- names(traces ) <- c(" x" , " y" )
51
+ traces <- data.frame (do.call(rbind , lapply(sim $ history , particles :: position )))
52
+ names(traces ) <- c(" x" , " y" )
50
53
51
- traces <-
52
- traces | >
53
- dplyr :: mutate(particle = rep(1 : n , 100 )) | >
54
- dplyr :: group_by(.data $ particle ) | >
55
- dplyr :: mutate(colour = sample(line_col , 1 , replace = TRUE ))
54
+ traces <-
55
+ traces | >
56
+ dplyr :: mutate(particle = rep(1 : n , 100 )) | >
57
+ dplyr :: group_by(.data $ particle ) | >
58
+ dplyr :: mutate(colour = sample(line_col , 1 , replace = TRUE ))
56
59
57
- p <- ggplot2 :: ggplot() +
58
- ggplot2 :: geom_path(data = traces ,
59
- mapping = ggplot2 :: aes(x = .data $ x ,
60
- y = .data $ y ,
61
- group = .data $ particle ,
62
- colour = .data $ colour ),
63
- size = 0.3 ,
64
- alpha = alpha ) +
65
- ggplot2 :: coord_cartesian(expand = FALSE ) +
66
- ggplot2 :: scale_color_identity(guide = " none" ) +
67
- ggplot2 :: theme_void() +
68
- ggplot2 :: theme(panel.background = ggplot2 :: element_rect(fill = bg_col , colour = bg_col ),
69
- plot.background = ggplot2 :: element_rect(fill = bg_col , colour = bg_col ),
70
- legend.position = " none" ,
71
- plot.margin = ggplot2 :: unit(c(0 , 0 , 0 , 0 ), " cm" ))
72
- return (p )
60
+ p <- ggplot2 :: ggplot() +
61
+ ggplot2 :: geom_path(data = traces ,
62
+ mapping = ggplot2 :: aes(x = .data $ x ,
63
+ y = .data $ y ,
64
+ group = .data $ particle ,
65
+ colour = .data $ colour ),
66
+ size = 0.3 ,
67
+ alpha = alpha ) +
68
+ ggplot2 :: coord_cartesian(expand = FALSE ) +
69
+ ggplot2 :: scale_color_identity(guide = " none" ) +
70
+ ggplot2 :: theme_void() +
71
+ ggplot2 :: theme(panel.background = ggplot2 :: element_rect(fill = bg_col , colour = bg_col ),
72
+ plot.background = ggplot2 :: element_rect(fill = bg_col , colour = bg_col ),
73
+ legend.position = " none" ,
74
+ plot.margin = ggplot2 :: unit(c(0 , 0 , 0 , 0 ), " cm" ))
75
+ return (p )
76
+ }
73
77
}
74
78
}
0 commit comments