%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         7.gyakorlat         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Nemlin. din. rsz-ek alapjai %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          2015.11.04.        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          Fraktálok          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Sierpinsky háromszög
N=1000;
%a háromszög csúcsai, az algoritmus fix pontjai
P(1,1)=1;
P(1,2)=1;
P(2,1)=1000;
P(2,2)=1;
P(3,1)=500;
P(3,2)=860;

figure;
title('Sierpinski háromszög');
xlim([0 N])
ylim([0 N])
iternum=1000000; %iterációszám
C=zeros(iternum,2); %ide kerülnek majd a háromszög pontjai

c=P(randi([1,3]),:)
hold on;
% c - aktuális pont - referencia (elõször az egyik fix pont)
for i=2:iternum
    r=randi([1,3]);
    c = [(c(1) + P(r,1)) / 2, (c(2) + P(r,2)) / 2]; %az új pont a fix és a refenciapont egyenesének felezõpontja
    C(i,:)=c; %eltárolom az új pontot 
    
    %lépésenként kirajzolva:
    %{
   plot(c(:,1), c(:,2), '.', 'MarkerSize', 1);
   title('Sierpinski háromszög');
   xlabel(['Iteration: ', num2str(i)]);
   drawnow;
    %}
end



 plot(C(:,1), C(:,2), '.', 'MarkerSize', 1);
 title('Sierpinski háromszög');
 xlabel(['Iteration: ', num2str(iternum)]);




