Fibonacci spirals

Celebrate the beauty of mathematics with this golden spiral created using Scilab.

Change the values ​​to explore how multiple Fibonacci spirals unfold from a single point, forming fascinating golden patterns found in nature, from seashells to sunflowers!

Have fun!

Here is the script to run:

// ----- Fibonacci sequence -----
function fib = fibonacci_seq(n)
    fib = ones(1, n);
    for i = 3:n
        fib(i) = fib(i-1) + fib(i-2);
    end
endfunction

// ----- Draw one Fibonacci spiral -----
function [x, y, theta, r] = draw_spiral(n, k, theta, angle_, color_)

    x = zeros(n, 1); y = zeros(n, 1);
    r = 1;

    for i = 1:n
        x(i) = r * cos(angle_);
        y(i) = r * sin(angle_);
        angle_ = modulo(angle_ + theta, 2*%pi);
        r = sqrt(i + 1);
    end

    e = plot(ax, x, y, ".");
    c = color_ * 255;
    e.mark_foreground = color(c(1), c(2), c(3));
    e.tag = "plot" + string(k);
    
endfunction

// ----- Draw all spirals -----
function draw_all_spirals(ax, n, k, theta)

    delete(ax.children)
    drawlater();
    
    colors = [0.95 0.8 0.3;
    0.9 0.7 0.2;
    0.85 0.65 0.15;
    0.8 0.6 0.1;
    0.75 0.55 0.1;
    0.7 0.5 0.05;
    0.95 0.85 0.45;
    0.9 0.75 0.35;
    0.85 0.7 0.3;
    0.8 0.65 0.25;
    0.9 0.8 0.4;
    0.85 0.7 0.2];

    for i = 1:k
        angle_ = (i-1)*(2*%pi/k);
        draw_spiral(n, i, theta, angle_, colors(i,:));
    end

    ax.axes_visible = "off"; 
    ax.isoview = "on";
    ax.tight_limits = "on";
    ax.box = "off";

    mi = floor(min(ax.children.children.data))-1;
    ma = abs(mi);
    ax.data_bounds = [mi mi; ma ma];
    ax.margins = zeros(1, 4);
    drawnow()
endfunction

function animate_spirals()
    try
        while %t
            n = round(strtod(get("value_n", "string")));
            k = round(strtod(get("value_k", "string")));

            ax = get("spiral_axe");
            st = ax.userdata;

            if (n < 5 | n > 1000 | isnan(n)) then
                n = st.n;
                set("value_n", "string", string(n));
            end

            if (k < 1 | k > 12 | isnan(k)) then
                k = st.k;
                set("value_k", "string", string(k));
            end

            // fibonnaci sequence
            fib = fibonacci_seq(n+1);
            // ratio - approximation of the Golden Ratio
            phi = fib(2:n+1)./fib(1:n);
            theta = 2.0 * %pi * ( phi - 1.0 ) ./ phi;
            theta = theta($);
            rot = 0;            

            if (st.n <> n | st.k <> k) then
                st.n = n;
                st.k = k;
                draw_all_spirals(ax, n, k, theta)
            end
            
            st.rot = rot;
            set("spiral_axe", "userdata", st);            

            while round(strtod(get("value_n", "string"))) == n && round(strtod(get("value_k", "string"))) == k
                if get("stop_btn", "userdata") then
                    continue
                end
                rot = rot - %pi/180; // rotation step (~20 s per full rotation)
                for i = 1:k
                    base_angle = (i-1)*(2*%pi/k) + rot;
                    e = get("plot" + string(i));
                    x = zeros(n, 1); y = zeros(n, 1); angle_ = base_angle;
                    r = 1;

                    for i = 1:n
                        x(i) = r * cos(angle_);
                        y(i) = r * sin(angle_);
                        angle_ = modulo(angle_ + theta, 2*%pi);
                        r = sqrt(i + 1);
                    end
                    e.data = [x, y];
                end
                st.rot = rot;
                set("spiral_axe", "userdata", st);
                
                sleep(100);

            end
        end
    catch
        // window closed
        lasterror();
    end
endfunction

function animation()
    running = ~get("stop_btn", "userdata");
    if running then
        set("stop_btn", "string", "Play");
    else
        set("stop_btn", "string", "Stop");
    end
    set("stop_btn", "userdata", running);
endfunction

function fibonacci_gui()
    close();
    fig = figure("figure_position", [247, 167], ...
        "background", -2, ...
        "resize", "off", ...
        "axes_size", [730 710], ...
        "default_axes", "off", ...
        "infobar_visible", "off", ...
        "figure_name", "Fibonacci Spiral — (Fibonacci Day — Nov 23)", ...
        "layout", "border", ...
        "visible", "off", ...
        "tag", "fig_fib");

    axe_fr = uicontrol(fig, ...
        "style", "frame", ...
        "backgroundcolor", [1 1 1], ...
        "tag", "axe_fr", ...
        "constraints", createConstraints("border", "center"));

    st = struct("n", [], "k", []);
    ax = newaxes(axe_fr);
    ax.tag = "spiral_axe";
    ax.userdata = st;

    top_fr = uicontrol(fig, ...
        "style", "frame", ...
        "backgroundcolor", [1 1 1], ...
        "layout", "border", ...
        "constraints", createConstraints("border", "top", [10 70]));

    uicontrol(top_fr, ...
        "style", "text", ...
        "string", "Fibonacci Spiral — (Fibonacci Day — Nov 23)", ...
        "fontsize", 15, ...
        "horizontalalignment", "center", ...
        "backgroundcolor", [1 1 1]);

    bg = [248 248 248]./255;
    fr = uicontrol(fig, ...
        "style", "frame", ...
        "backgroundcolor", bg, ...
        "constraints", createConstraints("border", "bottom", [10 60]));

    // n - number of fibonacci terms
    uicontrol(fr, ...
        "style", "text", ...
        "string", "Number of Fibonacci terms", ...
        "fontsize", 11, ...
        "backgroundcolor", bg, ...
        "position", [30 20 150 30]);

    uicontrol(fr, ...
        "style", "text", ...
        "string", "<html>5 &le; n &le; 1000", ...
        "fontsize", 11, ...
        "backgroundcolor", bg, ...
        "fontangle", "italic", ...
        "horizontalalignment", "center", ...
        "position", [30 7 130 17]);

    uicontrol(fr, ...
        "style", "edit", ...
        "string", string(500), ...
        "fontsize", 11, ...
        "tag", "value_n", ...
        "horizontalalignment", "right", ...
        "enable", "on", ...
        "position", [185 20 100 25]);

    // k - number of spirals
    uicontrol(fr, ...
        "style", "text", ...
        "string", "Number of spirals", ...
        "fontsize", 11, ...
        "backgroundcolor", bg, ...
        "position", [350 20 100 30]);

    uicontrol(fr, ...
        "style", "text", ...
        "string", "<html>1 &le; k &le; 12", ...
        "fontsize", 11, ...
        "backgroundcolor", bg, ...
        "fontangle", "italic", ...
        "horizontalalignment", "center", ...
        "position", [350 7 70 17]);

    uicontrol(fr, ...
        "style", "edit", ...
        "string", string(4), ...
        "fontsize", 11, ...
        "tag", "value_k", ...
        "horizontalalignment", "right", ...
        "enable", "on", ...
        "position", [460 20 100 25]);

    uicontrol(fr, ...
    "style", "pushbutton", ...
    "string", "Stop", ...
    "callback", "animation()", ...
    "tag", "stop_btn", ...
    "callback_type", 10, ...
    "userdata", %f, ...
    "position", [600 20 100 25]);

    fig.visible = "on";
endfunction

fibonacci_gui()
animate_spirals()
1 Like