示例#1
0
    VectorD PrincipalOrientationCenter(out PointD center)
    // The principle orientation is one of the eigenvectors of the covariance matrix
        {
        double meanX, meanY;
        double[,] covariance = CovarianceMatrix(out meanX, out meanY);
        center = new PointD(meanX, meanY);
        //
        double a = covariance[0,0], b = covariance[0,1];
        double c = covariance[1,0], d = covariance[1,1];
        //
        double r = Math.Sqrt(a*a + 4*b*c - 2*a*d + d*d);
        double n = (a - d + r); Debug.Assert(n >= 0);
        double x = n / (2 * c);
        //
        if (!x.IsFinite())
            {
            // Y does not vary with X: all X's look the same so far as Y is concerned
            //
            double width  = this.Vertices.Max(v => v.Location.X) - this.Vertices.Min(v => v.Location.X);
            double height = this.Vertices.Max(v => v.Location.Y) - this.Vertices.Min(v => v.Location.Y);

            if (width > height)
                return new VectorD(1, 0);
            else
                return new VectorD(0, 1);
            }
        else
            {
            VectorD eigenVector = new VectorD(x, 1) * Math.Sign(c);
            return eigenVector.Unit();
            }
        }