forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFS.jl
29 lines (23 loc) · 856 Bytes
/
IFS.jl
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
using DelimitedFiles
# This is a function to simulate a "chaos game"
function chaos_game(n::Int, shape_points)
# Initializing the output array and the initial point
output_points = zeros(n,2)
point = [rand(), rand()]
for i = 1:n
output_points[i,:] .= point
point = 0.5*(rand(shape_points) .+ point)
end
return output_points
end
# This will generate a Sierpinski triangle with a chaos game of n points for an
# initial triangle with three points on the vertices of an equilateral triangle:
# A = (0.0, 0.0)
# B = (0.5, sqrt(0.75))
# C = (1.0, 0.0)
# It will output the file sierpinski.dat, which can be plotted after
shape_points = [[0.0, 0.0],
[0.5, sqrt(0.75)],
[1.0, 0.0]]
output_points = chaos_game(10000, shape_points)
writedlm("sierpinski.dat", output_points)