Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Anyone aware of C/C++ code to fit a circle to a set of 2d datapoints using the linear least squares method as described by I.D.Coope?

Ron H.
Posted

You can do-it-yourself from this:

- use a 3x3 array A and a 3x1 vector B and accumulate the normal least-squares equations, using the formulas in the paper (this is an easy task);

- solve the 3x3 system using this function (this is the Cholesky method for a 3x3 symmetric positive defined matrix):

C#
bool Cholesky3x3(double* A, double* B)
{
#define A(i,j) A[i + j * 3]
    double Sum;
    double Diagonal[3];
    Sum= A(0,0);
    if (Sum <= 0.f) return false;
    Diagonal[0]= Sqrt(Sum);
    Sum= A(0,1);
    A(1,0)= Sum / Diagonal[0];
    Sum= A(0,2);
    A(2,0)= Sum / Diagonal[0];
    Sum= A(1,1) - A(1,0) * A(1,0);
    if (Sum <= 0.f) return false;
    Diagonal[1]= Sqrt(Sum);
    Sum= A(1,2) - A(1,0) * A(2,0);
    A(2,1)= Sum / Diagonal[1];
    Sum= A(2,2) - A(2,1) * A(2,1) - A(2,0) * A(2,0);
    if (Sum <= 0.f) return false;
    Diagonal[2]= Sqrt(Sum);
    Sum= B[0];
    B[0]= Sum / Diagonal[0];
    Sum= B[1] - A(1,0) * B[0];
    B[1]= Sum / Diagonal[1];
    Sum= B[2] - A(2,1) * B[1] - A(2,0) * B[0];
    B[2]= Sum / Diagonal[2];
    Sum= B[2];
    B[2]= Sum / Diagonal[2];
    Sum= B[1] - A(2,1) * B[2];
    B[1]= Sum / Diagonal[1];
    Sum= B[0] - A(1,0) * B[1] - A(2,0) * B[2];
    B[0]= Sum / Diagonal[0];
    return true;
}


Solutions in B. Compute Xc, Yc, R, you are done. Requires no library.
 
Share this answer
 
v7
Comments
Ron H. 19-Jul-11 11:08am    
Thanks! I appreciate your response!

Ron H.
YvesDaoust 19-Jul-11 16:26pm    
You can rate it then :)
YvesDaoust 20-Jul-11 4:31am    
Thx
Swatieson 1-Mar-15 18:12pm    
What are the formulas for the arrays? Apparently they are not in the Internet.
YvesDaoust 2-Mar-15 2:47am    
Lookup http://en.wikipedia.org/wiki/Linear_least_squares_(mathematics)#Derivation_of_the_normal_equations
Here's a JavaScript implementation: Least-squares circle fitting

It shouldn't be too hard to translate.
 
Share this answer
 
Comments
Ron H. 19-Jul-11 11:07am    
Thanks! I am not a Java programmer but this looks pretty straight forward!

Ron H.
Unsurprisingly, Google has lots of suggestions[^].
 
Share this answer
 
Comments
Ron H. 19-Jul-11 7:17am    
Also unsurprisingly Richard, most are irrelevant! While I didn't follow-up on all 475,000 hits ( now of course my question here is number 1) most address either "linear least squares fit" or "least squares circle fitting" ( non-linear ). Coope's method applies linear methods to the fitting of a circle to 2d data points. His solution seems to be much more tolerant of outliers thus providing a better "geometric" fit than most algebraic fits. Yes, Google is your friend and I am not sure what we did without it but sometimes a good forum of knowledgeable people provides a better result!

Ron H.
Richard MacCutchan 19-Jul-11 7:24am    
I hear what you say, but this is a technical Q&A forum and less suitable for general research questions such as yours. I appreciate that you are one of the exceptions to the rule, but we do get a lot of questions here of the form "how can I ...", where the poster has not done a basic Google search of the subject.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900