//最小二乘法直线拟合
BOOL CalculateLineKB(CFoldPointList *m_FoldList,double &k,double &b)
{
//最小二乘法直线拟合
//m_FoldList为关键点(x,y)的链表
//拟合直线方程(Y=kX+b)
if(m_FoldList==NULL)return FALSE;
long lCount=m_FoldList->GetCount();
if(lCount<2)return FALSE;
CFoldPoint *pFold;
double mX,mY,mXX,mXY,n;
mX=mY=mXX=mXY=0;
n=lCount;
POSITION pos=m_FoldList->GetHeadPosition();
while(pos != NULL)
{
pFold=m_FoldList->GetNext(pos);
mX+=pFold->X;
mY+=pFold->Y;
mXX+=pFold->X*pFold->X;
mXY+=pFold->X*pFold->Y;
}
if(mX*mX-mXX*n==0)return FALSE;
k=(mY*mX-mXY*n)/(mX*mX-mXX*n);
b=(mY-mX*k)/n;
return TRUE;
}