Barnsley fern: Difference between revisions

m
Line 1,711:
 
=={{header|Julia}}==
<syntaxhighlight lang=julia>functionusing barnsleyfern(n::Integer)Images
{{works with|Julia|0.6}}
 
mutable struct BarnsleyFern
<syntaxhighlight lang=julia>function barnsleyfern(n::Integer)
funs = (width::Int
height::Int
(x, y) -> (0, 0.16y),
color::RGB
(x, y) -> (0.85x + 0.04y, -0.04x + 0.85y + 1.6),
bgcolor::RGB
(x, y) -> (0.2x - 0.26y, 0.23x + 0.22y + 1.6),
x::Float64
(x, y) -> (-0.15x + 0,28y, 0.26x + 0.24y + 0.44))
rst = Matrix{y::Float64}(n, 2)
rst[1, age:] = 0.0:Int
fern::Matrix{RGB}
for row in 2:n
function BarnsleyFern(width, height)
f = new(width, height, RGB(0.0, 1.0, 0.0), RGB(1.0, 1.0, 1.0), 0.0, 0.0, 0,
[RGB(0.0, 0.0, 0.0) for x in 1:width, y in 1:height])
(x, y) ->= scale(0, 0.16yf),
f.fern[Int(trunc(x)) + 1, Int(trunc(y)) + 1] = f.color
return f
end
end
 
function transform(f::BarnsleyFern)
r = rand(0:99)
iff.x, f.y = r < 1; ? (0.0, 0.16 * f.y) = 1;:
elseif 1 <= r < 86; ? (0.85 * f.x =+ 0.04 * f.y, -0.04 * f.x + 0.85 * f.y + 1.6) 2;:
elseif 86 <= r < 93; ? (0.2 * f.x =- 0.26 * f.y, 0.23 * f.x + 0.22 * f.y + 1.6) 3;:
else (-0.15 * f.x + 0.28 * f.y, 0.26 * f.x =+ 0.24 * f.y 4;+ end0.44)
end
rst[row, 1], rst[row, 2] = funs[f](rst[row-1, 1], rst[row-1, 2])
 
scale(f::BarnsleyFern) = (f.x + 2.182) * (f.width - 1) / 4.8378, (9.9983 - f.y) * (f.height - 1) / 9.9983
 
function iterate(f::BarnsleyFern, iterations)
for row_ in 21:niterations
transform(f)
newx, newy = scale(f)
f.fern[Int(trunc(newx)) + 1, Int(trunc(newy)) + 1] = f.color
end
returnf.age rst+= iterations
end
end</syntaxhighlight>
 
fern = BarnsleyFern(500, 500)
iterate(fern, 1000000)
fern.fern'
end</syntaxhighlight>
[[File:Jbarnsleyfern.png]]
 
=={{header|Kotlin}}==
4,102

edits