Created
August 22, 2020 10:30
-
-
Save djnavarro/b522c7d43c41671cd174efc18c538a8a to your computer and use it in GitHub Desktop.
fractal flame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
using namespace Rcpp; | |
// turmite function to be called from R | |
// [[Rcpp::export]] | |
NumericMatrix flame(int iter, int layers) { | |
NumericMatrix points(iter, 3); // initially zero | |
NumericMatrix coeffs(9, layers); | |
// set coefficients | |
for(int i = 0; i < 9; i++) { | |
for(int j = 0; j < layers; j++) { | |
coeffs(i,j) = R::runif(-1, 1); | |
} | |
} | |
// initial values | |
points(0, 0) = R::runif(-1, 1); | |
points(0, 1) = R::runif(-1, 1); | |
points(0, 2) = R::runif(-1, 1); | |
// iterate | |
int r; | |
double x; | |
double y; | |
double z; | |
double s; | |
for(int t = 1; t < iter; t++) { | |
r = rand() % layers; // which affine transform to use? | |
// co-ordinates after random transform | |
x = coeffs(0, r) * points(t-1, 0) + coeffs(1, r) * points(t-1, 1) + coeffs(2, r); | |
y = coeffs(3, r) * points(t-1, 0) + coeffs(4, r) * points(t-1, 1) + coeffs(5, r); | |
z = coeffs(6, r) * points(t-1, 0) + coeffs(7, r) * points(t-1, 1) + coeffs(8, r); | |
// apply function to the transformed coords | |
s = x*x + y*y + z*z; | |
x = x/s; | |
y = y/s; | |
z = z/s; | |
// store results | |
points(t, 0) = x; | |
points(t, 1) = y; | |
points(t, 2) = (z + points(t-1, 2))/2; | |
} | |
return points; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(Rcpp) | |
library(ggplot2) | |
sourceCpp(here::here("source", "ff_b.cpp")) | |
# parameters | |
seed <- 11 | |
iter <- 20000000 | |
layers <- 4 | |
bg <- "ghostwhite" | |
pl <- "scico::oslo" | |
set.seed(seed) | |
df <- flame(iter, layers) | |
df <- as.data.frame(df) | |
names(df) <- c("x","y","c") | |
df <- df[-(1:100),] | |
p <- ggplot(df, aes(x,y, colour = c)) + | |
geom_point(size = 1.5, alpha = .2, stroke = 0, show.legend = FALSE) + | |
theme_void() + | |
theme(plot.background = element_rect(bg, bg)) + | |
paletteer::scale_color_paletteer_c(pl) | |
fname <- paste0("ff_03_", seed, ".png") | |
fpath <- here::here("image", fname) | |
ggsave(fpath, p, width = 16, height = 16, dpi = 100) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @coolbutuseless, @mdsumner and @martinmodrak! This is all super helpful!