Function Repository Resource:

PolygonOffsets

Source Notebook

Return two polylines a fixed distance from a polyline

Contributed by: Ed Pegg Jr

ResourceFunction["PolygonOffsets"][polyline,distance]

creates two polylines at distance from polyline.

Details

The polyline can be either an ordered list of points or a Polygon.
A polygon can be considered a closed polyline. A triangle might be {a,b,c}. A closed polyline would be {a,b,c,a}. OffsetClosedPolyline will treat both of these as a triangle.
Inside and outside doesn't apply to a figure-8 polyline, so these terms are not used.

Examples

Basic Examples (2) 

Create offsets to a triangle:

In[1]:=
pol = {{0, 0}, {8, 0}, {0, 6}};
off = ResourceFunction["PolygonOffsets"][pol, 1]
Out[2]=

The "closed polyline" version has an identical result:

In[3]:=
pol = {{0, 0}, {8, 0}, {0, 6}, {0, 0}};
off = ResourceFunction["PolygonOffsets"][pol, 1]
Out[4]=

Show the original polyline and the generated polylines:

In[5]:=
Graphics[{Thick,
  Green, Line[pol],
  Red, Line[First[off]],
  Blue, Line[Last[off]]}]
Out[5]=

Show a more complicated offset:

In[6]:=
pol = {{0, 2}, {0, 8}, {2, 8}, {7, 6}, {8, 8}, {8, 9}, {0, 2}};
off = ResourceFunction["PolygonOffsets"][pol, 1/3];
Graphics[{Thick, Green, Line[pol], Blue, Line[Last[off]], Red, Line[First[off]]}]
Out[8]=

Scope (2) 

Use a Polygon:

In[9]:=
poly = Polygon[{{-2, 0}, {-1, 3/2}, {1, 3/2}, {2, 0}, {1, -3/2}, {-1, -3/2}}];
offsets = ResourceFunction["PolygonOffsets"][poly, .5]
Out[10]=

See the offset polygons:

In[11]:=
Graphics[{Opacity[.2], Polygon /@ offsets, poly}]
Out[11]=

Applications (1) 

Create an op-art effect:

In[12]:=
pol = {{0, 0}, {8, 1}, {1, 4}, {7, 3}};
Graphics[
 Line /@ Table[
   ResourceFunction["PolygonOffsets"][pol, k], {k, .1, .5, .05}]]
Out[13]=

Possible Issues (1) 

If the offset is large, the results can become meaningless:

In[14]:=
pol = {{0, 2}, {0, 8}, {2, 8}, {7, 6}, {8, 8}, {8, 9}, {0, 2}};
off = ResourceFunction["PolygonOffsets"][pol, 2];
Graphics[{Thick, Green, Line[pol], Blue, Line[Last[off]], Red, Line[First[off]]}]
Out[16]=

Neat Examples (1) 

Round the corners using the resource function RoundedLine. Note that the beginning point on the bottom left is not rounded:

In[17]:=
pol = {{0, 1}, {2, 0}, {1, 1}, {3, 2}, {2, 3}, {1, 2}, {3, 1}, {1, 0}, {2, 1}, {0, 2}, {1, 3}, {2, 2}, {0, 1}};
off = ResourceFunction["PolygonOffsets"][pol, .04];
Graphics[{Thick, {Hue[Random[]], ResourceFunction["RoundedLine"][#, .07]} & /@ Append[off, pol]}]
Out[19]=

Version History

  • 1.0.0 – 23 February 2026

Related Resources

License Information