-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattractors.R
78 lines (72 loc) · 2.41 KB
/
attractors.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#Program: Generate Attractors using Rcpp
#Author: Anurag Raj
#Date: Jan 2022
#Adapted from https://www.williamrchase.com/post/strange-attractors-12-months-of-art-february
######################################################################
# Load in libraries
######################################################################
library(Rcpp)
library(tidyverse)
library(ggplot2)
######################################################################
# Parameters
######################################################################
# Framework environment
opt <- theme(legend.position = "none",
panel.background = element_rect(fill="white", color="black"),
plot.background = element_rect(fill="white"),
axis.ticks = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank())
#Function to loop
cppFunction('DataFrame createTrajectory(int n, double x0, double y0,
double a1, double a2, double a3, double a4, double a5,
double a6, double a7, double a8, double a9, double a10,
double a11, double a12, double a13, double a14) {
// create the columns
NumericVector x(n);
NumericVector y(n);
x[0]=x0;
y[0]=y0;
for(int i = 1; i < n; ++i) {
x[i] = a1+a2*x[i-1]+ a3*y[i-1]+ a4*pow(fabs(x[i-1]), a5)+ a6*pow(fabs(y[i-1]), a7);
y[i] = a8+a9*x[i-1]+ a10*y[i-1]+ a11*pow(fabs(x[i-1]), a12)+ a13*pow(fabs(y[i-1]), a14);
}
// return a new data frame
return DataFrame::create(_["x"]= x, _["y"]= y);
}
')
#particles points
a1 <- -0.8
a2 <- 0.4
a3 <- -1.1
a4 <- 0.5
a5 <- -0.6
a6 <- -0.1
a7 <- -0.5
a8 <- 0.8
a9 <- 1.0
a10 <- -0.3
a11 <- -0.6
a12 <- -0.3
a13 <- -1.2
a14 <- -0.3
#Trajectories
df <- createTrajectory(10000000, 1, 1, a1, a2, a3, a4, a5, a6,
a7, a8, a9, a10, a11, a12, a13, a14)
mx <- quantile(df$x, probs = 0.05)
Mx <- quantile(df$x, probs = 0.95)
my <- quantile(df$y, probs = 0.05)
My <- quantile(df$y, probs = 0.95)
#dataframe with points
df %>% filter(x > mx, x < Mx, y > my, y < My) -> df
#plots showing pattern
plot <- ggplot(df) +
geom_point(aes(x, y), shape=46, alpha=0.01, size=0, color="darkgreen") +
scale_x_continuous(expand = c(0,0))+
scale_y_continuous(expand = c(0,0))+
coord_fixed() +
opt
#save plot
ggsave("attractors.png", plot, height = 6, width = 6, units = 'in', dpi = 600)