How to fit the 2D scatter data with a line with C++

前端 未结 7 1587
春和景丽
春和景丽 2020-12-16 23:19

I used to work with MATLAB, and for the question I raised I can use p = polyfit(x,y,1) to estimate the best fit line for the scatter data in a plate. I was wondering which r

相关标签:
7条回答
  • 2020-12-17 00:17

    Equation of line is Ax + By + C=0.

    So it can be easily( when B is not so close to zero ) convert to y = (-A/B)*x + (-C/B)

    typedef double scalar_type;
    typedef std::array< scalar_type, 2 > point_type;
    typedef std::vector< point_type > cloud_type;
    
    bool fit( scalar_type & A, scalar_type & B, scalar_type & C, cloud_type const& cloud )
    {
        if( cloud.size() < 2 ){ return false; }
    
        scalar_type X=0, Y=0, XY=0, X2=0, Y2=0;
    
        for( auto const& point: cloud )
        { // Do all calculation symmetric regarding X and Y
            X  += point[0];
            Y  += point[1];
            XY += point[0] * point[1];
            X2 += point[0] * point[0];
            Y2 += point[1] * point[1];
        }
    
        X  /= cloud.size();
        Y  /= cloud.size();
        XY /= cloud.size();
        X2 /= cloud.size();
        Y2 /= cloud.size();
    
        A = - ( XY - X * Y ); //!< Common for both solution
    
        scalar_type Bx = X2 - X * X;
        scalar_type By = Y2 - Y * Y;
    
        if( fabs( Bx ) < fabs( By ) ) //!< Test verticality/horizontality
        { // Line is more Vertical.
            B = By;
            std::swap(A,B);
        }
        else
        {   // Line is more Horizontal.
            // Classical solution, when we expect more horizontal-like line
            B = Bx;
        }
        C = - ( A * X + B * Y );
    
        //Optional normalization:
        // scalar_type  D = sqrt( A*A + B*B );
        // A /= D;
        // B /= D;
        // C /= D;
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题