anilewe
anilewe

Reputation: 389

Barnsley fern in R

I tried to make this fractal in R, it looks almost right, but the stem is missing. What I am doing wrong? Here is code:

max_i<-100000

x <- rep(0, times=max_i)
y <- x

for (i in 2:(max_i)) {
rand=runif(1, 1, 100)

if (rand<1) {
    x[i]<-0
    y[i]<-0.16*y[i-1]
}

else if (rand<7){
    x[i]<- -0.15*x[i-1]+0.28*y[i-1]
    y[i]<-0.26*x[i-1]+0.24*y[i-1]+0.44
}

else if (rand<14){
    x[i]<-0.2*x[i-1]-0.26*y[i-1]
    y[i]<-0.23*x[i-1]+0.22*y[i-1]+1.6
}

else {
    x[i]<-0.85*x[i-1]+0.04*y[i-1]
    y[i]<- -0.04*x[i-1]+0.85*y[i-1]+1.6
    }
}
plot(x,y, pch='.')

Upvotes: 3

Views: 486

Answers (1)

Dan
Dan

Reputation: 12074

rand=runif(1, 1, 100) should read rand=runif(1, 0, 100). In the former case, rand cannot be less than one and, consequently, the first if statement (i.e., the stem part) is never used.

enter image description here

Upvotes: 3

Related Questions