VC6.0 MFC simulation spring motion (improved version)
1. Content description
运用VC6.0新建工程MFC AppWizard(exe),创建单文档应用程序,画一个弹簧(用矩形代替),下面挂有重物(用圆代替),设定重物质量和弹簧的弹性系数,模拟弹簧运动,可通过设置对话框来改变弹簧的一些基本参数,并实时绘制运动轨迹曲线图。
**源码链接:https://download.csdn.net/download/qq_46649692/16587973**
2. The final realization effect diagram
3. Implementation steps (detailed tutorial)
1. Create a new project and name it "SpringSimulation".
2. Select "Single Document (S)", click "Finish" and "OK".
3. Create a new class
4. Add member variables
5. Initialize variables in the constructor
code show as below
CTanHuang::CTanHuang()
{
m_YD.x = 200;
m_YD.y = 50;
m_kx = 200/0.2;
m_ky = -200/0.2;
m_K = 10.0;
m_L = 0.2;
m_l = 0.2;
m_m = 0.1;
m_Nv = 0;
m_Nl = 0;
m_v = 0.0;
}
6. Add member function Draw(CDC *p)
code show as below
void CTanHuang::Draw(CDC *p)
{
pDC = p;
int i;
int x,y,x1,y1;
int r = 20;
//画最上面悬挂弹簧的线
x = m_YD.x - 50;
y = m_YD.y;
pDC->MoveTo(x,y);
x = m_YD.x + 50;
pDC->LineTo(x,y);
x = m_YD.x;
y = m_YD.y;
pDC->MoveTo(x,y);
y = m_YD.y + 30;
pDC->LineTo(x,y);
//画弹簧
x = m_YD.x - 20;
x1 = m_YD.x + 20;
y1 = y - m_l * m_ky;
pDC->Rectangle(x,y,x1,y1);
//画弹簧与重物之间的线
x = m_YD.x;
y = y1;
pDC->MoveTo(x,y);
y += 20;
pDC->LineTo(x,y);
//画重物
y += r;
pDC->Ellipse(x-r,y-r,x+r,y+r);
//画曲线坐标轴
x = m_YD.x + 200;
y = m_YD.y;
pDC->MoveTo(x,y);
y += 300;
pDC->LineTo(x,y);
x += 300;
pDC->LineTo(x,y);
//画曲线
x = m_YD.x + 200;
y = m_YD.y + 300;
pDC->MoveTo(x,y);
for(i = 0; i < m_Nl; i++)
{
x = m_YD.x + 200 + 3 * i;
y = m_YD.y + 300 + m_nl[i] * m_ky;
pDC->LineTo(x,y);
}
//计算弹簧长度
m_nl[m_Nl] = m_l - m_L;
m_Nl++;
}
7. Add member function Move(float DeltaT)
code show as below
void CTanHuang::Move(float DeltaT)
{
float f,a,zn;
zn = 0.1 * fabs(m_v) * m_v;//记得嵌入#include "math.h"头文件
f = (m_l - m_L) * m_K - 9.8 * m_m - zn;
a = f/m_m;
m_v += a * DeltaT;
m_v *= 0.9;
m_l -= m_v * DeltaT;
}
8. Settings menu
Click "Start", right-click, and select "Build Class Wizard...", as shown in the figure below. ("Pause" and "Setting" operations are the same as "Start")
9. Add window message handle
10. Introduce #include "TanHuang.h" header file
11. Call in OnTimer
void CSpringSimulationView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_TanHuang.Move(0.1);
Invalidate(TRUE);
CView::OnTimer(nIDEvent);
}
12. Call in OnDraw
void CSpringSimulationView::OnDraw(CDC* pDC)
{
CSpringSimulationDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
m_TanHuang.Draw(pDC);
}
13. Call the clock function in OnMStartSpring(), OnMPauseSpring()
void CSpringSimulationView::OnMStartSpring()
{
// TODO: Add your command handler code here
SetTimer(1,100,NULL);
}
void CSpringSimulationView::OnMPauseSpring()
{
// TODO: Add your command handler code here
KillTimer(1);
}
14. Preliminary effect diagram of compiling and running
15. Create a new dialog box and set the spring parameters
16. Edit dialog
17. Create a class wizard
18. Add member variables
19. Pass dialog parameters in OnMSetSpring()
code show as below
void CSpringSimulationView::OnMSetSpring()
{
// TODO: Add your command handler code here
CDlg_Set_Spring dlg;//记得引入#include "Dlg_Set_Spring.h"头文件
dlg.m_YD_X = m_TanHuang.m_YD.x;
dlg.m_YD_Y = m_TanHuang.m_YD.y;
dlg.m_L = m_TanHuang.m_L;
dlg.m_K = m_TanHuang.m_K;
dlg.m_m = m_TanHuang.m_m;
dlg.m_v = m_TanHuang.m_v;
UpdateData(FALSE);
if (dlg.DoModal() == IDOK)
{
m_TanHuang.m_YD.x = dlg.m_YD_X;
m_TanHuang.m_YD.y = dlg.m_YD_Y;
m_TanHuang.m_L = dlg.m_L;
m_TanHuang.m_K = dlg.m_K;
m_TanHuang.m_m = dlg.m_m;
m_TanHuang.m_v = dlg.m_v;
Invalidate(TRUE);
}
}
20. The final effect
可以通过对话框来设置一些基本参数。