Brownian tree

You are encouraged to solve this task according to the task description, using any language you may know.



Task

Generate and draw a   Brownian Tree.



A Brownian Tree is generated as a result of an initial seed, followed by the interaction of two processes.

 The initial "seed" is placed somewhere within the field. Where is not particularly important; it could be randomized, or it could be a fixed point.
 Particles are injected into the field, and are individually given a (typically random) motion pattern.
 When a particle collides with the seed or tree, its position is fixed, and it's considered to be part of the tree.

Because of the lax rules governing the random nature of the particle's placement and motion, no two resulting trees are really expected to be the same, or even necessarily have the same general shape. 



function SetSeed( f )
    for i = 1, #f[1] do         -- the whole boundary of the scene is used as the seed
        f[1][i]  = 1
        f[#f][i] = 1
    end
    for i = 1, #f do
        f[i][1]     = 1
        f[i][#f[1]] = 1
    end
end
 
function SetParticle( f )
    local pos_x, pos_y
    repeat
        pos_x = math.random( #f )
        pos_y = math.random( #f[1] )
    until f[pos_x][pos_y] == 0
 
    return pos_x, pos_y
end
 
 
function Iterate( f, num_particles )
    for i = 1, num_particles do
        local pos_x, pos_y = SetParticle( f )
 
        while true do
            local dx = math.random(5) - 3
            local dy = math.random(5) - 3
 
            if ( pos_x+dx >= 1 and pos_x+dx <= #f and pos_y+dy >= 1 and pos_y+dy <= #f[1] ) then
                if f[pos_x+dx][pos_y+dy] ~= 0 then
                    f[pos_x][pos_y] = 1
                    break
                else
                    pos_x = pos_x + dx
                    pos_y = pos_y + dy
                end            
            end
        end
    end
end
 
 
size_x, size_y = 400, 400       -- size of the scene
num_particles  = 16000
 
math.randomseed( os.time() )
 
f = {}
for i = 1, size_x do
    f[i] = {}
    for j = 1, size_y do
        f[i][j] = 0
    end
end
 
SetSeed( f )
Iterate( f, num_particles )
 
-- prepare the data for writing into a ppm-image file
for i = 1, size_x do
    for j = 1, size_y do
        if f[i][j] == 1 then f[i][j] = 255 end
    end
end
Write_PPM( "brownian_tree.ppm", ConvertToColorImage(f) )