0
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ And what have you tried? How are "pixels" related to spheres? $\endgroup$
    – BlacKow
    Apr 6, 2016 at 18:36
  • $\begingroup$ 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. $\endgroup$
    – user147813
    Apr 6, 2016 at 19:13
  • 1
    $\begingroup$ So please show us your code $\endgroup$
    – BlacKow
    Apr 6, 2016 at 19:15
  • $\begingroup$ Do you know how to do it in 2D? Try there first. $\endgroup$
    – bill s
    Apr 6, 2016 at 19:26

1 Answer 1

4
$\begingroup$

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}}]

enter image description here

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}}]

enter image description here

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:

enter image description here

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.

$\endgroup$
9
  • $\begingroup$ 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? $\endgroup$
    – MathX
    Apr 6, 2016 at 21:25
  • $\begingroup$ "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. $\endgroup$ Apr 6, 2016 at 21:28
  • $\begingroup$ @MathX in what sense the 3D matrix can be tricky? memory wise and such? I don't see any problem on going to 3D. $\endgroup$ Apr 6, 2016 at 21:35
  • $\begingroup$ @tsuresuregusa Smart using of PlotRangeinstead of RegionIntersection etc... It looks like the interest towards "boxed" questions is high now days. $\endgroup$
    – BlacKow
    Apr 6, 2016 at 21:35
  • $\begingroup$ @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 $\endgroup$ Apr 6, 2016 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.