%% Bringing Back the Bucky Ball
% The Bucky Ball provides an elegant example of a graph, the connectivity of the Buckminster Fuller geodesic dome.
% We are also demonstrating publishing this blog with the Live Editor.  There is a button at the end of the post that downloads the live script so that you can run the animation on your own computer.
% You may see a second button at the end of the post that allows the live script to be executed by MATLAB Online and  the results displayed in your browser.  This feature is still under development and does not work in some browsers.
%% Bucky
% The statement

   B = bucky; 

% creates a sparse 60-by-60 matrix, the adjacency matrix of the connectivity graph for a geodesic dome, a soccer ball,  a carbon-60 molecule or a  truncated icosahedron.  These objects all have 32 faces; 12 of them are pentagons and 20 are hexagons.
%% Spy
% One view of this matrix is its spy plot, which shows the logical structure of nodes and edges.

   spy(B)

%
%% Graph
% To get other views, use the MATLAB graph object.  The statement

   G = graph(B);

% makes a graph.   By itself, a graph does not have any geometric structure. In order to plot it, you specify a layout, or coordinates, for the nodes. Coming up with good layouts is an art, as well as a science. There are currently six available layouts: 'circle', 'force', 'layered', 'subspace', 'force3' and 'subspace3'.  Let's use 'force3'.   This will give locate the nodes and edges of the graph in three dimensions.  The resulting image is recognized anywhere on Earth as a standard football or soccer ball.
% By the way, I am using the new assignment operator syntax for name-value pairs.

   plot(G, layout="force3", linewidth=1.5)
   axis square off

%% History
% The help entry for bucky,
%       BUCKY  Connectivity graph of the Buckminster Fuller geodesic dome.
%           B = BUCKY is the 60-by-60 sparse adjacency matrix of the
%           connectivity graph of the geodesic dome, the soccer ball,
%           and the carbon-60 molecule.
%       [B,V] = BUCKY also returns xyz coordinates of the vertices.
% Look at the code for bucky.
%       type bucky
% I wrote this function 30 years ago, when we were first developing sparse matrices.  It is almost one-hundred lines.  Looking at it now, it is very difficult to see how it works.  Most of the effort goes into placing the vertices.  We don't need the coordinates anymore.  With the three-dimensional force3 layout, the graph/plot function does that work.
%
%% Bend It Like Beckham
% Putting a little spin on a soccer ball makes it more interesting to watch.  Place transparent patches on two of the pentagons.

    k1 = 1:5;
    k2 = 56:60;
    colors = get(0,'defaultaxescolororder');
    P = plot(G, layout="force3", markersize=3, nodefontsize=8, linewidth=1.5);
    patch(P.XData(k1),P.YData(k1),P.ZData(k1), colors(2,:), facealpha=0.5);
    patch(P.XData(k2),P.YData(k2),P.ZData(k2), colors(5,:), facealpha=0.5);
    axis  tight square off vis3d 

% Turn off the node numbering.

    P.NodeLabel = {};

% Let her go.   On my laptop, it takes about 20 seconds to finish this loop.  The animation is being recorded and a playback widget ultimately appears.

    tic
    nframes = 120;
    d = 360/nframes;
    a = 0;
    e = 0;
    for frame = 1:nframes
        view(a,e);
        a = a - d;
        e = e + d;
        drawnow
    end
    view(a,e)
    
    toc

%% Thanks
% Thanks to Cheng Chen, Bill York and Christine Tobler for help with this post.
%% To Be Continued
% I am not through with this.  I have been learning a lot by experimenting with the code.  I'll be back with what I've learned.
% In the meantime, try
% Increment a or  e alone.
% Remove  off and vis3d.
% Some things to ponder,
% Can you color two opposing hexagons instead of two pentagons?
% Why do the node labels and colors sometimes swap hemispheres?
% Is the hemisphere swapping a bug or a feature?
% Does it appear to you that the ball is still moving slightly after the animation loop terminates?
%