
The Fibonacci word may be represented as a fractal as described here:


(Clicking on the above website   (hal.archives-ouvertes.fr)    will leave a cookie.)



For F_wordm start with F_wordCharn=1
Draw a segment forward
If current F_wordChar is 0
Turn left if n is even
Turn right if n is odd
next n and iterate until end of F_word


Task

Create and display a fractal similar to Fig 1.


(Clicking on the above website   (hal.archives-ouvertes.fr)    will leave a cookie.)

final_length = 37;
 
word_n = '';
word_n_1 = '';
word_n_2 = '';
 
for i = 1:final_length
    if i == 1 then
        word_n = '1';
    elseif i == 2
        word_n = '0';
    elseif i == 3
        word_n = '01';
        word_n_1 = '0';
    else
        word_n_2 = word_n_1;
        word_n_1 = word_n;
        word_n = word_n_1 + word_n_2;
    end
end
 
word = strsplit(word_n);
fractal_size = sum(word' == '0');
fractal = zeros(1+fractal_size,2);
 
direction_vectors = [1,0; 0,-1; -1,0; 0,1];
direction = direction_vectors(4,:);
direction_name = 'N';
 
for j = 1:length(word_n);
    fractal(j+1,:) = fractal(j,:) + direction;
    if word(j) == '0' then
        if pmodulo(j,2) then
            //right
            select direction_name
            case 'N' then
                direction = direction_vectors(1,:);
                direction_name = 'E';
            case 'E' then
                direction = direction_vectors(2,:);
                direction_name = 'S';
            case 'S' then
                direction = direction_vectors(3,:);
                direction_name = 'W';
            case 'W' then
                direction = direction_vectors(4,:);
                direction_name = 'N';
            end
        else
            //left
            select direction_name
            case 'N' then
                direction = direction_vectors(3,:);
                direction_name = 'W';
            case 'W' then
                direction = direction_vectors(2,:);
                direction_name = 'S';
            case 'S' then
                direction = direction_vectors(1,:);
                direction_name = 'E';
            case 'E' then
                direction = direction_vectors(4,:);
                direction_name = 'N';
            end
        end
    end
end
 
scf(0); clf();
plot2d(fractal(:,1),fractal(:,2));
set(gca(),'isoview','on');