2

modfun, A Short Program Produces Impressive Graphics

 1 year ago
source link: https://blogs.mathworks.com/cleve/2022/10/17/modfun-a-short-program-produces-impressive-graphics/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

This nifty graphics gem started with a contribution by Paul Villain to the MATLAB 2022 Mini Hack, currently taking place on MATLAB Central.

Contents

modfun

Villain's contribution is 102 mod 500 . My rewrite is modfun. 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).

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
  end

The initialization makes line possible.

  function init_fig
      axis([-1 1 -1 1])
      axis square
      axis off
  end

Animation

This animation of modfun(105,200) has one frame for every five lines.

modfun105.gif

Gallery

A sample.

modfuns1.png
modfuns2.png

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)

Software

An interactive modfun.

modfun_app.m.

Published with MATLAB® R2022a


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK