# How do i get the goodness of fit for the result of fitEllipse?

fit an ellipse and get goodness of fit

```
rRect = fitEllipse( Coords);
double angle = rRect.angle/180*Pi;
Point2f Center = rRect.center;
Size2f Sz = rRect.size;
g_GOF = 0; //Goodness Of Fit, the smaller the better
double posx, posy;
for(int i = 0; i < 32; i++){
posx = (Coords[i].x - Center.x) * cos(-angle) - (Coords[i].y- Center.y) * sin(-angle);
posy = (Coords[i].x - Center.x) * sin(-angle) + (Coords[i].y- Center.y) * cos(-angle);
g_GOF += abs( posx/Sz.width*posx/Sz.width + posy/Sz.height*posy/Sz.height - 0.25);
}
```

this works very well but comments are appreciated

I agree this works very well, but could you explain it? I understand that finding the distance from a point to an arbitrary ellipse is a transcendental problem, which is the reason for your posx and posy calculations (transforming from an arbitrary ellipse to a centered, non-rotated ellipse). Why do you use the negative angle? Where does the -0.25 factor come from? Why use only 32 points?

Thanks!

32 points is an arbitrary number, and in this case just the number of points I used in my call to fitEllipse. If each point used to otain the fit is perfectly on the ellipse then:

which follows from the definition of an ellipse if it's center is at 0,0. However, since i'm using full width and height, that is (2 x half_width)^2 and (2xhalf_height)^2, the result is 0.25 instead of 1.0.

equation 1, is defined for an ellipse with vertical and horizontal axes, whearas fitEllipse returns an ellipse that is rotated, using -angle rotates the positions back on to an ellipse with orthogonal axes.