Barnsley fern: Difference between revisions
Content added Content deleted
Line 1,711: | Line 1,711: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
⚫ | |||
{{works with|Julia|0.6}} |
|||
mutable struct BarnsleyFern |
|||
⚫ | |||
width::Int |
|||
height::Int |
|||
⚫ | |||
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)) |
|||
y::Float64 |
|||
age::Int |
|||
fern::Matrix{RGB} |
|||
⚫ | |||
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]) |
|||
⚫ | |||
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) |
||
f.x, f.y = r < 1 ? (0.0, 0.16 * f.y) : |
|||
1 <= r < 86 ? (0.85 * f.x + 0.04 * f.y, -0.04 * f.x + 0.85 * f.y + 1.6) : |
|||
86 <= r < 93 ? (0.2 * f.x - 0.26 * f.y, 0.23 * f.x + 0.22 * f.y + 1.6) : |
|||
(-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) |
|||
⚫ | |||
transform(f) |
|||
newx, newy = scale(f) |
|||
f.fern[Int(trunc(newx)) + 1, Int(trunc(newy)) + 1] = f.color |
|||
end |
end |
||
f.age += iterations |
|||
end |
|||
⚫ | |||
fern = BarnsleyFern(500, 500) |
|||
iterate(fern, 1000000) |
|||
fern.fern' |
|||
⚫ | |||
[[File:Jbarnsleyfern.png]] |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |