{"id":9309,"date":"2022-10-17T18:17:12","date_gmt":"2022-10-17T22:17:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=9309"},"modified":"2022-10-18T11:09:43","modified_gmt":"2022-10-18T15:09:43","slug":"modfun-a-short-program-produces-impressive-graphics","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2022\/10\/17\/modfun-a-short-program-produces-impressive-graphics\/","title":{"rendered":"modfun, A Short Program Produces Impressive Graphics"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>This nifty graphics gem started with a contribution by Paul Villain to the MATLAB 2022 Mini Hack, currently taking place on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/contests.html\">MATLAB Central<\/a>.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#56a0ec8a-8f48-4d49-951d-ede8fd35a5a3\"><tt>modfun<\/tt><\/a><\/li><li><a href=\"#a4a42958-572e-4506-9fd6-fd5704698093\">code<\/a><\/li><li><a href=\"#580b1824-d2ab-4ea9-883a-1aa69d0c8bb1\">Animation<\/a><\/li><li><a href=\"#24d1d844-3760-4a2d-bd68-8671d9ff55b6\">Gallery<\/a><\/li><li><a href=\"#b5cd86af-e8cd-4db6-9341-fa63d035cc3d\">Quiz<\/a><\/li><li><a href=\"#336d3ee4-5965-44e5-bc73-926eb724f3f4\">Software<\/a><\/li><\/ul><\/div><h4><tt>modfun<\/tt><a name=\"56a0ec8a-8f48-4d49-951d-ede8fd35a5a3\"><\/a><\/h4><p>Villain's contribution is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/communitycontests\/contests\/5\/entries\/9880\">102 mod 500<\/a> .  My rewrite is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/communitycontests\/contests\/5\/entries\/11093\"><tt>modfun<\/tt><\/a>. Villain's <tt>102<\/tt> and <tt>500<\/tt> become the parameters <tt>m<\/tt> and <tt>n<\/tt>.<\/p><pre>  modfun(m,n) connects n points, z(j), equally spaced\r\n  around the complex unit circle, by n+1 straight lines.\r\n  The j-th line connects z(j+1) to z(mod(j*m,n)+1).<\/pre><h4>code<a name=\"a4a42958-572e-4506-9fd6-fd5704698093\"><\/a><\/h4><p>The basic code uses complex arithmetic and is only eight lines long. When the graphics is done with <tt>line<\/tt> instead of <tt>plot<\/tt>, it is not necessary to use <tt>hold on<\/tt>.<\/p><pre>  function modfun(m,n)\r\n      init_fig\r\n      z = exp(2i*pi*(0:n)\/n);\r\n      for j = 0:n\r\n          zj = [z(j+1),z(mod(j*m,n)+1)];\r\n          line(real(zj),imag(zj))\r\n      end\r\n  end<\/pre><p>The initialization makes <tt>line<\/tt> possible.<\/p><pre>  function init_fig\r\n      axis([-1 1 -1 1])\r\n      axis square\r\n      axis off\r\n  end<\/pre><h4>Animation<a name=\"580b1824-d2ab-4ea9-883a-1aa69d0c8bb1\"><\/a><\/h4><p>This animation of <tt>modfun(105,200)<\/tt> has one frame for every five lines.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/modfun105.gif\" alt=\"\"> <\/p><h4>Gallery<a name=\"24d1d844-3760-4a2d-bd68-8671d9ff55b6\"><\/a><\/h4><p>A sample.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/modfuns1.png\" alt=\"\"> <\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/modfuns2.png\" alt=\"\"> <\/p><h4>Quiz<a name=\"b5cd86af-e8cd-4db6-9341-fa63d035cc3d\"><\/a><\/h4><p>Match these calls to <tt>modfun<\/tt> to the plots in the Gallery.<\/p><pre>  modfun(88,179)\r\n  modfun(89,220)\r\n  modfun(99,200)\r\n  modfun(101,200)\r\n  modfun(111,200)\r\n  modfun(113,188)\r\n  modfun(126,188)\r\n  modfun(126,200)<\/pre><h4>Software<a name=\"336d3ee4-5965-44e5-bc73-926eb724f3f4\"><\/a><\/h4><p>An interactive <tt>modfun<\/tt>.<\/p><p><a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/modfun.m\">modfun.m<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_ce8d1e93d27746db8cedca87febedacd() {\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='ce8d1e93d27746db8cedca87febedacd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' ce8d1e93d27746db8cedca87febedacd';\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_ce8d1e93d27746db8cedca87febedacd()\"><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; R2022a<br><\/p><\/div><!--\r\nce8d1e93d27746db8cedca87febedacd ##### SOURCE BEGIN #####\r\n%% |modfun|, A Short Program Produces Impressive Graphics\r\n% This nifty graphics gem started with a contribution by Paul Villain\r\n% to the MATLAB 2022 Mini Hack, currently taking place on\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/contests.html MATLAB Central>.\r\n\r\n%% |modfun|\r\n% Villain's contribution is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/communitycontests\/contests\/5\/entries\/9880\r\n% 102 mod 500> .  My rewrite is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/communitycontests\/contests\/5\/entries\/11093\r\n% |modfun|>.\r\n% Villain's |102| and |500| become the parameters |m| and |n|.\r\n%\r\n%    modfun(m,n) connects n points, z(j), equally spaced\r\n%    around the complex unit circle, by n+1 straight lines.\r\n%    The j-th line connects z(j+1) to z(mod(j*m,n)+1).\r\n%\r\n%% code\r\n% The basic code uses complex arithmetic and is only eight lines long.\r\n% When the graphics is done with |line| instead of |plot|, \r\n% it is not necessary to use |hold on|.\r\n%\r\n%    function modfun(m,n)\r\n%        init_fig\r\n%        z = exp(2i*pi*(0:n)\/n);\r\n%        for j = 0:n\r\n%            zj = [z(j+1),z(mod(j*m,n)+1)];\r\n%            line(real(zj),imag(zj))\r\n%        end\r\n%    end\r\n% \r\n% The initialization makes |line| possible. \r\n%\r\n%    function init_fig\r\n%        axis([-1 1 -1 1])\r\n%        axis square\r\n%        axis off\r\n%    end\r\n\r\n%% Animation\r\n% This animation of |modfun(105,200)| has one frame for every five lines.\r\n%\r\n% <<modfun105.gif>>\r\n%\r\n\r\n%% Gallery\r\n% A sample.\r\n%\r\n% <<modfuns1.png>>\r\n%\r\n% <<modfuns2.png>>\r\n\r\n%% Quiz\r\n% Match these calls to |modfun| to the plots in the Gallery.\r\n%\r\n%    modfun(88,179)\r\n%    modfun(89,220)\r\n%    modfun(99,200)\r\n%    modfun(101,200)\r\n%    modfun(111,200)\r\n%    modfun(113,188)\r\n%    modfun(126,188)\r\n%    modfun(126,200)\r\n\r\n%% Software\r\n% An interactive |modfun|.\r\n% \r\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/modfun.m modfun.m>.\r\n##### SOURCE END ##### ce8d1e93d27746db8cedca87febedacd\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/modfun105.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction--><p>This nifty graphics gem started with a contribution by Paul Villain to the MATLAB 2022 Mini Hack, currently taking place on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/contests.html\">MATLAB Central<\/a>.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2022\/10\/17\/modfun-a-short-program-produces-impressive-graphics\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":9318,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,23,37],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/9309"}],"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=9309"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/9309\/revisions"}],"predecessor-version":[{"id":9351,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/9309\/revisions\/9351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media\/9318"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=9309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=9309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=9309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}