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

📄 work2dlg.cpp

📁 计算机模拟物理作业,用来模拟2体运动
💻 CPP
字号:
// work2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "work2.h"
#include "work2Dlg.h"
#include<math.h>

#include <iostream>
#include <fstream>
using namespace std;

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWork2Dlg dialog

CWork2Dlg::CWork2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWork2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWork2Dlg)
	m_grav = 0.0;
	m_step = 0.0;
	m_vystart = 0.0;
	m_circuit = 0;
	m_vycoeff = 0.0;
	m_short = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWork2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWork2Dlg)
	DDX_Control(pDX, IDC_PIC, m_pic);
	DDX_Text(pDX, IDC_GRAV, m_grav);
	DDV_MinMaxDouble(pDX, m_grav, 0., 10000.);
	DDX_Text(pDX, IDC_STEP, m_step);
	DDX_Text(pDX, IDC_VYSTART, m_vystart);
	DDX_Text(pDX, IDC_CIRCUIT, m_circuit);
	DDV_MinMaxUInt(pDX, m_circuit, 0, 1000);
	DDX_Text(pDX, IDC_VYCOEFF, m_vycoeff);
	DDX_Text(pDX, IDC_SHORT, m_short);
	DDV_MinMaxUInt(pDX, m_short, 1, 10);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWork2Dlg, CDialog)
	//{{AFX_MSG_MAP(CWork2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDCLEAN, OnClean)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWork2Dlg message handlers

BOOL CWork2Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//添加上了自己的变量的初始化
	m_circuit =1;
	m_vycoeff = 1.0;
	m_step =0.001;
	m_grav = 2;
	m_short =1;
	CWnd *p; 
	CString tempstring;
	p=GetDlgItem(IDC_CIRCUIT); 
	tempstring.Format("%d", m_circuit);
    p->SetWindowText(tempstring);

	p=GetDlgItem(IDC_VYCOEFF); 
	tempstring.Format("%f", m_vycoeff);
    p->SetWindowText(tempstring);

	p=GetDlgItem(IDC_STEP); 
	tempstring.Format("%f", m_step);
    p->SetWindowText(tempstring);

	p=GetDlgItem(IDC_GRAV); 
	tempstring.Format("%f", m_grav);
    p->SetWindowText(tempstring);

	p=GetDlgItem(IDC_SHORT); 
	tempstring.Format("%d", m_short);
    p->SetWindowText(tempstring);
	//添加变量结束
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWork2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWork2Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWork2Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWork2Dlg::OnOK() 
{
	// TODO: Add extra validation here
	CWork2Dlg::UpdateData (TRUE);
	const double coefficient  = 1.334e20; //coefficient = MSun * G;
	const double distance = 1.5e11;
	double T;   //圆周运动的周期
	double X;   //X的坐标
	double Y;   //Y的坐标
	double Vx;  //X的速度
	double Vy;  //Y的速度
	double Ax;  //X的加速度
	double Ay;  //Y的加速度
	double grav = m_grav;//引力的系数
	double step = m_step;//计算的步长
	unsigned int circult = m_circuit; //计算的圈数
	double Vycoeff = m_vycoeff;//Y方向的速度系数
	double deltaT;//递加的时间
	double multdistance;//地球跟太阳的距离的平方
	double multspeed;//速度的平方

	//****************************************
	CWnd *p; 
	CString tempstring;
	//****************************************
	ofstream outx;
	ofstream outy;
	ofstream outvx;
	ofstream outvy;
	outx.open ("c:\\outx.txt");
	outy.open ("c:\\outy.txt");
	outvx.open ("c:\\outvx.txt");
	outvy.open ("c:\\outvy.txt");
	//*****************************************
	X = -distance; //初始化各个变量的值
	Y=0;	
	double temp1;
	Vx = 0;
	Vy = sqrt(coefficient/pow(distance,grav-1));  //做圆周运动的初始的速度
    p=GetDlgItem(IDC_VYSTART); 
	tempstring.Format("%f", Vy);
    p->SetWindowText(tempstring); 

	Vy *= Vycoeff;
	T = 2*3.1415926*sqrt(pow(distance,grav+1)/coefficient); //做圆周运动的周期
	deltaT  = T* step;
	long allcount = (long)1/step;

	//**************************************************************
	//定义跟界面相关的变量
	CRect rect;
	m_pic.GetClientRect (&rect);
	CWnd *pWnd;//得到图片框的窗口指针
	pWnd=GetDlgItem(IDC_PIC);
	CDC *dc=pWnd->GetDC();
	COLORREF col;
	dc->SetBkColor(RGB(220,230,0)) ;
	col = dc->GetBkColor();
	dc->FillSolidRect (rect.left,rect.top,rect.Width(),rect.Height(),col);
	dc->FillSolidRect (rect.Width()/2-5,rect.Height()/2-5,10,10,RGB(255,0,0));
	//dc->TextOut (rect.Width()/2,rect.Height()/2,"hello");
	double changesize;
	if(rect.Width ()<=rect.Height ())
		changesize= (rect.Width ()-40)/2/m_short/distance;
	else
		changesize= (rect.Height()-40)/2/m_short/distance;
	dc->MoveTo (rect.Width ()/2-(rect.Height()-40)/2/m_short,rect.Height()/2); 
	//移到开始画点的起始点
	int logicx,logicy;
	//**************************************************************
	//下面迭代计算
	for(int count = 0;count<circult*allcount;count ++)
	{
		multdistance = X*X + Y*Y;
		multspeed = Vx*Vx+Vy*Vy;
		temp1 = pow(multdistance,(grav+1)/2) ; 
		Ax =  -coefficient * X / temp1 ;//+ multspeed*X/multdistance;
		Ay =  -coefficient * Y / temp1 ;//+ multspeed*Y/multdistance;
		Vx += Ax * deltaT;
		Vy += Ay * deltaT;
		X += Vx * deltaT ; // - Ax*deltaT*deltaT/2;
		Y += Vy * deltaT ;
		outvx<<Vx<<endl;
		outvy<<Vy<<endl;
		outx<<X<<","<<endl;
		outy<<Y<<","<<endl;
		// 画图
		logicx = rect.Width ()/2 + X*changesize;
		logicy = rect.Height()/2 + Y*changesize;
		if((logicx==0) &&(logicy==0))  //如果已经是在太阳的位置了,就终止计算
		{
			dc->TextOut (rect.Width()-100,rect.Height ()-20,"行星被太阳捕获");
			break;
		}
		//if((logicx>rect.Width())||(logicy>rect.Height()))  //如果已经是在太阳的位置了,就终止计算
		//{
		//	dc->TextOut (rect.Width()-120,rect.Height ()-20,"行星脱离太阳系");
		//	continue;
		//}
		dc->LineTo (logicx,logicy);	

	}
	dc->TextOut (0,rect.Height ()-20,"计算结束");
	outy.close ();
	outx.close ();
	outvy.close ();
	outvx.close ();
}

void CWork2Dlg::OnClean() 
{
	// TODO: Add your control notification handler code here
	CRect rect;
	m_pic.GetClientRect (&rect);
	CWnd *pWnd;//得到图片框的窗口指针
	pWnd=GetDlgItem(IDC_PIC);
	CDC *dc=pWnd->GetDC();
	COLORREF col;
	dc->SetBkColor(RGB(220,230,0)) ;
	col = dc->GetBkColor();
	dc->FillSolidRect (rect.left,rect.top,rect.Width(),rect.Height(),col);
}

void CWork2Dlg::OnCancel() 
{
	// TODO: Add extra cleanup here
		//******************************
	OnClean() ;
	CWnd *p; 
	CString tempstring;
	p=GetDlgItem(IDC_PIC);
	CDC *dc=p->GetDC();
	CRect rect;
	m_pic.GetClientRect (&rect);
	dc->TextOut (0,rect.top+10,"行星运动(地球绕太阳的运行)");
	dc->TextOut (0,rect.top+30,"可以得到地球运动的初速度,周期等参数");
	dc->TextOut (0,rect.top+50,"同时计算的速度,坐标的数据都放在c盘的根目录下面,请查看:");
	dc->TextOut (0,rect.top+70,"文件名为:速度:outvx.txt,outvy.txt,坐标:outx.txt,outy.txt");
	p->UpdateData (TRUE);
	AfxMessageBox("确定退出程序?");
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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