I'd like to draw a large number of 3D cylinders using Graphics3D. My list specifying the cylinders has elements that look like the following:

{ {{{p1x1, p1y1, p1z1}, {p2x1, p2y1, p2z1}},r1}, {{{p1x2, p1y2, p1z2}, {p2x2, p2y2, p2z2}},r2}, {{{p1x3, p1y3, p1z3}, {p2x3, p2y3, p2z3}},r3}, ...}

Here, the first subelement, e.g. {{p1x1, p1y1, p1z1}, {p2x1, p2y1, p2z1}}, specifies the coordinates for the two end-points of the cylinder, and the the second subelement, e.g. r1 or r2, specifies the radius of the specific cylinder.

Is there a way to use mapping to draw all of these cylinders with a condensed line of code?


2 Answers 2


Simple /@ does it! Form $1500$ random cylinders from data.

cyildat =Table[{RandomReal[{-100, 100}, {2, 3}], RandomReal[5]}, {1500}];
Graphics3D[{EdgeForm[None],Directive[Opacity@RandomReal[{.4, .9}], Hue[RandomReal[]]], 
Cylinder[First@#, Last@#]} & /@ cyildat,(* Map on your data *)
Boxed -> False,ImageSize -> 800]

enter image description here

  • $\begingroup$ For some reason, this code is causing Mathematica 9.0.1 to crash on a mid-2013 MacBook Air running OS X 10.9 (Mavericks). It works, though, if I make the number of cylinders smaller. (Haven't determined how small before it starts crashing.) Anyone else experiencing this problem? $\endgroup$
    – Aky
    Commented Nov 14, 2013 at 6:08

What you want is Apply:

Cylinder @@@ {{{{p1x1, p1y1, p1z1}, {p2x1, p2y1, p2z1}}, 
   r1}, {{{p1x2, p1y2, p1z2}, {p2x2, p2y2, p2z2}}, 
   r2}, {{{p1x3, p1y3, p1z3}, {p2x3, p2y3, p2z3}}, r3}}

(* {Cylinder[{{p1x1, p1y1, p1z1}, {p2x1, p2y1, p2z1}}, r1], 
 Cylinder[{{p1x2, p1y2, p1z2}, {p2x2, p2y2, p2z2}}, r2], 
 Cylinder[{{p1x3, p1y3, p1z3}, {p2x3, p2y3, p2z3}}, r3]} *)

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.