Advertisement
Guest User

Untitled

a guest
Jul 1st, 2018
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.76 KB | None | 0 0
  1. # Load packages
  2. library(Rcpp)
  3. library(viridis)
  4.  
  5. # output parameters
  6. output_width = 1920 * 4
  7. output_height = 1080 * 4
  8. N_points = 50e6
  9. point_alpha = 0.05 #point transperancy
  10.  
  11. # Attractor parameters
  12. params <- c(1.886,-2.357,-0.328, 0.918)
  13.  
  14. # C++ function to rapidly generate points
  15. cliff_rcpp <- cppFunction(
  16.     "
  17.    NumericMatrix cliff(int nIter, double A, double B, double C, double D) {
  18.    NumericMatrix x(nIter, 2);
  19.    for (int i=1; i < nIter; ++i) {
  20.    x(i,0) = sin(A*x(i-1,1)) + C*cos(A*x(i-1,0));
  21.    x(i,1) = sin(B*x(i-1,0)) + D*cos(B*x(i-1,1));
  22.    }
  23.    return x;
  24.    }"
  25. )
  26.  
  27. # Function for mapping a point to a colour
  28. map2color <- function(x, pal, limits = NULL) {
  29.     if (is.null(limits))
  30.         limits = range(x)
  31.     pal[findInterval(x,
  32.                      seq(limits[1], limits[2], length.out = length(pal) + 1),
  33.                      all.inside = TRUE)]
  34. }
  35.  
  36. # Obtain matrix of points
  37. cliff_points <- cliff_rcpp(N_points, params[1], params[2], params[3], params[4])
  38.  
  39. # Calculate angle between successive points
  40. cliff_angle <- atan2(
  41.     (cliff_points[, 1] - c(cliff_points[-1, 1], 0)),
  42.     (cliff_points[, 2] - c(cliff_points[-1, 2], 0))
  43. )
  44.  
  45. # Obtain colours for points
  46. available_cols <-
  47.     viridis(
  48.         1024,
  49.         alpha = point_alpha,
  50.         begin = 0,
  51.         end = 1,
  52.         direction = 1
  53.     )
  54.  
  55. cliff_cols <- map2color(
  56.     cliff_angle,
  57.     c(available_cols, rev(available_cols))
  58. )
  59.  
  60.  
  61. # Output image directly to disk
  62. jpeg(
  63.     "clifford_attractor.jpg",
  64.     width = output_width,
  65.     height = output_height,
  66.     pointsize = 1,
  67.     bg = "black",
  68.     quality = 100
  69.    
  70. )
  71.     plot(
  72.         cliff_points[-1, ],
  73.         bg = "black",
  74.         pch = ".",
  75.         col = cliff_cols
  76.     )
  77.  
  78. dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement