-
-
Save mcne65/88cefbf040c3bcc6af373003c9eafe8b to your computer and use it in GitHub Desktop.
A fractal Christmas tree plot in R
This file contains 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
# http://www.r-bloggers.com/merry-christmas-5/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+RBloggers+%28R+bloggers%29 | |
# Each row is a 2x2 linear transformation | |
# Christmas tree | |
L <- matrix( | |
c(0.03, 0, 0 , 0.1, | |
0.85, 0.00, 0.00, 0.85, | |
0.8, 0.00, 0.00, 0.8, | |
0.2, -0.08, 0.15, 0.22, | |
-0.2, 0.08, 0.15, 0.22, | |
0.25, -0.1, 0.12, 0.25, | |
-0.2, 0.1, 0.12, 0.2), | |
nrow=4) | |
# ... and each row is a translation vector | |
B <- matrix( | |
c(0, 0, | |
0, 1.5, | |
0, 1.5, | |
0, 0.85, | |
0, 0.85, | |
0, 0.3, | |
0, 0.4), | |
nrow=2) | |
prob = c(0.02, 0.6,.08, 0.07, 0.07, 0.07, 0.07) | |
# Iterate the discrete stochastic map | |
N = 1e5 #5 # number of iterations | |
x = matrix(NA,nrow=2,ncol=N) | |
x[,1] = c(0,2) # initial point | |
k <- sample(1:7,N,prob,replace=TRUE) # values 1-7 | |
for (i in 2:N) | |
x[,i] = crossprod(matrix(L[,k[i]],nrow=2),x[,i-1]) + B[,k[i]] # iterate | |
# Plot the iteration history | |
par(bg='darkblue',mar=rep(0,4)) | |
plot(x=x[1,],y=x[2,], | |
col=grep('green',colors(),value=TRUE), | |
axes=FALSE, | |
cex=.1, | |
xlab='', | |
ylab='' )#,pch='.') | |
bals <- sample(N,20) | |
points(x=x[1,bals],y=x[2,bals]-.1, | |
col=c('red','blue','yellow','orange'), | |
cex=2, | |
pch=19 | |
) | |
text(x=-.7,y=8, | |
labels='Merry', | |
adj=c(.5,.5), | |
srt=45, | |
vfont=c('script','plain'), | |
cex=3, | |
col='gold' | |
) | |
text(x=0.7,y=8, | |
labels='Christmas', | |
adj=c(.5,.5), | |
srt=-45, | |
vfont=c('script','plain'), | |
cex=3, | |
col='gold' | |
) | |
text(x=-0.6,y=0, | |
cex=0.8, | |
labels="To Chengdu R Users Group members", | |
col="white" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment