Advertisement
Guest User

bansley_fern

a guest
Mar 19th, 2025
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1.  
  2. # Let's try a different approach with a Barnsley fern
  3. generate_barnsley_fern <- function(n_points = 50000) {
  4. # Initialize points array
  5. points <- data.frame(x = numeric(n_points), y = numeric(n_points))
  6.  
  7. # Start with (0,0)
  8. x <- 0
  9. y <- 0
  10.  
  11. # Fill the points
  12. for(i in 1:n_points) {
  13. # Store the current point
  14. points$x[i] <- x
  15. points$y[i] <- y
  16.  
  17. # Choose a random transformation
  18. r <- runif(1)
  19.  
  20. if(r < 0.01) {
  21. # Transformation 1 (stem)
  22. x_new <- 0
  23. y_new <- 0.16 * y
  24. } else if(r < 0.86) {
  25. # Transformation 2 (smaller leaflets)
  26. x_new <- 0.85 * x + 0.04 * y
  27. y_new <- -0.04 * x + 0.85 * y + 1.6
  28. } else if(r < 0.93) {
  29. # Transformation 3 (left leaflet)
  30. x_new <- 0.20 * x - 0.26 * y
  31. y_new <- 0.23 * x + 0.22 * y + 1.6
  32. } else {
  33. # Transformation 4 (right leaflet)
  34. x_new <- -0.15 * x + 0.28 * y
  35. y_new <- 0.26 * x + 0.24 * y + 0.44
  36. }
  37.  
  38. # Update for next iteration
  39. x <- x_new
  40. y <- y_new
  41. }
  42.  
  43. # Add a group to color by
  44. points <- points %>%
  45. dplyr::mutate(
  46. group = ceiling(1:n_points / (n_points/7)) # Create 7 groups
  47. )
  48.  
  49. return(points)
  50. }
  51.  
  52. # Generate the fractal
  53. set.seed(42)
  54. fern <- generate_barnsley_fern(50000)
  55.  
  56. # Plot the fern
  57. p <- ggplot(fern, aes(x, y, color = factor(group))) +
  58. geom_point(size = 0.1, alpha = 0.6) +
  59. scale_color_viridis_d(option = "D") +
  60. theme_void() +
  61. theme(
  62. legend.position = "none",
  63. panel.background = element_rect(fill = "#0E0E18"),
  64. plot.background = element_rect(fill = "#0E0E18")
  65. ) +
  66. labs(title = "Fractal Dreams") +
  67. coord_fixed(ratio = 1)
  68.  
  69. print(p)
  70.  
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement