http://alecjacobson.com/art/digital/

http://alecjacobson.com/art/

## Archive for January, 2011

### anyone can make a beautiful work of art even you

Monday, January 31st, 2011### “close” packing of random spheres

Tuesday, January 18th, 2011Here’s a matlab snippet to create a “packing of spheres” in 3d centered at 100 random locations in the unit cube. The basic idea is to pick 100 random locations in 3D, then compute their delaunay tessellation. Then place a sphere at every location with radius equal to half the minimum distance to any of that location’s neighbors in the tessalation. After you have that you could iterate over each sphere making as big as will fit: for each location check all neighbors and find the maximum radius its sphere could have before intersecting existing spheres. I’m using bubbleplot3 to display the spheres.

```
% Generate 100 random 3d points in the unit cube
V = rand(100,3);
% Construct their Delaunay triangulation: tetrahedral mesh
T = delaunay3(V(:,1),V(:,2),V(:,3));
% Gather all edges of the tet mesh
E = [ T(:,1) T(:,2); T(:,2) T(:,3); T(:,3) T(:,4); T(:,4) T(:,1)];
% include both edge directions
E = [E; fliplr(E)];
% Indices of source vertex of each edge as seen by each other vertex
EE1 = repmat(E(:,1),1,size(V,1));
% length of each edge
d= (sqrt(sum((V(E(:,1),:) - V(E(:,2),:)).^2,2)));
% lengths as seen by each other vertex
DD = repmat(d,[1,size(V,1)]);
% indices of all vertices as see by each edge (source vertex)
II = repmat(1:size(V,1),size(E,1),1);
% create a mask that is infinity if source vertex == vertex
mask = (EE1 == II).^-1;
% compute minimum distance to any vertex: radius of largest ball
r = min(mask.*DD)./2;
% plot mesh and balls
figure
subplot(1,3,1);
plot3(V(:,1),V(:,2),V(:,3),'.')
axis equal
campos([8,-4.5,5])
title('centers');
subplot(1,3,2);
tetramesh(T,V,'FaceAlpha',0.1);
axis equal
campos([8,-4.5,5])
title('delaunay tessellation');
subplot(1,3,3);
bubbleplot3(V(:,1),V(:,2),V(:,3),r,repmat([0.3,0.2,0.7],size(V,1),1))
axis equal
campos([8,-4.5,5])
title('conservative packing of spheres');
shading interp; camlight right; lighting phong;
```

There seems to be a very well studied connection between sphere packing and delaunay tessellation. Lots more to investigate here…

### Screen capture into photoshop

Monday, January 10th, 2011Making figures for our upcoming submission I find my self repetitively taking screen captures of my program and then pasting them into photoshop where I can crop and arrange them. This is a lot of clicking around and switching apps and it’s hard to take a sequence of shots quickly. I could make just write all the screen shots to file and load them into photoshop as a batch. But Photoshop’s batch loader script is slow, so pasting is the fastest way to get all the images as layers and move on to editing.

Here’s a script that stays open with a prompt asking whether to take another screenshot and dump it into the current photoshop document. Replace MYAPP with the name of the app your are trying to screen capture.

```
repeat
tell application "MYAPP"
activate
display dialog "Screen capture and paste into Photoshop?"
end tell
do shell script "screencapture -c"
tell application "Adobe Photoshop CS5"
activate
tell current document
paste
end tell
end tell
end repeat
```

### balloon man

Wednesday, January 5th, 2011### Dock or “Command Tab” stop working

Wednesday, January 5th, 2011For some reason “command tab” or “apple tab” stopped working for me this morning. This was easy to recover from once I remember that this feature is actually controlled by the Dock application. Using Terminal.app I issued:

```
killall Dock
open /System/Library/CoreServices/Dock.app/
```

Which kills the Dock and restarts it.

### Write a matrix to file in matlab, read a matrix from file in python with numpy

Sunday, January 2nd, 2011Since matlab and numpy seem to disagree about csv, the easiest way I’ve found to export a matlab matrix to file and then read it in python with numpy is calling from matlab:

```
save -ascii 'A.txt' A
```

Then in python with numpy:

```
from numpy import *
A = genfromtxt('A.txt');
```

To go the other direction use from python with numpy:

```
from numpy import *
savetxt("A.txt", A)
```

And from matlab:

```
load -ascii A.txt A
```

When I use these to save a matrix from matlab to file, read it into a numpy array then save it out to file and load it back into matlab I get error between the two matrices on the order of 1e-15 (i.e. exact up to double precision with the default settings).

The above code works for 1D and 2D arrays/vectors/dense matrices. Hopefully I will find something just as easy for sparse matrices.

source (although it’s rather incomplete on this issue) or better source