Function Repository Resource:

RoundedCylinder

Source Notebook

Represent a cylinder with rounded edges

Contributed by: Jaroslav Kysela

ResourceFunction["RoundedCylinder"][{{x1,y1,z1},{x2,y2,z2}},r]

represents a rounded cylinder of radius r around the line from (x1,y1,z1) to (x2,y2,z2).

ResourceFunction["RoundedCylinder"][{{x1,y1,z1},{x2,y2,z2}}]

represents a rounded cylinder of radius 1.

Details and Options

ResourceFunction["RoundedCylinder"][] is equivalent to ResourceFunction["RoundedCylinder"][{{0,0,-1},{0,0,1}},1].
ResourceFunction["RoundedCylinder"] can be used in Graphics3D.
Graphics rendering is affected by directives such as FaceForm, Specularity, Opacity, and color.
The following option can be given:
RoundingRadius0.1the radius of rounded edges

Examples

Basic Examples (3) 

A unit radius and two units height rounded cylinder:

In[1]:=
Graphics3D[ResourceFunction["RoundedCylinder"][], Axes -> True]
Out[1]=

A rounded cylinder from the origin to {1,1,1} with radius 1/2:

In[2]:=
Graphics3D[
 ResourceFunction["RoundedCylinder"][{{0, 0, 0}, {1, 1, 1}}, 1/2]]
Out[2]=

Differently styled rounded cylinders:

In[3]:=
{Graphics3D[{Yellow, ResourceFunction["RoundedCylinder"][]}], Graphics3D[{Opacity[.5], ResourceFunction["RoundedCylinder"][]}], Graphics3D[{Specularity[.5], ResourceFunction["RoundedCylinder"][]}]}
Out[3]=

Scope (6) 

Rounded cylinders with different endpoints:

In[4]:=
Graphics3D[{ResourceFunction["RoundedCylinder"][], ResourceFunction["RoundedCylinder"][{{0, 3, 0}, {0, 5, 0}}, 1]}]
Out[4]=

Rounded cylinders with different radii:

In[5]:=
Graphics3D[{ResourceFunction["RoundedCylinder"][], ResourceFunction["RoundedCylinder"][{{-1, 5, 0}, {1, 5, 0}}, 2]}]
Out[5]=

Rounded cylinders with different rounding radii:

In[6]:=
Graphics3D[{ResourceFunction["RoundedCylinder"][RoundingRadius -> .9],
   ResourceFunction["RoundedCylinder"][{{1, 2, 0}, {1, 5, 0}}, RoundingRadius -> .3]}]
Out[6]=

Colored cylinders:

In[7]:=
Table[Graphics3D[{c, ResourceFunction["RoundedCylinder"][]}], {c, {Red, Green, Blue, Yellow}}]
Out[7]=

Different properties can be specified for the front and back of faces using FaceForm:

In[8]:=
Graphics3D[{FaceForm[Yellow, Blue], ResourceFunction["RoundedCylinder"][]}, PlotRange -> {{-2, 2}, {-.8, 1}, {-1, 1}}]
Out[8]=

Opacity specifies the face opacity:

In[9]:=
Table[Graphics3D[{Opacity[o], ResourceFunction["RoundedCylinder"][]}], {o, {0.3, 0.5, 0.9}}]
Out[9]=

Options (1) 

RoundingRadius (1) 

Adjust how rounded the edges are:

In[10]:=
Graphics3D[
   ResourceFunction["RoundedCylinder"][{{0, 0, 0}, {5, 0, 0}}, 1, RoundingRadius -> #], Boxed -> False] & /@ {.1, .5, 1}
Out[10]=

Applications (1) 

A simple 3D bar chart:

In[11]:=
data = RandomReal[{1, 3}, {10, 2}];
In[12]:=
Graphics3D[
 MapIndexed[{Hue[(Last[#2] - 1)/4], ResourceFunction[
     "RoundedCylinder"][{Append[{1, 2} #2, 0], Append[{1, 2} #2, #1]}, .4, RoundingRadius -> .3]} &, data, {2}], Axes -> {False, False, True}, Lighting -> "Neutral"]
Out[12]=

Properties and Relations (2) 

For small rounding radii, RoundedCylinder is effectively equivalent to Cylinder:

In[13]:=
{Graphics3D[
  ResourceFunction["RoundedCylinder"][{{0, 0, 0}, {0, 0, 1}}, 1, RoundingRadius -> 0.00001]], Graphics3D[{EdgeForm[], Cylinder[{{0, 0, 0}, {0, 0, 1}}, 1]}]}
Out[13]=

When the rounding radius is set to the cylinder radius, RoundedCylinder closely resembles CapsuleShape as well as Tube with rounded caps:

In[14]:=
rad = 1;
height = 2;
{
 Graphics3D[{Blue, ResourceFunction[
    "RoundedCylinder"][{{0, 0, 0}, {0, 0, height + 2 rad}}, rad, RoundingRadius -> rad]}, PlotLabel -> "RoundedCylinder"],
 Graphics3D[{Blue, CapsuleShape[{{0, 0, 0}, {0, 0, height}}, rad]}, PlotLabel -> "CapsuleShape"],
 Graphics3D[{Blue, Tube[{{0, 0, 0}, {0, 0, height}}, rad]}, PlotLabel -> "Tube"]
 }
Out[15]=

Possible Issues (3) 

When the two endpoints coincide, no cylinder is created:

In[16]:=
Graphics3D[
 ResourceFunction["RoundedCylinder"][{{1, 0, 2}, {1, 0, 2}}]]
Out[16]=

When the rounding radius exceeds that of the cylinder, a warning is issued and artifacts appear:

In[17]:=
Graphics3D[
   ResourceFunction["RoundedCylinder"][{{0, 0, 0}, {0, 0, 3}}, .5, RoundingRadius -> #]] & /@ {0.1, 0.3, 0.5}
Out[17]=
In[18]:=
Graphics3D[
   ResourceFunction["RoundedCylinder"][{{0, 0, 0}, {0, 0, 3}}, .5, RoundingRadius -> #]] & /@ {0.7, 0.9}
Out[18]=

When the double of the rounding radius exceeds the height of the cylinder, a warning is issued and artifacts appear:

In[19]:=
Graphics3D[
   ResourceFunction["RoundedCylinder"][{{0, 0, 0}, {0, 0, 3}}, 2, RoundingRadius -> #]] & /@ {0.5, 1, 1.5}
Out[19]=
In[20]:=
Graphics3D[
   ResourceFunction["RoundedCylinder"][{{0, 0, 0}, {0, 0, 3}}, 2, RoundingRadius -> #]] & /@ {1.7, 2}
Out[20]=

Neat Examples (2) 

Random unit rounded cylinders:

In[21]:=
Graphics3D[
 Table[{EdgeForm[Opacity[.3]], Hue[RandomReal[]], ResourceFunction["RoundedCylinder"][RandomReal[10, {2, 3}], 1, RoundingRadius -> .5]}, {20}]]
Out[21]=

Bouncing cylinders:

In[22]:=
fB[t_, o_] := 0.5 Abs[Sin[2 \[Pi] t + o]]
fT[t_, o_] := 2 + 0.5 Sin[4 \[Pi] t + o + 4 ]
In[23]:=
pos = RandomReal[{-2, 2}, {5, 4}];
In[24]:=
Animate[Graphics3D[{{Hue[#4], ResourceFunction[
       "RoundedCylinder"][{{#1, #2, fB[t, #3]}, {#1, #2, fT[t, #3]}}, 0.5]} & @@@ pos, Darker@Brown, InfinitePlane[{0, 0, 0}, {{1, 0, 0}, {0, 1, 0}}]}, PlotRange -> {2.5 {-1, 1}, 2.5 {-1, 1}, {0, 3}}, Lighting -> "Neutral", Boxed -> False, ViewPoint -> {1.669, -2.713, 1.140}], {t, 0, 5}, AnimationRate -> .2, SaveDefinitions -> True]
Out[24]=

Publisher

Jaroslav Kysela

Version History

  • 1.0.0 – 20 December 2021

Related Resources

License Information