Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Let's try a different approach with a Barnsley fern
- generate_barnsley_fern <- function(n_points = 50000) {
- # Initialize points array
- points <- data.frame(x = numeric(n_points), y = numeric(n_points))
- # Start with (0,0)
- x <- 0
- y <- 0
- # Fill the points
- for(i in 1:n_points) {
- # Store the current point
- points$x[i] <- x
- points$y[i] <- y
- # Choose a random transformation
- r <- runif(1)
- if(r < 0.01) {
- # Transformation 1 (stem)
- x_new <- 0
- y_new <- 0.16 * y
- } else if(r < 0.86) {
- # Transformation 2 (smaller leaflets)
- x_new <- 0.85 * x + 0.04 * y
- y_new <- -0.04 * x + 0.85 * y + 1.6
- } else if(r < 0.93) {
- # Transformation 3 (left leaflet)
- x_new <- 0.20 * x - 0.26 * y
- y_new <- 0.23 * x + 0.22 * y + 1.6
- } else {
- # Transformation 4 (right leaflet)
- x_new <- -0.15 * x + 0.28 * y
- y_new <- 0.26 * x + 0.24 * y + 0.44
- }
- # Update for next iteration
- x <- x_new
- y <- y_new
- }
- # Add a group to color by
- points <- points %>%
- dplyr::mutate(
- group = ceiling(1:n_points / (n_points/7)) # Create 7 groups
- )
- return(points)
- }
- # Generate the fractal
- set.seed(42)
- fern <- generate_barnsley_fern(50000)
- # Plot the fern
- p <- ggplot(fern, aes(x, y, color = factor(group))) +
- geom_point(size = 0.1, alpha = 0.6) +
- scale_color_viridis_d(option = "D") +
- theme_void() +
- theme(
- legend.position = "none",
- panel.background = element_rect(fill = "#0E0E18"),
- plot.background = element_rect(fill = "#0E0E18")
- ) +
- labs(title = "Fractal Dreams") +
- coord_fixed(ratio = 1)
- print(p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement