This project has moved. For the latest updates, please go here.

VB example for circle fitting

Aug 26, 2011 at 8:46 PM

Hi,

I'm quite happy to find a library like this, providing a nice pack of functionality.

As I'm working in VB2008 and the examples are C++ or C# it is a bit troublesome to find the right entrance. May I ask for an example how to use UncertainMeasurementSample.FitToFunction properly in order to find a circle (X0, Y0, R) which fits to a number of X,Y points?

It would be very helpful, thank you in advance!

Werner

Coordinator
Jul 14, 2012 at 6:44 PM

Hi Werner,

There isn't a built-in function to do this, but it easy to do by defining a metric and adjusting the circle parameters to minimize it using FunctionMath.FindMinimum. Here is some example code:

 ' Define our test points
    ' These are some points on the circle with center (1, 3) and radius 5
    Dim x = New Double() {1, 4, 5, 6, 5}
    Dim y = New Double() {8, 7, 6, 3, 0}

    ' Computes the total squared distance of our test points
    ' from the circle with center at a(0), a(1) and radius a(2)
    Function TotalSquaredDistance(a As Double()) As Double
        Dim s As Double = 0.0
        For i As Integer = 0 To 4
            Dim dx As Double = x(i) - a(0)
            Dim dy As Double = y(i) - a(1)
            Dim d As Double = MoreMath.Hypot(dx, dy)
            s = s + MoreMath.Pow(d - a(2), 2)
        Next
        Return s
    End Function

    Sub FitToCircle()
        ' Make an initial guess at center and radius parameters
        Dim a = New Double() {0, 1, 2}
        ' Adjust parameters to minimize the total squared distance function
        Dim result As SpaceExtremum = FunctionMath.FindMinimum(AddressOf TotalSquaredDistance, a)
        ' Report the parameters that mimimize the function; these are our best-fit parameters
        Dim b As Double() = result.Location
        Console.WriteLine("(x, y) = ({0}, {1}) R = {2}", b(0), b(1), b(2))
    End Sub

BY the way, by differentiating the metric function wrt R, you can find an analytic expression for the best-fit R in terms of just the center parameters and the data. That allows you to further reduce a 3-parameter minimization problem to a 2-parameter minimization problem.

Jul 16, 2013 at 1:53 PM
Edited Jul 16, 2013 at 2:35 PM
Hi,

great, hat's exactly what I am looking for.
Unfortunately I need to use c# and have some trouble expressing
"Function TotalSquaredDistance(a As Double()) As Double"
using Linq in order to pass it to FunctionMath.FindMinimum() Method.
I couldnt find any Examples concernig that.
May I please ask for a hint how to translate the example to c# or where I can find a c# example using FindMinimum()?

Thanks and best regards,
Martin
Jul 17, 2013 at 9:09 AM
To answer my own question (hope it may be useful to others):
         //  some points to fit  
         double[,] sampleData = { { 1, 8 }, { 4, 7 }, { 5, 6 }, { 6, 3 }, { 5, 0 } };

         //    ' Make an initial guess at center and radius parameters
         var initialGuess = new double[] { 0, 1, 2 };

        FitToCircle(sampleData, initialGuess);

        private static void FitToCircle(double[,] xy,  double[] initialGuess)
        {
            var circle = FunctionMath.FindMinimum(
                (double[] a) =>
                {
                    Double s = 0.0;
                    int length = xy.GetLength(0); 
                    for (int i = 0; i < length; i++)
                    {
                        Double dx = xy[i, 0] - a[0];
                        Double dy = xy[i, 1] - a[1];
                        Double d = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
                        s = s + Math.Pow(d - a[2], 2);
                    }
                    return s;
                }, initialGuess
            );
       }