⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drafting.cpp

📁 一个最小二乘法的拟合数据的小程序
💻 CPP
字号:
// Drafting.cpp : implementation file
//

#include "stdafx.h"
#include "zerchfa.h"
#include "Drafting.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDrafting dialog


CDrafting::CDrafting(CWnd* pParent /*=NULL*/)
	: CDialog(CDrafting::IDD, pParent)
{

	or1=0;
	or2=0;
	or3=0;
	//{{AFX_DATA_INIT(CDrafting)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


////////////////////////带点指针,函数系数指针的构造函数
CDrafting::CDrafting(double* point_x,double* point_y,double* funcof,
					 int degree,int nodenum)
{
	p_x=point_x;
	p_y=point_y;
	f_c=funcof;
	ddegree=degree;
	nnodenum=nodenum;
}


long double CDrafting::f(double x)
{
	if (ddegree==4||ddegree==3) 
	{
		if (ddegree==4) 
			return f_c[4]*x*x*x*x+f_c[3]*x*x*x+f_c[2]*x*x+f_c[1]*x+f_c[0];
		else
			return f_c[3]*x*x*x+f_c[2]*x*x+f_c[1]*x+f_c[0];		
	}
	else if(ddegree==2)
	{
		return f_c[2]*x*x+f_c[1]*x+f_c[0];
	}
	else if (ddegree==1)
	{
		return  f_c[1]*x+f_c[0];
	}
	else
		return 0;
}

void CDrafting::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDrafting)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP

}



BEGIN_MESSAGE_MAP(CDrafting, CDialog)
	//{{AFX_MSG_MAP(CDrafting)
	ON_BN_CLICKED(IDC_RETURN, OnReturn)
	ON_BN_CLICKED(IDC_POINTS, OnPoints)
	ON_BN_CLICKED(IDC_ZOOM, OnZoom)
	ON_WM_MOUSEWHEEL()
	ON_BN_CLICKED(IDC_CURVE, OnCurve)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDrafting message handlers

void CDrafting::OnReturn() 
{
	or2=0;
	or1=0;
	or3=0;
	CDialog::OnOK();
	
}

void CDrafting::OnPoints() 
{
	CWnd *pWnd=GetDlgItem(IDC_TU);	
	CDC *pDC=pWnd->GetDC();
    pDC->SetMapMode(MM_LOMETRIC);
	pDC->SelectStockObject(LTGRAY_BRUSH);	
	pDC->SetROP2(R2_NOT);
	if(!(or3||or1))//如果坐标轴还没有绘制,则绘之!
	{
		pDC->LineTo(0,1100);
		pDC->MoveTo(0,0);
		pDC->LineTo(1400,0);
	}


// 	pDC->DrawText("sdjfskdj",NULL,NULL);
	///////描圆形点
	for(int i=0;i<nnodenum;i++)
	{
	   pDC->Ellipse((int)p_x[i]-8,(int)p_y[i]-8,(int)p_x[i]+8,(int)p_y[i]+8);
	}
	
	pWnd->ReleaseDC(pDC);

	or1=TRUE;

	
}


void CDrafting::OnZoom() /////具体实现见下OnMouseWheel函数!!
{
	if (or1||or3)
	{
		or2=1;
	}
	 
}


void CDrafting::OnCurve() 
{
	CWnd *pWnd=GetDlgItem(IDC_TU);	
	CDC *pDC=pWnd->GetDC();    
    pDC->SetMapMode(MM_LOMETRIC);
	pDC->SelectStockObject(LTGRAY_BRUSH);	
	pDC->SetROP2(R2_NOT);
	if(!(or3||or1))//如果坐标轴还没有绘制,则绘之!
	{
		pDC->LineTo(0,1100);
		pDC->MoveTo(0,0);
		pDC->LineTo(1400,0);
	}

	if (!or3) 
	{
	
		double x=0;
		double x0=0.1;//////步长以及放大系数都是暂定的!!!!!!!!!!!

		for(int i=0;i<1000;i++)
		{			
			x=x+x0;
			pDC->LineTo((int)x*10,(int)f(x)*10);
			// 	pDC->SetPixelV((int)x*100,(int)f(x)*10,COLORREF(RGB(255,0,0)));			
		}
	}

	pWnd->ReleaseDC(pDC);

	
	or3=TRUE;
	
}



//////////////////////////////////////////////////////////////////////////

/////缩放功能有待改进!!!
BOOL CDrafting::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 

{
	if(or2)
	{
		CWnd *pWnd=GetDlgItem(IDC_TU);	
		CDC *pDC=pWnd->GetDC();    
		pDC->SetMapMode(MM_LOMETRIC);
		pDC->SelectStockObject(LTGRAY_BRUSH);	
		pDC->SetROP2(R2_NOT);

		///////描圆形点



		//////绘拟合曲线图

		
		pWnd->ReleaseDC(pDC);
	}
	
	return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -