{"id":8194,"date":"2022-01-03T22:07:27","date_gmt":"2022-01-04T03:07:27","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=8194"},"modified":"2022-01-03T22:07:27","modified_gmt":"2022-01-04T03:07:27","slug":"anatomy-of-a-cube","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2022\/01\/03\/anatomy-of-a-cube\/","title":{"rendered":"Anatomy of a Cube"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>A cube is the familiar three-dimensional solid with eight vertices, six faces and twelve edges. I have been working with cubes recently in posts about both the Menger sponge fractal and the 4-by-4 matrix from computer graphics.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#1fa7f07c-be2a-44fe-a34a-0dc3e6672e06\">Vertices<\/a><\/li><li><a href=\"#83452c44-3a54-4a0c-b93a-d1ecc99b8ab5\">Edges<\/a><\/li><li><a href=\"#81261545-a0d3-467e-8dd8-b5ef3c2d2824\">Wireframe<\/a><\/li><li><a href=\"#11024e9e-fbab-4e1b-902e-fb283954f9f3\">Faces<\/a><\/li><\/ul><\/div><h4>Vertices<a name=\"1fa7f07c-be2a-44fe-a34a-0dc3e6672e06\"><\/a><\/h4><p>Cartesian coordinates, <tt>V<\/tt>, for the eight vertices of a cube can be generated from the binary representation of <tt>0:7<\/tt>.<\/p><pre class=\"codeinput\">    j = (0:7)'\r\n<\/pre><pre class=\"codeoutput\">\r\nj =\r\n\r\n     0\r\n     1\r\n     2\r\n     3\r\n     4\r\n     5\r\n     6\r\n     7\r\n\r\n<\/pre><pre class=\"codeinput\">    k = dec2bin(j)\r\n<\/pre><pre class=\"codeoutput\">\r\nk =\r\n\r\n  8&times;3 char array\r\n\r\n    '000'\r\n    '001'\r\n    '010'\r\n    '011'\r\n    '100'\r\n    '101'\r\n    '110'\r\n    '111'\r\n\r\n<\/pre><pre class=\"codeinput\">    V = double(k-<span class=\"string\">'0'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nV =\r\n\r\n     0     0     0\r\n     0     0     1\r\n     0     1     0\r\n     0     1     1\r\n     1     0     0\r\n     1     0     1\r\n     1     1     0\r\n     1     1     1\r\n\r\n<\/pre><h4>Edges<a name=\"83452c44-3a54-4a0c-b93a-d1ecc99b8ab5\"><\/a><\/h4><p>The twelve edges of a cube are described by the adjacency matrix <tt>A<\/tt> of connections between its vertices.<\/p><pre class=\"codeinput\">    A = adjacency(V)\r\n    spy(A)\r\n<\/pre><pre class=\"codeoutput\">\r\nA =\r\n\r\n     0     1     1     0     1     0     0     0\r\n     1     0     0     1     0     1     0     0\r\n     1     0     0     1     0     0     1     0\r\n     0     1     1     0     0     0     0     1\r\n     1     0     0     0     0     1     1     0\r\n     0     1     0     0     1     0     0     1\r\n     0     0     1     0     1     0     0     1\r\n     0     0     0     1     0     1     1     0\r\n\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cube_blog_01.png\" alt=\"\"> <h4>Wireframe<a name=\"81261545-a0d3-467e-8dd8-b5ef3c2d2824\"><\/a><\/h4><p>A plot of the graph of <tt>A<\/tt> provides a wireframe view of our cube.<\/p><pre class=\"codeinput\">    G = graph(A);\r\n    p = plot(G, <span class=\"keyword\">...<\/span>\r\n        NodeLabel = string(k), <span class=\"keyword\">...<\/span>\r\n        NodeFontSize = 12, <span class=\"keyword\">...<\/span>\r\n        XData = V(:,3), <span class=\"keyword\">...<\/span>\r\n        YData = V(:,2), <span class=\"keyword\">...<\/span>\r\n        ZData = V(:,1));\r\n\r\n    axis([-1 4 -1 4 -1 4]\/3)\r\n    axis <span class=\"string\">square<\/span> <span class=\"string\">off<\/span> <span class=\"string\">vis3d<\/span>\r\n    view(3)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cube_blog_02.png\" alt=\"\"> <p>Let's replace the node labels with 1-based indices for rows of <tt>V<\/tt>.<\/p><pre class=\"codeinput\">p.NodeLabel = string(j+1);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cube_blog_03.png\" alt=\"\"> <h4>Faces<a name=\"11024e9e-fbab-4e1b-902e-fb283954f9f3\"><\/a><\/h4><p>A cube has six square faces. This array <tt>F<\/tt> provides the indices in <tt>V<\/tt> of the coordinates of the corners of each face.  The ordering ensures that the normal to each face points out of the cube.<\/p><pre class=\"codeinput\">    F = [ 1 5 7 3\r\n          3 7 8 4\r\n          1 3 4 2\r\n          2 4 8 6\r\n          1 2 6 5\r\n          5 6 8 7 ]\r\n<\/pre><pre class=\"codeoutput\">\r\nF =\r\n\r\n     1     5     7     3\r\n     3     7     8     4\r\n     1     3     4     2\r\n     2     4     8     6\r\n     1     2     6     5\r\n     5     6     8     7\r\n\r\n<\/pre><p>If you Google \"rgb gold\", you will get links to Web sites offering red-green-blue values for dozens of shades of the color gold. My forthcoming post about the complement of the Menger sponge fractal uses just two shades.<\/p><pre class=\"codeinput\">    gold = [212 175 55]\/256\r\n    dark = gold\/2\r\n<\/pre><pre class=\"codeoutput\">\r\ngold =\r\n\r\n    0.8281    0.6836    0.2148\r\n\r\n\r\ndark =\r\n\r\n    0.4141    0.3418    0.1074\r\n\r\n<\/pre><p>The single <tt>patch<\/tt> formed from <tt>V<\/tt> and <tt>F<\/tt> is just the skin enclosing our cube; its inside is hollow.<\/p><pre class=\"codeinput\">    cla\r\n    patch(Faces = F, <span class=\"keyword\">...<\/span>\r\n        Vertices = V, <span class=\"keyword\">...<\/span>\r\n        FaceColor = gold, <span class=\"keyword\">...<\/span>\r\n        EdgeColor = dark, <span class=\"keyword\">...<\/span>\r\n        LineWidth = 1.5);\r\n\r\n    axis([-1 4 -1 4 -1 4]\/3)\r\n    axis <span class=\"string\">square<\/span> <span class=\"string\">off<\/span> <span class=\"string\">vis3d<\/span>\r\n    view(3)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cube_blog_04.png\" alt=\"\"> <script language=\"JavaScript\"> <!-- \r\n    function grabCode_1f6e93fb58014cf8a9a56f0b0a985b4c() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='1f6e93fb58014cf8a9a56f0b0a985b4c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1f6e93fb58014cf8a9a56f0b0a985b4c';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2022 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_1f6e93fb58014cf8a9a56f0b0a985b4c()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2021a<br><\/p><\/div><!--\r\n1f6e93fb58014cf8a9a56f0b0a985b4c ##### SOURCE BEGIN #####\r\n%% Anatomy of a Cube\r\n% A cube is the familiar three-dimensional solid with eight vertices,\r\n% six faces and twelve edges. \r\n% I have been working with cubes recently in posts about both\r\n% the Menger sponge fractal and the 4-by-4 matrix from computer graphics.\r\n\r\n%% Vertices\r\n% Cartesian coordinates, |V|, for the eight vertices of a cube\r\n% can be generated from the binary representation of |0:7|.\r\n\r\n    j = (0:7)'\r\n    \r\n%%\r\n\r\n    k = dec2bin(j)\r\n    \r\n%%\r\n\r\n    V = double(k-'0')\r\n    \r\n%% Edges\r\n% The twelve edges of a cube are described by the adjacency matrix |A| of\r\n% connections between its vertices.\r\n\r\n    A = adjacency(V)\r\n    spy(A)\r\n    \r\n%% Wireframe\r\n% A plot of the graph of |A| provides a wireframe view of our cube.\r\n\r\n    G = graph(A);\r\n    p = plot(G, ...\r\n        NodeLabel = string(k), ...\r\n        NodeFontSize = 12, ...\r\n        XData = V(:,3), ...\r\n        YData = V(:,2), ...\r\n        ZData = V(:,1));\r\n\r\n    axis([-1 4 -1 4 -1 4]\/3)\r\n    axis square off vis3d\r\n    view(3)\r\n\r\n%%\r\n% Let's replace the node labels with 1-based indices for rows of |V|.\r\n\r\np.NodeLabel = string(j+1);\r\n    \r\n%% Faces\r\n% A cube has six square faces.\r\n% This array |F| provides the indices in |V| of the coordinates\r\n% of the corners of each face.  The ordering ensures\r\n% that the normal to each face points out of the cube.\r\n\r\n    F = [ 1 5 7 3\r\n          3 7 8 4\r\n          1 3 4 2\r\n          2 4 8 6\r\n          1 2 6 5\r\n          5 6 8 7 ]\r\n\r\n%%\r\n% If you Google \"rgb gold\", you will get links to Web sites offering \r\n% red-green-blue values for dozens of shades of the color gold.\r\n% My forthcoming post about the complement of the Menger sponge fractal\r\n% uses just two shades.\r\n\r\n    gold = [212 175 55]\/256\r\n    dark = gold\/2\r\n\r\n%%\r\n% The single |patch| formed from |V| and |F| is just the skin enclosing\r\n% our cube; its inside is hollow.\r\n\r\n    cla\r\n    patch(Faces = F, ...\r\n        Vertices = V, ...\r\n        FaceColor = gold, ...\r\n        EdgeColor = dark, ...\r\n        LineWidth = 1.5);\r\n    \r\n    axis([-1 4 -1 4 -1 4]\/3)\r\n    axis square off vis3d\r\n    view(3)\r\n\r\n         \r\n##### SOURCE END ##### 1f6e93fb58014cf8a9a56f0b0a985b4c\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/cube_blog_02.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction--><p>A cube is the familiar three-dimensional solid with eight vertices, six faces and twelve edges. I have been working with cubes recently in posts about both the Menger sponge fractal and the 4-by-4 matrix from computer graphics.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2022\/01\/03\/anatomy-of-a-cube\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":8206,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[23,34,37],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/8194"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=8194"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/8194\/revisions"}],"predecessor-version":[{"id":8200,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/8194\/revisions\/8200"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media\/8206"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=8194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=8194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=8194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}