%%********************************************************************************************
%  Author: Tim Chartier
%  Title:  mountain2d.m
%  Description: This code creates 2D fractal mountains.
%*********************************************************************************************
%
% Exercises:
%
% 1. Run the code to create your own fractal mountain. 
% 2. Perform more iterations by altering the parameter "iterates". 
% 3. Zoom in on the mountain. How many zooms can you perform before it no longer 
%    appears like a mountain? Can you alter the code to allow you to zoom in 
%    more times? 
% 4. Increase and decrease the value 2 in the line: 
%               scale = (1/2)^(level);
%    How does this effect the mountain? Alter the total number of iterates 
%    with a new value and view the effect. 
% 5. Find the line: 
%               yValues = [0 0];
%    and replace it with: 
%               yValues = [0 0.3 0];
%    Explain the resulting behavior and then change 0.3 to a value of your choice. 
%
%*********************************************************************************************


%% Input parameters
iterates = 6;     % Set the number of iterates
yValues = [0 0];  % Define the 0th (starting) iterate

%% Perform iterates to create 2D mountain
for level=1:iterates
    scale = (1/2)^(level);     % adjust the value 2 for effect 
    yValuesTmp = [];           % initialize temporary array
    for i=1:length(yValues)-1  
        yValuesTmp = [yValuesTmp yValues(i)];
        a = 0; b = 1;
        yRand = (a + (b-a)*rand);
        yValueNew = mean([yValues(i),yValues(i+1)])+ yRand*scale;
        yValuesTmp = [yValuesTmp yValueNew];    
    end
    yValuesTmp = [yValuesTmp yValues(end)];
    yValues = yValuesTmp;
    % Plot resulting 2D landscape 
    rgb = [147 112 219];
    axes('Color',[rgb(1)/255 rgb(2)/255 rgb(3)/255],'Xtick',[],'Ytick',[])  
    hold on
    h = area(yValues);
    set(h,'FaceColor',[0 0 0]);
    axis tight
    hold off
    pause(0.5)
end 


