Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 981803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:36:01+00:00 2026-05-16T04:36:01+00:00

I would appreciate an extra set of eyes on this code. It’s supposed to

  • 0

I would appreciate an extra set of eyes on this code. It’s supposed to find a best-fit ellipse for a set of data points. The problem is that the length of major & minor axis (aDist and bDist) are coming out larger than they should.

Inputs:

  • points – a set of (x,y) coordinates for the data points; x and y are non-negative
  • avgX, avgY – the average of the x and y coordinates of all data points

Outputs:

  • aDist, bDist – the lengths of the major and minor axes
        // Find a and b -- use principal component analysis
    // http://ask.metafilter.com/36213/Best-Fit-Ellipse (2nd reply)
    // http://number-none.com/product/My%20Friend,%20the%20Covariance%20Body/index.html

    double mat[2][2];   // Will be the covariance matrix. 
                        // Eigenvectors will be major & minor axes. Eigenvalues will be lengths of axes, squared. 

    mat[0][0] = mat[0][1] = mat[1][0] = mat[1][1] = 0; 
    for (CPixelList::iterator i = points->begin(); i != points->end(); i++) 
    {
        // Add  [ x - avgX, y - avgY ] * [ x - avgX ]  to mat
        //                               [ y - avgY ]
        double diffX = i->x - avgX; 
        double diffY = i->y - avgY; 
        mat[0][0] += diffX * diffX; 
        mat[0][1] += diffX * diffY; 
        mat[1][1] += diffY * diffY; 
    }
    mat[1][0] = mat[0][1]; 

    // http://www.math.harvard.edu/archive/21b_fall_04/exhibits/2dmatrices/index.html
    double T = mat[0][0] + mat[1][1];                           // Trace
    double D = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];   // Determinant
    double L1 = T/2 + sqrt(T*T/4 - D);      // Eigenvalues
    double L2 = T/2 - sqrt(T*T/4 - D);      // 
    aDist = sqrt(L1); 
    bDist = sqrt(L2); 

I have checked the inputs in the debugger, and they look OK. I have tried this code for some simple shapes (circles, ellipses, rectangles) with no rotation, and aDist and bDist are proportional to the shape but always too large. For example, if ‘points’ is a 100×100 circle, then aDist and bDist are 582.

Update: After summing up mat, I now divide each element by points->size(), as Mike suggested. If points is the square <(0,0),(10,0),(10,10),(0,10)>, then aDist and bDist are now 5, as expected which is too small. As more pixels are added to that square, aDist and bDist get smaller. For example, <(0,0),(5,0),(10,0),(10,5),(10,10),(5,10),(0,10),(0,5)> gives a radius of sqrt(18.75)=4.33.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:36:01+00:00Added an answer on May 16, 2026 at 4:36 am

    You need to divide mat by the total number of points to get the correct covariance matrix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would appreciate any help on this issue. Lets say I want to load
This is about my assignment so I would appreciate generic answers with explanation. I
I have an interesting problem and would appreciate your thoughts for the best solution.
Edit: Seems numerous people think this is a dumb idea, so I would appreciate
Django documents give this example of associating extra data with a M2M relationship. Although
How would I set up the URL routing for a scenario such as this:
I would appreciate some guidance on modelling services and operations in WCF. I have
I would appreciate some help with something I working on and have not done
I would appreciate some help with an UPDATE statement. I want to update tblOrderHead
I would appreciate some help on creating the proper SQL to retrieve only one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.