www.pudn.com > NiHe > NiHeScrollView.cpp


// NiHeScrollView.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "math.h" 
#include "NiHe.h" 
#include "NiHeScrollView.h" 
#include "NiHeDoc.h" 
#include "NiHeCishu.h" 
#include "NiHeResultD.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// NiHeScrollView 
 
IMPLEMENT_DYNCREATE(NiHeScrollView, CScrollView) 
 
NiHeScrollView::NiHeScrollView() 
{ 
} 
 
NiHeScrollView::~NiHeScrollView() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(NiHeScrollView, CScrollView) 
	//{{AFX_MSG_MAP(NiHeScrollView) 
	ON_COMMAND(ID_NIHE_PROCESS, OnNiheProcess) 
	ON_COMMAND(ID_SET_CISHU, OnSetCishu) 
	ON_COMMAND(ID_NIHERESULT, OnNiheresult) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// NiHeScrollView drawing 
 
void NiHeScrollView::OnInitialUpdate() 
{ 
	CScrollView::OnInitialUpdate(); 
 
	CSize sizeTotal; 
	// TODO: calculate the total size of this view 
	sizeTotal.cx = sizeTotal.cy = 150; 
	SetScrollSizes(MM_TEXT, sizeTotal); 
} 
 
void NiHeScrollView::OnDraw(CDC* pDC) 
{ 
//	CDocument* pDoc = GetDocument(); 
	CNiHeDoc *pDoc; 
	pDoc=GetDocument(); 
	ASSERT_VALID(pDoc); 
	char str[20];	 
	pDC->TextOut(0,290,"目前已经输入的离散点的个数为:  "); 
	sprintf(str,"%d",pDoc->count); 
	pDC->TextOut(230,290,str); 
	pDC->MoveTo(10,150);        //画x轴,先定起点(10.150) 
	pDC->LineTo(310,150);       //定x轴终点(310,150) 
	pDC->TextOut(320,150,"X"); 
	POINT pointstemp[4]={{310,150},{300,148},{300,152},{310,150}}; 
	pDC->Polyline(pointstemp,4); 
 
	pDC->MoveTo(160,280);         //画y轴,先定起点(160.280) 
	pDC->LineTo(160,20); 
	pDC->LineTo(158,30); 
	pDC->LineTo(162,30); 
	pDC->LineTo(160,20); 
	pDC->TextOut(160,10,"Y"); 
    //开始画离散点 
	//pDC->MoveTo(160+(int)pDoc->points[1].x,150-(int)pDoc->points[1].y); 
	for(int i=1;i<=pDoc->count;i++) 
	{		 
		pDC->TextOut(int(pDoc->points[i].x)+160,150-int(pDoc->points[i].y),"*"); 
	} 
 
	pDC->TextOut(0,330,"根据最小二乘法拟合的结果是:y="); 
	sprintf(str,"%.2f",pDoc->c[0]); 
	pDC->TextOut(250,330,str); 
	for(i=1;i<=pDoc->cishu;i++) 
	{ 
		sprintf(str," + %.3fx~%d ",pDoc->c[i],i); 
		pDC->TextOut(250+i*85,330,str); 
	} 
 
	int x=-151; 
	float y=0; 
	int qidian_flag=0; 
	CPen NewPen; 
	CPen *pOldPen; 
	NewPen.CreatePen(PS_SOLID,1,RGB(0,0,255)); 
	pOldPen=pDC->SelectObject(&NewPen); 
	for(i=0;i<=300;i++) 
	{ 
		y=0; 
		x+=1; 
		for(int j=0;j<=pDoc->cishu;j++) 
		{ 
			if(x==0) 
			{ 
				y=pDoc->c[0]; 
				break; 
			} 
			else 
				y+=pDoc->c[j]*pow(x,j); 
		} 
		if(qidian_flag==0&&y>=-100&&y<=145) 
		{ 
			pDC->MoveTo(x+160,150-(int)y); 
			qidian_flag=1; 
		} 
		else 
		{ 
			if(y>=140||y<=-100) 
				continue; 
			pDC->LineTo(x+160,150-(int)y); 
		} 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// NiHeScrollView diagnostics 
 
#ifdef _DEBUG 
void NiHeScrollView::AssertValid() const 
{ 
	CScrollView::AssertValid(); 
} 
 
void NiHeScrollView::Dump(CDumpContext& dc) const 
{ 
	CScrollView::Dump(dc); 
} 
 
CNiHeDoc* NiHeScrollView::GetDocument() 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNiHeDoc))); 
	return (CNiHeDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// NiHeScrollView message handlers 
 
void NiHeScrollView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	Invalidate(true);            //重画客户区	 
} 
 
void NiHeScrollView::OnNiheProcess()  
{ 
	// TODO: Add your command handler code here 
	CNiHeDoc *pDoc; 
	pDoc=GetDocument(); 
	ASSERT_VALID(pDoc); 
	float fai[100][10]; 
	float array[11][12]; 
	float x[10];          //纪录拟合多项式系数结果 
	float temp=0,si=0; 
	int m,n; 
	int i,j,k,flag; 
	m=pDoc->cishu+1;     //纪录拟合系数的个数 
	n=pDoc->count;       //纪录离散点的个数 
	for(i=0;ipoints[j+1].x,i); 
 
	for(i=0;ipoints[k+1].y*fai[i][k]; 
	} 
 
	//开始解最小二乘正规方程组,列主元LG分解法解方程 
    for(i=0;iabs(si)) 
		{        
			  si=temp; 
			  flag=k; 
		} 
	   } 
 
	    if(flag!=i) 
		{ 
		   for(j=0;j<=m;j++) 
		   { 
			temp=array[i][j]; 
			array[i][j]=array[flag][j]; 
			array[flag][j]=temp; 
		   } 
		} 
 
	   for(j=i;j<=m;j++) 
	   { 
		   temp=0; 
		   for(k=0,temp=0;k<=i-1;k++) 
			   temp+=array[i][k]*array[k][j]; 
		   array[i][j]=array[i][j]-temp; 
	   } 
	   for(j=i+1;j=0;i--) 
   { 
	   temp=0; 
	   for(j=i+1,temp=0;j<=m-1;j++) 
		   temp+=array[i][j]*x[j]; 
	   x[i]=(array[i][m]-temp)/array[i][i]; 
   } 
  
 
   for(i=m-1;i>=0;i--) 
   { 
	   pDoc->c[i]=x[i]; 
   } 
   Invalidate(true); 
} 
 
void NiHeScrollView::OnSetCishu()  
{ 
	// TODO: Add your command handler code here 
	CNiHeDoc *pDoc; 
	pDoc=GetDocument(); 
	ASSERT_VALID(pDoc); 
 
	NiHeCishu dlg; 
	if(dlg.DoModal()!=IDOK) 
		return; 
	pDoc->cishu=dlg.m_cishu; 
} 
 
void NiHeScrollView::OnNiheresult()  
{ 
	// TODO: Add your command handler code here 
	CNiHeDoc *pDoc; 
	pDoc=GetDocument(); 
	ASSERT_VALID(pDoc); 
	NiHeResultD dlg; 
	dlg.m_c0=pDoc->c[0];	 
	dlg.m_c1=pDoc->c[1];	 
	dlg.m_c2=pDoc->c[2];	 
	dlg.m_c3=pDoc->c[3];	 
	dlg.m_c4=pDoc->c[4];	 
	dlg.m_c5=pDoc->c[5];	 
	dlg.m_c6=pDoc->c[6];	 
	dlg.m_c7=pDoc->c[7];	 
	dlg.m_c8=pDoc->c[8];	 
	dlg.m_c9=pDoc->c[9];	 
	dlg.m_c10=pDoc->c[10]; 
	dlg.DoModal(); 
}