function [ x, y, r ] = feto_fractal( N, prob )
% [ x, y, r ] = feto_fractal( N, prob );

x = zeros( N, 1 );
y = zeros( N, 1 );
r = zeros( N, 1 );

if nargin<2
    prob = [.01 .07 .07 .85];
end
x(1) = 0;
y(1) = 0;
for n = 1 : N-1
    r(n+1) = gera_aleatorio( prob, 1 );
    switch( r(n+1) )
        case{ 1 }
            x(n+1) = 0;
            y(n+1) = 0.16*y(n);
        case{ 2 }
            x(n+1) = 0.20*x(n) - 0.26*y(n);
            y(n+1) = 0.23*x(n) + 0.22*y(n) + 1.6;
        case{ 3 }
            x(n+1) = -0.15*x(n) + 0.28*y(n);
            y(n+1) = 0.26*x(n) + 0.24*y(n) + 0.44;
        case{ 4 }
            x(n+1) = 0.85*x(n) + 0.04*y(n);
            % y(n+1) = -0.004*x(n) + 0.85*y(n) + 1.6; % wrong (see en.wikipedia.org/wiki/Barnsley_fern)
            y(n+1) = -0.04*x(n) + 0.85*y(n) + 1.6; 
    end
end

if nargout==0
    plot( x, y, '.', 'markersize', 1 )
    clear x
end