3.5 Examples in R
3.5.1 The Sierpinski triangle once again
The following code reproduces the Sierpiński triangle construction presented in this chapter. In the triangle
vector, we define the three vertices of the triangle, and in the point
object we put the coordinates of the starting point for the fractal construction. We then move the point toward a random vertex in a loop.
- \(x' = x/2\), \(y' = y/2\) (left corner)
- \(x' = x/2 + \frac 12\), \(y' = y/2\) (right corner)
- \(x' = x/2 + \frac 14\), \(y' = y/2 + \frac{\sqrt{3}}2\) (upper corner)
= 200000
N = y = 0
x
plot(0, xlim = c(0,1), ylim = c(0,0.9),
xlab = "", ylab = "", col="white", asp=1)
for (i in 1:N) {
# We use the three transformations as different scenarios in the switch statement block.
= sample(1:3, 1)
ind switch(ind,
'1' = {x <- x/2; y <- y/2},
'2' = {x <- x/2 + 1/2; y <- y/2},
'3' = {x <- x/2 + 1/4; y <- y/2 + sqrt(3)/4})
points(x, y, pch = ".", col=ind)
}
3.5.2 Barnsley fern
= function(x,y)
trans1 c(0., 0.16*y)
= function(x,y)
trans2 c(0.85*x + 0.04*y, -0.04*x + 0.85*y + 1.6)
= function(x,y)
trans3 c(0.2*x - 0.26*y, 0.23*x + 0.22*y + 0.8)
= function(x,y)
trans4 c(-0.15*x + 0.28*y, 0.26*x + 0.24*y + 0.44)
# In this example, we stored the transformations (which are a function)
# in a list to make it easier to work on them.
= list(trans1, trans2, trans3, trans4)
trans = c(0.01, 0.79, 0.1, 0.1)
probs
= 200000
N = y = 0
x
plot(0, xlim = c(-2.5,2.5), ylim = c(0,10),
xlab = "", ylab = "", col="white", asp=1)
for (i in 1:N) {
= sample(seq_along(trans), 1, prob = probs)
ind = trans[[ind]](x, y)
res = res[1]
x = res[2]
y points(x, y, pch = ".", col=ind)
}
3.5.3 Maple leaf
= function(x,y, affine)
transform c(affine[1]*x + affine[2]*y + affine[3],
4]*x + affine[5]*y + affine[6])
affine[
= 400000
N = y = 0
x
# List of transformations, we store it as a list of vectors of length 6.
= list(c(0.14, 0.01, -0.08, 0.0, 0.51, -1.31),
affines c(0.43, 0.52, 1.49, -0.45, 0.5, -0.75),
c(0.45, -0.49, -1.62, 0.47, 0.47, -0.74),
c(0.49, 0.0, 0.02, 0.0, 0.51, 1.62))
= c(0.25, 0.25, 0.25, 0.25)
probs
plot(0, xlim = c(-3.5,3.5), ylim = c(-3.5,3.5),
xlab = "", ylab = "", col="white", asp=1)
for (i in 1:N) {
# We randomly choose the transformations with the probabilities
# indicated in the probs vector.
= sample(seq_along(affines), 1, prob = probs)
ind = transform(x, y, affines[[ind]])
res = res[1]
x = res[2]
y points(x, y, pch = ".", col=ind)
}