%%
%% 7.2 Fractals (Computational example)
%

clear all

%
%% 1 Turtle graphics
% Instead of coordinates of lines points, movements of the pen are specified. There are commands for forward movement, rotation, color and storage of states:
% \bullet\ Line: F,G
% \bullet\ Intermediate space: f,g
% \bullet\ Line color indices: 1,2,3,4,...
% \bullet\ Rotation angle between lines: \alpha
% \bullet\ Turn by angle \alpha: +,\ -
% \bullet\ 90°-rotation: >,\,<
% \bullet\ Save and recall an intermediate state: [ \ ]
%
% Function Turtle (\alpha, commands, colormap, generation index)
% \bullet\ \alpha: Rotation angle (see above)
% \bullet\ Commands: string with sequence of lines, colors, ...
% \bullet\ Colormap: Assignment of line color indices to rgb colors
% \bullet\ Generation index: for fractals (see below) (-1: no indication of the generation in the plot)
%
% Example: Turtle hut

Col=[0.4 .4 .5;1 0 0;1 .5 0;1 1 0];
S='<1F<+2F+F+1F>+3F+F>+1F<F-3F-F>-f';
Turtle(2*pi/3,S,Col,-1)

%

Turtle(2*pi/3,[S S S S S],Col,-1)

%
% Example: Turtle rosette

figure
Col=[1 0 0];
S=[];
for n=1:40
    S=[S 'F+'];
end
Turtle(81*pi/180,S,Col,-1)

%
%% 2 Lindenmayer systems
% A fractal is a structure that recursively consists of itself. The structure can be symbolized by letters.
% The graphic representation can be done through Turtel graphics.
%
% Example: Cantor set (see below)
% \bullet a line consists of two lines with an intermediate space f: F\to FfF
% \bullet an intermediate space consists of three intermediate spaces f\to fff
% Recursion: F\to FfF\to FfF\,fff\,FfF \to FfFffffFf\,  ffffffff ...
%
% Function LSys(\alpha,start string,Iteration rules,colormap,generation index)
% Iteration rules: cell array with iteration rules, for instance \{'F:FfF'\ 'f:fff'\ 'G:GG'\}
% every iteration is then replaced as follows:  F by FfF, f by fff and G by GG
%
%% 3 Hausdorff dimension
% A structure consists of substructures with a specific scaling. It is characterized by the length scaling s and the number of substructures N.
% The Hausdorff dimension D is then defined by s^D=N, or D=\frac{\log N}{\log s}.
% For lines, surfaces and spatial structures follow the correct values:
% \bullet\ 3 lines become one line (1D): s=3,\ N=3 \to D=1
% \bullet\ 9 rectangles become one rectangle (2D): s=3,\ N=9 \to D=2
% \bullet\ 27 cubes become one cube (3D): s=3,\ N=27 \to D=3
%
% For fractals, the dimension is fractal.
%
%% 4 Cantor set
% \bullet\ a line F consists of two lines with an intermediate space f: F\to FfF
% \bullet\ an intermediate space consists of three intermediate spaces: f\to fff
% therefore: F\to FfF\to FfF\,fff\,FfF \to ...
% This describes a set with a smaller dimension than a line, but a larger dimension than a point.
% Hausdorff dimension:

s=3; %A line is three times the size of its parts (scaling)
N=2; %A line consists of two lines
Dimension=log(N)/log(s)
Col=[1 0 0;0 1 0];
for n=1:4
    subplot(2,2,n)
    LSys(pi/3,'F',{'F:1Ff2F','f:fff'},Col,n-1)
end

%
%% 5 Koch curve
% A line consists of four lines in a cone shape
% F\to F-F++F-F,\ {\rm or\ graphical:\ \  } \_\to \_ / \backslash \_
% With D\in[1,2] the linear structure is already somewhat planar

s=3; % a line has three times the size of its parts (scaling)
N=4; % a line consists of four lines
Dimension=log(N)/log(s)
Col=[1 0 0;0 0 1;0 1 0;1 1 0];
for n=1:4
    subplot(2,2,n)
    LSys(pi/3,'1F++2F++3F',{'F:1F-2F++3F-4F'},Col,n-1)
end

%
%% 6 Farn

subplot(1,1,1)
Col=[.6*[1 .5 0];0 .5 0;0 1 0;0 1 0];
LSys(pi/9,'<F',{'F:1G[-2F]1G[-2F]+3F' 'G:GG' },Col,5)

% __________________________________________________________
%
%% Lindenmayer systems

function LSys(phi,S,s,Col,N)
for n=1:N
    T=S;
    S=[];
    K=size(s,2);
    for m=1:size(T,2)
        TM=T(m);
        if isletter(TM)
            for k=1:K
                s0=s{k};
                if strcmp(s0(1),TM)
                    S=[S s0(3:end)];
                end
            end
        else S=[S TM];
        end
    end
end
Turtle(phi,S,Col,N)
end

% __________________________________________________________
%% Turtle graphics

function Turtle(phi,S,Col,N)
X=[1;0];
Y=[0 0;0 0];
L=[cos(phi) sin(phi);-sin(phi) cos(phi)];
R=[cos(phi) -sin(phi);sin(phi) cos(phi)];
col=Col(1,:);
cnt=0;
hold on
for n=1:size(S,2)
    switch S(n)
        case {'F' 'G'}
            Y=[Y(:,2) Y(:,2)+X];
            plot(Y(1,:),Y(2,:),'Color',col)
        case {'f' 'g'}
            Y=[Y(:,2) Y(:,2)+X];
        case {'1' '2' '3' '4' '5' '6' '7' '8'}
            col=Col(str2double(S(n)),:);
        case '-'
            X=R*X;
        case '+'
            X=L*X;
        case '<'
            X=[0 -1;1 0]*X;
        case '>'
            X=[0 1;-1 0]*X;
        case '['
            cnt=cnt+1;
            Mem{cnt}={Y, X, col};
        case ']'
            [Y,X,col]=Mem{cnt}{:};
            cnt=cnt-1;
    end
end
set(gcf','DefaultTextInterpreter','LaTex');
if N+1
    txt=['${\rm Generation}\ N=' num2str(N) '$'];
    title(txt)
end
axis equal
axis off
hold off
end