3.6 Examples in Julia
3.6.1 The Sierpinski triangle once again
The following code reproduces the Sierpiński triangle construction presented in this chapter. In the triangle
matrix, 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)
using Plots
# Number of steps, triangle vertices and colors for transformation.
= 100000
N = [0 0; 0.5 2; 1.5 0.5]
triangle = ["red" "green" "blue"]
col = [0.1 0]
point
# Matrix for consecutive points of the Sierpiński triangle.
= zeros(N, 2)
points = String[]
cols
# The cols vector collects the colors corresponding to the chosen transformations
# and the coords array collects the pen coordinates after each jump.
for i in 1:N
= rand(1:3,1)
ind append!(cols, col[ind])
= (point + triangle[ind, :])/2
point :] = point
coords[i,end
scatter(coords[:,1], coords[:,2], color=cols,
=:false, markersize=2, axis=nothing) legend
3.6.2 Barnsley fern
using StatsBase
# The following transformations are written as separate functions
# that will later make up the vector of trans.
trans1(x) = [ 0. 0.; 0. 0.16]*x
trans2(x) = [0.85 0.04; -0.04 0.85]*x + [0; 1.6]
trans3(x) = [0.2 -0.26; 0.23 0.22]*x + [0; 0.8]
trans4(x) = [-0.15 0.28; 0.26 0.24]*x + [0; 0.44]
= [trans1, trans2, trans3, trans4]
trans = [0 0]'
point
= ["red" "green" "blue" "orange"]
col = [0.01, 0.79, 0.1, 0.1]
probs
= 200000
N = zeros(N, 2)
coords = String[]
cols
for i in 1:N
# We randomly choose transformations using a vector of *probs* with the frequencies of each transformation.
# The sample function is available in the StatsBase library.
= sample(1:length(probs), Weights(probs))
ind = trans[ind[1]]
selected_trans = selected_trans(point)
point :] = point
coords[i,push!(cols, col[ind])
end
scatter(coords[:,1], coords[:,2], color=cols,
=:false, markersize=1, axis=nothing,
legend=cols) markerstrokecolor
3.6.3 Maple leaf
# We write the transformations as 3x3 matrices representing linear
# transformations. This makes the notation more concise.
= [[0.14 0.01 -0.08; 0.0 0.51 -1.31; 0 0 1],
affines 0.43 0.52 1.49; -0.45 0.5 -0.75 ; 0 0 1],
[0.45 -0.49 -1.62; 0.47 0.47 -0.74; 0 0 1],
[0.49 0.0 0.02; 0.0 0.51 1.62 ; 0 0 1]]
[= [0.25, 0.25, 0.25, 0.25]
probs = ["red", "green", "blue", "orange"]
col
= 200000
N = [0 0 1]'
point = zeros(N, 2)
coords = String[]
cols
for i in 1:N
# Unlike in the previous chapters, here we do not use recursion.
# We iteratively count the positions for N jumps and then
# draw all the determined points with a single scatter instruction.
= sample(1:length(probs), Weights(probs))
ind = affines[ind[1]] * point
point :] = point[1:2]
coords[i,push!(cols, col[ind])
end
scatter(coords[:,1], coords[:,2], color=cols,
=:false, markersize=1, axis=nothing,
legend=cols) markerstrokecolor