Barnsley fern: Difference between revisions

Content added Content deleted
Line 1,711: Line 1,711:


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang=julia>using 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{Float64}(n, 2)
y::Float64
rst[1, :] = 0.0
age::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(f)
f.fern[Int(trunc(x)) + 1, Int(trunc(y)) + 1] = f.color
return f
end
end

function transform(f::BarnsleyFern)
r = rand(0:99)
r = rand(0:99)
if r < 1; f = 1;
f.x, f.y = r < 1 ? (0.0, 0.16 * f.y) :
elseif r < 86; f = 2;
1 <= r < 86 ? (0.85 * f.x + 0.04 * f.y, -0.04 * f.x + 0.85 * f.y + 1.6) :
elseif r < 93; f = 3;
86 <= r < 93 ? (0.2 * f.x - 0.26 * f.y, 0.23 * f.x + 0.22 * f.y + 1.6) :
else f = 4; end
(-0.15 * f.x + 0.28 * f.y, 0.26 * f.x + 0.24 * f.y + 0.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 _ in 1:iterations
transform(f)
newx, newy = scale(f)
f.fern[Int(trunc(newx)) + 1, Int(trunc(newy)) + 1] = f.color
end
end
return rst
f.age += iterations
end
end</syntaxhighlight>

fern = BarnsleyFern(500, 500)
iterate(fern, 1000000)
fern.fern'
</syntaxhighlight>
[[File:Jbarnsleyfern.png]]


=={{header|Kotlin}}==
=={{header|Kotlin}}==