Cylinder Generator

The Cylinder function creates the graphics primitives necessary for rendering a closed cylinder. By default the axis of the cylinder is parallel to the vertical axis. The arguments are the length of the cylinder, the radius, the position of the center of the cylinder and a final optional argument specifying a vector parallel to the central-axis of the cylinder.

The function

Cylinder[ht_, rad_, center_, centralaxis_: {0, 0, 1}] :=  
      Module[{upts, dpts, polypts}, 
frstang  = 
          If[centralaxis  === {0, 0, 1}, 0,  
            ArcTan[centralaxis[[3]], 
              centralaxis[[2]]]]; 
scndang  =  
          If[centralaxis  === {0, 0, 1}, 0, 
            ArcTan[centralaxis[[1]], 
              centralaxis[[2]]]]; 
rotmat  =  {{Cos[
                scndang], Cos[frstang] Sin[scndang], 
              Sin[frstang] Sin[scndang]}, {-Sin[scndang], 
              Cos[frstang] Cos[scndang], 
              Cos[scndang] Sin[frstang]}, {0, -Sin[frstang], 
              Cos[frstang]}}; 
upts  =  
          Table[rotmat . {rad Cos[t], rad Sin[t], ht/2} +  center, {t, 0, 
              2 Pi, Pi/12}]; 
dpts  =  
          Table[rotmat . {rad Cos[t], rad Sin[t], -ht/2}  +  
              center, {t, 0, 2 Pi, Pi/12}]; 
polypts  =  
          Map[Flatten[#, 1] &, 
            MapThread[
              List, {Partition[upts, 2, 1], 
                Map[Reverse, 
                  Partition[dpts, 2, 1]]}]]; 
{{EdgeForm[], 
            Polygon[upts], Polygon[dpts]}, 
          Polygon /@ polypts}
]

An example

Here is an example with two cylinders both having their center at the point {1, 1, 2}. The first cylinder has its axis parallel to the vector {2,1,1}.

Show[
      Graphics3D[{Cylinder[5, 1, {1, 1, 2}, {2, 1, 1}], 
          Cylinder[4, 1, {1, 1, 2}]}], Axes  ->  True]

Slicing a solid

Here we use the Cylinder function to illustrate the method of "washers" for approximating the volume of the solid z = 4 - ( (x^2)+ (y^2)).

discs  =  
        Table[Cylinder[1/4, (Sqrt[i]), {0, 0, 4  - i + 1/8 }], {i, 1/4, 4, 
            1/4}];

Show[Graphics3D[discs], Axes  ->  True]