Seasonal greetings of the Greater Solstice Metropolitan Area! As I write this, the solstitial sun sets on the shortest day of my northern hemispheric year. Winter is here. So naturally, my mind turns to cookies.
He's back, this time with code for seasonal cookies and snowflakes. If you want to look at his code, you can find it in his
Digital Art with MATLAB repo. In fact, let's go look at it now.
We can start by cloning our own copy in MATLAB. You can do this directly from the Current Folder Browser. Select "Manage Files..." from the Source Control context menu.
Eric made some cookie code using blobby implicit surfaces as described by Jim Blinn. We can use Eric's template as cookie-cutter (heh) to stamp out other designs. Shown below is the code from gingerbreadperson.m, only I've substituted my own design at the top: a candy cane.
% Create a gingerbread person cookie
% Convert K into array of blob centers
% Scale and offset centers into 3D cookie space
C=[x*5,y*5]+5; C(:,3)=10;
% Compute a volume using a short implementation of Blinn's blobs
nx=size(K,2)*5+10; ny=size(K,1)*5+10; nz=20; % Size of volume
[y,x,z]=ndgrid(1:ny,1:nx,1:nz);
vol=vol+exp(-.05*((C(i,1)-x).^2 + (C(i,2)-y).^2 + (C(i,3)-z).^2));
% Compute isosurface from the blobs
% Draw the isosurface using patch
patch(S,'FaceColor','#a56c3c','EdgeColor','none'); % cookie
It's not the most shapely candy cane, but it will do. Let's add some icing.
patch(S,'FaceColor','interp','EdgeColor','none','FaceVertexCData',S.vertices(:,3));
colormap(validatecolor({ '#a56c3c' '#f09090' }, 'multiple'));
Obviously, it's easy to draw with X's into the variable K to make any cookie shape that pleases you. What happens if we make it random?
% Create a gingerbread blob
K = double(rand(10,10)>0.5);
% Convert K into array of blob centers
% Scale and offset centers into 3D cookie space
C=[x*5,y*5]+5; C(:,3)=10;
% Compute a volume using a short implementation of Blinn's blobs
nx=size(K,2)*5+10; ny=size(K,1)*5+10; nz=20; % Size of volume
[y,x,z]=ndgrid(1:ny,1:nx,1:nz);
vol=vol+exp(-.05*((C(i,1)-x).^2 + (C(i,2)-y).^2 + (C(i,3)-z).^2));
% Compute isosurface from the blobs
patch(S,'FaceColor','interp','EdgeColor','none','FaceVertexCData',S.vertices(:,3));
colormap(validatecolor({ '#a56c3c' '#8090f0' }, 'multiple'));
I like my random cookies, and I'm now inclined to try some in the kitchen.
I'll close with Eric's snowflake code. Here's the default.
But, just as with the cookies (and as with real-life snowflakes), we can opt for a random flake.
steps = randi(10,1,randi(8)+2)
Happy holidays, and thanks for the cookies, Eric!
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.