Function Repository Resource:

CircleFit

Source Notebook

Find the best-fit circle for a set of points

Contributed by: Sander Huisman

ResourceFunction["CircleFit"][pts]

returns the best-fit circle for the points pts.

ResourceFunction["CircleFit"][wpts]

returns the best-fit circle for the points pts having the weights w.

ResourceFunction["CircleFit"][wpts,"Association"]

returns an association with the center, the radius, the Circle, a pure function etc.

Details

ResourceFunction["CircleFit"] returns a Circle object or an Association.
For 1 point, a circle at that point with 0 radius is returned.
For 2 points, a circle at the midpoint with a radius equal to half the distance between the points is returned.
For 3 points that are not collinear, an exact solution will be returned.
For 4 or more points pts, ResourceFunction["CircleFit"] returns the circle centered at {x,y} with radius r that minimizes .
For 4 or more points pts and weights w, ResourceFunction["CircleFit"] returns the circle centered at {x,y} with radius r that minimizes .

Examples

Basic Examples (2) 

Find the circle through 4 points and visualize the result:

In[1]:=
pts = {{1, 2}, {2, 1}, {3, 4}, {3.5, 1.5}};
circ = ResourceFunction["CircleFit"][pts];
Graphics[{PointSize[Large], Point[pts], Red, circ}, Frame -> True]
Out[1]=

Find the best-fitting circle for a 2500 points:

In[2]:=
SeedRandom[1234];
n = 2500;
pts = MapThread[
   AngleVector[{#2, #1}] &, {RandomReal[{0, 2 Pi}, n], RandomVariate[NormalDistribution[20, 2], n]}];
circ = ResourceFunction["CircleFit"][pts];
Graphics[{Point[pts], Red, Thick, circ}, Frame -> True]
Out[2]=

Scope (2) 

Specify weights:

In[3]:=
pts = {{11, 2.2}, {12, 2}, {13, 4}, {14, 2}};
weights = {10, 0.2, 1, 1};
circ = ResourceFunction["CircleFit"][weights -> pts];
ListPlot[MapThread[Callout, {pts, weights}], Prolog -> {Red, circ}, AspectRatio -> Automatic, Frame -> True]
Out[3]=

Return an Association with all the details:

In[4]:=
pts = {{11, 2.2}, {12, 2}, {13, 4}, {14, 2}};
weights = {10, 0.2, 1, 1};
Dataset[ResourceFunction["CircleFit"][weights -> pts, "Association"]]
Out[4]=

Properties and Relations (1) 

For 2 points, the center of the circle is the midpoints of the points:

In[5]:=
pts = {{10, 5}, {8, 7}};
circ = ResourceFunction["CircleFit"][pts];
Graphics[{PointSize[Large], Point[pts], Red, circ}, Frame -> True]
Out[5]=

Possible Issues (3) 

When no points are given, a Failure object is returned:

In[6]:=
ResourceFunction["CircleFit"][{}]
Out[6]=

When 3 points are given and they are collinear, a Failure object is returned:

In[7]:=
ResourceFunction["CircleFit"][{{1, 1}, {2, 2}, {3, 3}}]
Out[7]=

The case of multiple collinear points results in circle that is far away with a large radius:

In[8]:=
SeedRandom[1234];
pts = Table[{0, -1} + t {2, 3}, {t, RandomVariate[NormalDistribution[], 5]}];
cc = ResourceFunction["CircleFit"][pts]
Out[8]=

Neat Examples (1) 

Find the best-fitting circle for points clustered around a circular arc:

In[9]:=
SeedRandom[1234];
n = 1000;
pts = MapThread[
   AngleVector[{10^2, 10^3}, {#2, #1}] &, {RandomReal[{0, Pi/3}, n], RandomVariate[NormalDistribution[400, 20], n]}];
circ = ResourceFunction["CircleFit"][pts];
Graphics[{Point[pts], Red, circ}, Frame -> True]
Out[9]=

Publisher

SHuisman

Version History

  • 1.1.0 – 17 May 2021

Related Resources

Author Notes

First an estimate is found by minimizing . After that the real problem is minimized using the estimate as a starting points, .

License Information