Function Repository Resource:

LassoPoints

Source Notebook

Create Graphics primitives of a closed curve that encloses a set of points

Contributed by: Jon McLoone

ResourceFunction["LassoPoints"][pts]

return Graphics primitives for a curve that encloses the given 2D points.

Details and Options

ResourceFunction["LassoPoints"] generates the convex hull of the given points and then uses these as the key points for constructing the curve out of Line and Circle primitives.
The function accepts the option Padding, with the default around points being Scaled[.2].

Examples

Basic Examples (2) 

In[1]:=
lasso = ResourceFunction["LassoPoints"][{{0, 0}, {1, 0}, {0, 1}}]
Out[1]=
In[2]:=
ListPlot[{{0, 0}, {1, 0}, {0, 1}}, Epilog -> lasso, Sequence[
 PlotRange -> {{-0.2, 1.2}, {-0.2, 1.2}}, Axes -> False]]
Out[2]=

Lasso the seven points nearest the mean of some data:

In[3]:=
SeedRandom[1];
data = RandomReal[1, {20, 2}];
pointsOfInterest = Nearest[data, Mean[data], 7];
ListPlot[data, Epilog -> ResourceFunction["LassoPoints"][pointsOfInterest]]
Out[6]=

Scope (1) 

Lassoing a single point creates a circle:

In[7]:=
lasso = ResourceFunction["LassoPoints"][{{0, 0}}]
Out[7]=
In[8]:=
Graphics[{lasso, Point[{0, 0}]}]
Out[8]=

Options (3) 

Control how closely the curve encloses the points with Padding:

In[9]:=
SeedRandom[1];
data = RandomReal[1, {20, 2}];
pointsOfInterest = Nearest[data, Mean[data], 7]; ListPlot[data, Epilog -> {Green, Point /@ pointsOfInterest, ResourceFunction["LassoPoints"][pointsOfInterest, Padding -> 0]}]
Out[10]=

Scaled values for Padding represent a proportion of the distance from the centroid of the enclosed area:

In[11]:=
ListPlot[data, Epilog -> {Green, Point /@ pointsOfInterest, ResourceFunction["LassoPoints"][pointsOfInterest, Padding -> Scaled[0.5]]}]
Out[11]=

Absolute values are applied using the coordinate system of the points:

In[12]:=
ListPlot[data, Epilog -> {Green, Point /@ pointsOfInterest, ResourceFunction["LassoPoints"][pointsOfInterest, Padding -> 0.2]}]
Out[12]=

Publisher

Jon McLoone

Version History

  • 1.0.0 – 30 January 2023

Related Resources

License Information