• Remix
  • Share
  • New Entry

on 23 Oct 2021
  • 5
  • 62
  • 2
  • 0
  • 279
% Script for drawing a gallery of Hypotrochoids, more commonly know as
% spirographs.
% Sequence of inputs that compute different kinds of spirographs
% Normally I'd use R1, R2, R3 - but I needed to cut back on some
% characters.
% Also, replaced spaces in array with ; so I could save the ' char
% needed to transpose these.
A=[.2;.1;.1;.04;.2;.4];
B=[.7;.4;.8;.4;.9;.9];
C=[.5;.4;.5;.7;.7;.5];
% Each input combo requires a different number of loops around before it
% reconnects with itself. Here are those sizes, but times 2pi to represent
% the loops as angles.
Z=[7;4;8;10;9;9]*pi*2;
% These inputs m,n,p are intermediate values.
% Since A,B,C are column vectors, and colonop is a row vector, this makes
% a 2d matrix through scalar expansion. (Check out Loren's blog post
% about that.) What this means is that each row represents the computed
% coordinates of one loops and allows all the different shapes to be
% computed at the same time. This saves a few characters, as the
% alternative is to do this one shape at a time in a loop. The chars
% saved are the array subsref into A,B,C - or 3chars per variable
% occurance. (ie A instead of A(k) )
% Also, use colonop for m as it is shorter than linspace.
m=(0:.001:1).*Z;
n=m.*(A./B);
p=A-B;
X=p.*cos(m)-cos(n).*C;
Y=p.*sin(m)-sin(n).*C;
% Have colors expand out from the center.
Z=hypot(X,Y);
% I wanted to save characters by skipping the call to tiledlayout, but in
% 'flow' mode I ended up with 2 columns instead of 3. This call to tiled
% layout was shorter than tweaking the figure size, and it let me change
% the padding to make the pictures bigger.
tiledlayout(2,3,'pad','t')
% Loop over each row and display it.
for k=1:6
nexttile
fill(X(k,:),Y(k,:),Z(k,:));
axis off equal
end
% Expensive character wise, but I think this colormap looks nice for these shapes.
colormap(flipud(turbo))
Remix Tree