Periodic boundary for 3D sphere packing

I have the $x,y,z$ coordinates and radius $r$, of $N$ spherical particles, packed in a box with size $L$. For some cases, $x \pm r$ and/or $y\pm r$ and/or $z\pm r$ go outside the box. I want to be able to wrap them around so that the fraction of the sphere not outside the box shows on the other side. Second, I want to be able to turn this into a binary matrix of $N \times N \times N$ with zeros and ones representing void and solid pixels. I know for the first part I have to use Mod[] but can't figure out how. Any help is much appreciated.

• And what have you tried? How are "pixels" related to spheres? – BlacKow Apr 6 '16 at 18:36
• The pixels - or i should have said voxels - come into picture when putting the system on a lattice. That's of a secondary importance. In terms of the wrapping the fraction of sphere that goes out of the box in - i tried identifying that bounded region and moving it by \pm L depending on where it goes out MATLAB. However that approach seems to get too messy too soon. I prefer working with Mathematica and thought there might be a more efficient approach on this platform. – user147813 Apr 6 '16 at 19:13
• So please show us your code – BlacKow Apr 6 '16 at 19:15
• Do you know how to do it in 2D? Try there first. – bill s Apr 6 '16 at 19:26

3D is the same as 2D so for simplicity I stick to 2D.

First part: plot the position vector shifted 2L in each direction, inefficient but it works.

L = 10;
pos = RandomInteger[{-L, L}, {10, 2}]
Show[Graphics[{EdgeForm[{Thick, Black}], FaceForm[White],
Rectangle[{-L, -L}, {L, L}]}],
Table[Graphics[
MapIndexed[{Hue[#2/10], Disk[#1 + {2 i L, 2 j L}]} &,
pos]], {i, -1, 1, 1}, {j, -1, 1, 1}],
PlotRange -> {{-L, L}, {-L, L}}]


Second part: take the Floor of the position and put a square on it.

Show[Graphics[{EdgeForm[{Thick, Black}], FaceForm[White],
Rectangle[{-L, -L}, {L, L}]}],
Table[Graphics[
MapIndexed[{Black, Rectangle[Floor[#1] + {2 i L, 2 j L}]} &,
pos]], {i, -1, 1, 1}, {j, -1, 1, 1}],
PlotRange -> {{-L, L}, {-L, L}}]


Here there's no need to plot the shifted data since Floor takes care of putting the particles inside the box.

And a gratuitous gif:

full disclusure: my phd was on particle simulation so I developed a love for moving colorful dots. You can make really cool flipbooks with this.

• I'm not completely sure but I think OP required the new box to enclose the the spheres. So that none of them are cut off by the box. Also the 3D matrix part could be tricky? – MathX Apr 6 '16 at 21:25
• "I want to be able to wrap them around so the fraction of the sphere not outside of the box shows on the other size." I understood this as he wants half spheres if they are beyond the borders, like orange and pink. Maybe I'm wrong though. – tsuresuregusa Apr 6 '16 at 21:28
• @MathX in what sense the 3D matrix can be tricky? memory wise and such? I don't see any problem on going to 3D. – tsuresuregusa Apr 6 '16 at 21:35
• @tsuresuregusa Smart using of PlotRangeinstead of RegionIntersection etc... It looks like the interest towards "boxed" questions is high now days. – BlacKow Apr 6 '16 at 21:35
• @BlacKow it was mas first thought, to region difference with some band on the borders, but that would have been too complicated if you are only interested on seeing them – tsuresuregusa Apr 6 '16 at 21:37