📄 mathview.cpp
字号:
// MathView.cpp : implementation of the CMathView class
//
#include "stdafx.h"
#include "Math.h"
#include "MathDoc.h"
#include "MathView.h"
#include "InputNum.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMathView
IMPLEMENT_DYNCREATE(CMathView, CView)
BEGIN_MESSAGE_MAP(CMathView, CView)
//{{AFX_MSG_MAP(CMathView)
ON_COMMAND(ID_INPUT_NUM, OnInputNum)
ON_COMMAND(ID_GET_GRAPH, OnOutputGraph)
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_SET_COLOR, OnSetColor)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMathView construction/destruction
CMathView::CMathView()
{
// TODO: add construction code here
NodeNum=1;
m_LineColor=RGB(0,0,0);
}
CMathView::~CMathView()
{
}
BOOL CMathView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMathView drawing
void CMathView::OnDraw(CDC* pDC)
{
CMathDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->MoveTo(50,320); //绘制坐标轴
pDC->LineTo(910,320); //x轴
pDC->MoveTo(900,315);
pDC->LineTo(910,320);
pDC->LineTo(900,326);
pDC->MoveTo(480,480); //y轴
pDC->LineTo(480,100);
pDC->MoveTo(475,110);
pDC->LineTo(480,100);
pDC->LineTo(485,110);
double xa=-5.0,ya;
for(;xa<5.0;xa=xa+0.05) //原始图像
{
ya=1/(1+xa*xa);
pDC->MoveTo(80*xa+480,320-80*ya);
ya=1/(1+(xa+0.05)*(xa+0.05));
pDC->LineTo(80*(xa+0.05)+480,320-80*ya);
}
pDC->TextOut(470,322,"0");
pDC->TextOut(905,325,"x");
pDC->TextOut(465,100,"y");
}
/////////////////////////////////////////////////////////////////////////////
// CMathView printing
BOOL CMathView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMathView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMathView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMathView diagnostics
#ifdef _DEBUG
void CMathView::AssertValid() const
{
CView::AssertValid();
}
void CMathView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMathDoc* CMathView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMathDoc)));
return (CMathDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMathView message handlers
void CMathView::OnInputNum() //输入节点数
{
// TODO: Add your command handler code here
CInputNum dNum;
if(dNum.DoModal()==IDOK)
{
NodeNum=dNum.m_nNum;
}
}
void CMathView::OnOutputGraph() //输出图形
{
// TODO: Add your command handler code here
CDC *pDC=GetDC();
CPen NewPen(PS_DASH,1,m_LineColor);
CPen *pOldPen=pDC->SelectObject(&NewPen);
double distance=10.0/NodeNum;
double t=-5.0;
for(int i=0;i<NodeNum;i++) //图像
{
x[i]=t;
y[i]=1/(1+t*t);
t=t+distance;
}
double wx;
int n=0;
for(double xx=-5.0;xx<5.0;xx=xx+0.05) //拉格朗日插值
{
double ya=0.0;
for(int k=0;k<NodeNum;k++)
{
double aa=1.0,bb=1.0;
for (int j=0;j<NodeNum;j++)
{
if(j!=k)
{
aa*=xx-x[j];
bb*=x[k]-x[j];
}
}
// Lx[n] +=y[k]*aa/bb;
ya +=y[k]*aa/bb;
}
// n++;
if(xx==-5.0)
pDC->MoveTo(80*xx+480,320-80*ya);
else
pDC->LineTo(80*xx+480,320-80*ya);
}
/* double xxx=-5.0;
for(int k=0;k<n;k++)
{
pDC->MoveTo(80*xxx+480,320-80*Lx[k]);
xxx +=0.05;
pDC->LineTo(80*xxx+480,320-80*Lx[k+1]);
}
*/
pDC->SelectObject(pOldPen);
// pDC->ReleaseDC();
}
void CMathView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC=GetDC();
char tbuf[80];
sprintf(tbuf,"Position:(%3d,%3d)",point.x,point.y);
pDC->TextOut(20,20,tbuf);
CView::OnMouseMove(nFlags, point);
}
void CMathView::OnSetColor() //设置颜色
{
// TODO: Add your command handler code here
CColorDialog dlg;
if(dlg.DoModal()==IDOK)
{
m_LineColor=dlg.GetColor();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -