public double LikeAEllipse(List <Vector2> points)
    {
        foreach (Vector2 p in points)
        {
            mean += p;
        }

        mean /= points.Count;
        float W = points.Count;

        float[,] C = new float[2, 2];
        foreach (Vector2 point in points)
        {
            C[0, 0] += (point.x - mean.x) * (point.x - mean.x);
            C[0, 1] += (point.x - mean.x) * (point.y - mean.y);

            C[1, 0] += (point.y - mean.y) * (point.x - mean.x);
            C[1, 1] += (point.y - mean.y) * (point.y - mean.y);
        }

        C[0, 0] /= W;
        C[0, 1] /= W;
        C[1, 0] /= W;
        C[1, 1] /= W;

        Matrix2d CM    = new Matrix2d(C);
        double   error = Mathf.Abs(W - 4 * Mathf.PI * Mathf.Sqrt(CM.Det()));

        error /= W;
        //Debug.Log("Like a ellipse error: " + error); //10^-2 may be a good threshold

        return(error);
    }
        public double ErrorScore(List <MyVector2> points)
        {
            foreach (MyVector2 p in points)
            {
                mean += p;
            }

            mean /= points.Count;
            double W = points.Count;


            double[,] C = new double[2, 2];
            foreach (MyVector2 point in points)
            {
                C[0, 0] += (point.x - mean.x) * (point.x - mean.x);
                C[0, 1] += (point.x - mean.x) * (point.y - mean.y);

                C[1, 0] += (point.y - mean.y) * (point.x - mean.x);
                C[1, 1] += (point.y - mean.y) * (point.y - mean.y);
            }

            C[0, 0] /= W;
            C[0, 1] /= W;
            C[1, 0] /= W;
            C[1, 1] /= W;

            Matrix2d CM  = new Matrix2d(C);
            SVD      svd = new SVD(C);
            //svd.w - eigen value, start from 1
            //svd.u - eigen vector, start from 1

            int max = 1, min = 2;

            if (svd.w[max] < svd.w[min])
            {
                int temp = max;
                max = min;
                min = temp;
            }

            double    major     = 2 * Math.Sqrt(svd.w[max]);
            MyVector2 majoraxis = new MyVector2(svd.u[1, max], svd.u[2, max]);

            majoraxis = majoraxis.Normalize();
            double    minor     = 2 * Math.Sqrt(svd.w[min]);
            MyVector2 minoraxis = new MyVector2(svd.u[1, min], svd.u[2, min]);

            minoraxis = minoraxis.Normalize();

            majorendp   = mean + majoraxis * major;
            majorstartp = mean - majoraxis * major;
            minorendp   = mean + minoraxis * minor;
            minorstartp = mean - minoraxis * minor;

            double error = Math.Abs(W - 4 * Math.PI * Math.Sqrt(CM.Det()));

            error /= W;
            Console.WriteLine("Like a ellipse error: {0}", error); //10^-2 may be a good threshold

            return(error);
        }
    public double FitEllipse()
    {
        List <Vector2> points = IExtension.GetMaskPoints(img);

        foreach (Vector2 p in points)
        {
            mean += p;
        }

        mean /= points.Count;
        float W = points.Count;


        float[,] C = new float[2, 2];
        foreach (Vector2 point in points)
        {
            C[0, 0] += (point.x - mean.x) * (point.x - mean.x);
            C[0, 1] += (point.x - mean.x) * (point.y - mean.y);

            C[1, 0] += (point.y - mean.y) * (point.x - mean.x);
            C[1, 1] += (point.y - mean.y) * (point.y - mean.y);
        }

        C[0, 0] /= W;
        C[0, 1] /= W;
        C[1, 0] /= W;
        C[1, 1] /= W;

        Matrix2d CM  = new Matrix2d(C);
        SVD      svd = new SVD(C);
        //svd.w - eigenvalue, start from 1
        //svd.u - eigenvector, start from 1

        int max = 1, min = 2;

        if (svd.w[max] < svd.w[min])
        {
            int temp = max;
            max = min;
            min = temp;
        }

        float   major     = 2 * Mathf.Sqrt(svd.w[max]);
        Vector2 majoraxis = new Vector2(svd.u[1, max], svd.u[2, max]);

        majoraxis.Normalize();
        float   minor     = 2 * Mathf.Sqrt(svd.w[min]);
        Vector2 minoraxis = new Vector2(svd.u[1, min], svd.u[2, min]);

        minoraxis.Normalize();

        majorendp   = mean + majoraxis * major;
        majorstartp = mean - majoraxis * major;
        minorendp   = mean + minoraxis * minor;
        minorstartp = mean - minoraxis * minor;

        double error = Mathf.Abs(W - 4 * Mathf.PI * Mathf.Sqrt(CM.Det()));

        error /= W;
        if (!m_engine.m_is_quiet)
        {
            Debug.Log("Like a ellipse error: " + error);                       //10^-2 may be a good threshold
        }
        return(error);
    }