2
$\begingroup$

I have the centers, radius, and xyz-coordinates of say 200 spheres that I'd like to plot in a box. I know how to do one sphere using this information and Graphics3D, but do not know how to efficiently draw all 200.

How can I do it?

$\endgroup$
  • $\begingroup$ In principle, you will want to Map the Sphere command over the list of centers, radii, etc. Can you show us the exact format of your data? Also, what do you mean by xyz coordinated? A sphere is already uniquely determined by its center and radius. $\endgroup$ – MarcoB Mar 11 '16 at 17:54
  • $\begingroup$ Thanks for your response. I get my data from running a post-processing script in MATLAB - so the format is flexible. I plan on importing four different arrays from MATLAB into MATHEMATICA: Arry1=x coordinates of the center, Arry2=y coordinates of the center, Arry3=z coordinates of the center, Arry4=radii. The mapping is exactly my problem here. How do i pass on the information to Graphics3D? $\endgroup$ – user147813 Mar 11 '16 at 18:27
4
$\begingroup$

Update:

After you clarified the format of your data that is imported in four arrays, here is a modified approach. Again, I will generate some fake data to play with:

array1 = RandomReal[{-10, 10}, 10]; (* = imported x coords *)
array2 = RandomReal[{-10, 10}, 10]; (* = imported y coords *)
array3 = RandomReal[{-10, 10}, 10]; (* = imported z coords *)
array4 = RandomReal[2, 10];         (* = imported radii    *)

You can then generate your Sphere expressions using MapThread (the cleaner, more readable choice, as suggested by J.M. below):

spheres = MapThread[Sphere[{#1, #2, #3}, #4] &, {array1, array2, array3, array4}]; 

or alternatively by explicit application, which was my first attempt:

spheres = Sphere[{#1, #2, #3}, #4] & @@@ Transpose[{array1, array2, array3, array4}];

Generate some play data for 10 random spheres in the form {{centerX, centerY, centerZ}, radius}:

spheredata = Transpose@{RandomReal[{-10, 10}, {10, 3}], RandomReal[2, 10]}

The generate Sphere objects from each of them and feed them to Graphics3D:

Graphics3D[
  Sphere @@@ spheredata
]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Additionally if all the spheres had equal radii, then Sphere[(* point list *), (* radius *)] suffices. $\endgroup$ – J. M. will be back soon Mar 11 '16 at 18:16
  • $\begingroup$ OK, got it by manipulating your example. Thanks for your help. $\endgroup$ – user147813 Mar 11 '16 at 18:37
  • $\begingroup$ @user147813 I have just posted a modification of my answer for your format, but if you got it to work on your own already already, even better! Glad to help! $\endgroup$ – MarcoB Mar 11 '16 at 18:39
  • $\begingroup$ I'd have done it as spheres = MapThread[Sphere[{#1, #2, #3}, #4] &, {array1, array2, array3, array4}]; myself. $\endgroup$ – J. M. will be back soon Mar 11 '16 at 18:45
  • $\begingroup$ @J.M. Yes, that's cleaner. I'll add it to my answer. $\endgroup$ – MarcoB Mar 11 '16 at 18:49

Your Answer

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

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