How do I get an animated gif from the following construction?
Possibly also changing the functions used, if a different function is more suitable for the case.
using Plots
gr()
tr=[(0,0),(1,0),0.5.*(1,sqrt(3))]
p=plot(tr, st=:scatter, markersize=3, legend=false)
m=0.5 .* ((0,0).+ (1,0))
sierpinski=[m]
for i in 1:10000
m=0.5 .*(m .+ tr[rand(1:3)])
push!(sierpinski,m)
end
plot!(p,sierpinski,st=:scatter, markersize=1, legend=false)
savefig(p,"sierpinski.png")
![sierpinski|600x400](upload://zD6zATpX1cxDoNJtUgU0e2LRxr3.png)
I tried this way, but it doesn’t work.
julia> using Plots
julia> gr()
Plots.GRBackend()
julia> tr=[(0,0),(1,0),0.5.*(1,sqrt(3))]
3-element Vector{Tuple{Real, Real}}:
(0, 0)
(1, 0)
(0.5, 0.8660254037844386)
julia> p = scatter(1,xlim = (0, 1),ylim = (0, sqrt(3)/2), legend = false, marker = 1)
julia> m=0.5 .* ((0,0).+ (1,0))
(0.5, 0.0)
julia> @gif for i=1:10000
m=0.5 .*(m .+ tr[rand(1:3)])
push!(p, m)
end every 10
ERROR: UndefVarError: m not defined
Stacktrace:
[1] macro expansion
@ c:\Users\sprmn\.julia\v1.8\plot3.jl:86 [inlined]
[2] top-level scope
@ C:\Users\sprmn\.julia\packages\Plots\yJrrq\src\animation.jl:235
I tried to use the same scheme used for the lorenz attractor and in this way I get the animated gif. But I don’t understand why it doesn’t work in the first case and I’m pretty sure there are easier ways to achieve the same result.
#-------------
using Plots
# define the Lorenz attractor
Base.@kwdef mutable struct Sier
a = (0,0)
b=(1,0)
c=0.5.*(1,sqrt(3))
tr=[a,b,c]
x=0.5 * (a[1]+ b[1])
y=0.5 * (a[2]+ b[2])
end
function step!(m::Sier)
v=m.tr[rand(1:3)]
m.x = 0.5*(m.x+v[1])
m.y = 0.5*(m.y+v[2])
end
midpoint = Sier()
plt = scatter(
1,
xlim = (0, 1),
ylim = (0, sqrt(3)/2),
legend = false,
marker = 1,
)
@gif for i=1:10000
step!(midpoint)
push!(plt, midpoint.x, midpoint.y)
end every 10