![]() |
modfun, A Short Program Produces Impressive Graphics - Cleve Moler on Mathematics and Computing
This nifty graphics gem started with a contribution by Paul Villain to the MATLAB 2022 Mini Hack, currently taking place on [URL="https://www.mathworks.com/matlabcentral/contests.html"]MATLAB Central[/URL].
[B]Contents[/B] [LIST][*][URL="https://www.labfans.com/bbs/#bc213b21-2d10-4642-b5c0-bd063ce65d4c"]modfun[/URL][*][URL="https://www.labfans.com/bbs/#d66e0b8d-1378-4fef-b0bb-420f380daacf"]code[/URL][*][URL="https://www.labfans.com/bbs/#d2f8c556-f215-4361-89c6-dfbeb2bbe028"]Animation[/URL][*][URL="https://www.labfans.com/bbs/#7d5f14a5-3a82-40f1-9c70-c44b8d4a9a25"]Gallery[/URL][*][URL="https://www.labfans.com/bbs/#cda73389-b504-46c6-b079-31572fccdde4"]Quiz[/URL][/LIST][B]modfun[/B] Villain's contribution is [URL="https://www.mathworks.com/matlabcentral/communitycontests/contests/5/entries/9880"]102 mod 500[/URL] . My rewrite is [URL="https://www.mathworks.com/matlabcentral/communitycontests/contests/5/entries/11093"]modfun[/URL]. Villain's 102 and 500 become the parameters m and n. modfun(m,n) connects n points, z(j), equally spaced around the complex unit circle, by n+1 straight lines. The j-th line connects z(j+1) to z(mod(j*m,n)+1).[B]code[/B] The basic code uses complex arithmetic and is only eight lines long. When the graphics is done with line instead of plot, it is not necessary to use hold on. function modfun(m,n) init_fig z = exp(2i*pi*(0:n)/n); for j = 0:n zj = [z(j+1),z(mod(j*m,n)+1)]; line(real(zj),imag(zj)) end endThe initialization makes line possible. function init_fig axis([-1 1 -1 1]) axis square axis off end[B]Animation[/B] This animation of modfun(105,200) has one frame for every five lines. [IMG]http://blogs.mathworks.com/cleve/files/modfun105.gif[/IMG] [B]Gallery[/B] A sample. [IMG]http://blogs.mathworks.com/cleve/files/modfuns1.png[/IMG] [IMG]http://blogs.mathworks.com/cleve/files/modfuns2.png[/IMG] [B]Quiz[/B] Match these calls to modfun to the plots in the Gallery. modfun(88,179) modfun(89,220) modfun(99,200) modfun(101,200) modfun(111,200) modfun(113,188) modfun(126,188) modfun(126,200)[RIGHT][COLOR=gray][I] [URL="javascript:grabCode_9fe1545c722d44b5acd7198948e85482()"][I]Get the MATLAB code (requires JavaScript)[/I][/URL] Published with MATLAB® R2022a [/I][/COLOR][/RIGHT] [url=https://blogs.mathworks.com/cleve/2022/10/17/modfun-a-short-program-produces-impressive-graphics/?s_tid=feedtopost]More...[/url] |
所有时间均为北京时间。现在的时间是 23:30。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.