2
$\begingroup$

I am trying to fill a cylinder uniformly (not randomly) with spheres. How can I do this efficiently for several different radii? The radius of all the spheres in the cylinder are the same but that radius can change so rows need to be added/removed to fit inside the cylinder.

enter image description here

Currently I am adding the rows one by one and I want to do create rows more efficiently.

L = 10; R1 = r0/100; R2 = L/5;

Graphics3D[{
  {Opacity@0.3, Cylinder[{{0, 0, 0}, {L, 0, 0}}, R2]},
  {Blue, Table[
      Sphere[{#, 2*i*r0, -4*r0}, r0], {i, -(R2 - 2*r0), R2 - r0, 
       2*r0}],
     Table[
      Sphere[{#, 2*i*r0, 0}, r0], {i, -(R2 + r0), R2 + 2*r0, 2*r0}],
     Table[
      Sphere[{#, 2*i*r0, 4*r0}, r0], {i, -(R2 - 2*r0), R2 - r0, 
       2*r0}]} & /@ Range[0, L, 2*r0],
  {Blue, Table[
      Sphere[{#, 2*i*r0, -2*r0}, r0], {i, -(R2 - r0), R2 + r0, 2*r0}],
     Table[
      Sphere[{#, 2*i*r0, 2*r0}, r0], {i, -(R2 - r0), R2 + r0, 
       2*r0}]} & /@ Range[r0, L - r0, 2*r0],
  }, Boxed -> False, ViewPoint -> Front, ImageSize -> {600, 400}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.