%BARNSLEY          Barnsley's game to compute Sierpinski gasket.

rand('state',1)                   % Set random number state.
V = [0, 1, 0.5; 0, 0, sqrt(3)/2]; % Columns give triangle vertices.

point = V(:,1);                   % Start at a vertex.
n = input('Enter number of points (try 1000) ');

plot(point(1),point(2),'.','MarkerSize',10)
axis([0 1 0 1])
axis off 
hold on

for k = 1:n
    node = ceil(3*rand);          % node is 1, 2, or 3 with equal prob.
    point = (V(:,node) + point)/2;
    pause(0.015)
    plot(point(1),point(2),'.','MarkerSize',10)
end

hold off

