Function Repository Resource:

RoundedPolygon

Source Notebook

Represent a polygon with rounded corners

Contributed by: Jan Mangaldan

ResourceFunction["RoundedPolygon"][{p1,,pn},r]

represents a filled rounded polygon with points pi and rounding radius r.

ResourceFunction["RoundedPolygon"][{p1,,pn},{r1,,rn}]

represents a filled rounded polygon with points pi and corresponding rounding radii ri.

Details

ResourceFunction["RoundedPolygon"] returns a Polygon object, and can be used as a graphics primitive.
ResourceFunction["RoundedPolygon"][poly,rspec], where poly is a Polygon, Rectangle or Triangle object, gives a rounded version of poly.
The rounding radii ri must be nonnegative numbers.
ResourceFunction["RoundedPolygon"] works with 2D and 3D polygons.

Examples

Basic Examples (2) 

A triangle with rounded corners:

In[1]:=
Graphics[{Pink, ResourceFunction["RoundedPolygon"][{{0., 0.}, {1., 0.}, {0., 1.}}, 0.1]}]
Out[1]=

A rounded rectangle with different rounding radii for each corner:

In[2]:=
Graphics[{Pink, ResourceFunction["RoundedPolygon"][
   Rectangle[{0, 0}, {2, 1}], {0.2, 0.1, 0.2, 0.1}]}]
Out[2]=

Scope (4) 

Coordinates for a star-shaped polygon:

In[3]:=
star = N[Riffle[CirclePoints[{1, \[Pi]/10}, 5], RotateLeft@CirclePoints[{4 Sin[\[Pi]/10]^2, -\[Pi]/10}, 5]]];

Show the original polygon and the rounded version:

In[4]:=
Graphics[{{Blue, Polygon[star]}, {Yellow, ResourceFunction["RoundedPolygon"][Polygon[star], 0.1]}}]
Out[4]=

Use different rounding radii for each vertex:

In[5]:=
Graphics[{{Blue, Polygon[star]}, {Yellow, ResourceFunction["RoundedPolygon"][Polygon[star], PadRight[{}, 10, {1/20, 1/8}]]}}]
Out[5]=

A rounded 3D polygon:

In[6]:=
Graphics3D[{FaceForm[Blue, Red], ResourceFunction["RoundedPolygon"][
   Polygon[PadRight[star, {Automatic, 3}]], PadRight[{}, 10, {1/20, 1/8}]]}]
Out[6]=

Applications (2) 

Plot a function over a rounded polygon domain:

In[7]:=
Plot3D[Sin[6 x + Sin[6 y]]/6, {x, y} \[Element] ResourceFunction["RoundedPolygon"][N[CirclePoints[6]], 1/3], BoxRatios -> Automatic]
Out[7]=

A polyhedron with rounded faces:

In[8]:=
facs = N[Flatten[Normal[PolyhedronData["DuerersSolid", "Faces"]]]];
In[9]:=
Graphics3D[{LightBlue, ResourceFunction["RoundedPolygon"][#, 2/5] & /@ facs}]
Out[9]=

Properties and Relations (2) 

RoundedPolygon returns a Polygon object:

In[10]:=
ResourceFunction["RoundedPolygon"][{{0., 0.}, {1., 0.}, {0., 1.}}, 0.1] // Head
Out[10]=

Applying RoundedPolygon to a Rectangle is equivalent to setting its RoundingRadius:

In[11]:=
{Graphics[
  ResourceFunction["RoundedPolygon"][Rectangle[{0., 0.}, {4., 2.}], 1/2], Frame -> True], Graphics[Rectangle[{0., 0.}, {4., 2.}, RoundingRadius -> 1/2], Frame -> True]}
Out[11]=

Possible Issues (2) 

If the rounding radius is too large, RoundedPolygon may give unexpected results:

In[12]:=
Graphics[{Brown, ResourceFunction[
   "RoundedPolygon"][{{0., 0.}, {2., 0.}, {1.5, 1.}, {0.5, 1.}}, 0.6]}]
Out[12]=

Use a smaller rounding radius:

In[13]:=
Graphics[{Brown, ResourceFunction[
   "RoundedPolygon"][{{0., 0.}, {2., 0.}, {1.5, 1.}, {0.5, 1.}}, 0.3]}]
Out[13]=

Neat Examples (2) 

Rounded Voronoi cells:

In[14]:=
pts = RandomReal[{-2, 2}, {50, 2}];
Graphics[{Directive[ColorData[61, RandomInteger[{1, 9}]], EdgeForm[Gray]], ResourceFunction["RoundedPolygon"][#, 1/9]} & /@
   MeshPrimitives[VoronoiMesh[pts], 2]]
Out[14]=

Use RoundedPolygon with the resource function PerforatePolygons on a truncated icosahedron:

In[15]:=
tric = N[Flatten[
    Normal[PolyhedronData["TruncatedIcosahedron", "Faces"]]]];
In[16]:=
Graphics3D[{Gray, FaceForm[LightBlue], ResourceFunction["PerforatePolygons"][
   ResourceFunction["RoundedPolygon"][#, 2/5] & /@ tric]}, Boxed -> False]
Out[16]=

Use RoundedPolygon with the resource function OutlinePolygons on a truncated icosahedron:

In[17]:=
Graphics3D[{FaceForm[LightBlue], EdgeForm[], ResourceFunction["OutlinePolygons"][
   ResourceFunction["RoundedPolygon"][#, 2/5] & /@ tric]}, Boxed -> False]
Out[17]=

Version History

  • 1.0.1 – 16 March 2021
  • 1.0.0 – 08 February 2021

Related Resources

Author Notes

Polygons with holes are currently not supported.

License Information