Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 2 down vote accepted

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

share|improve this answer
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? – Aky Nov 14 '13 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]} *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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