c++模擬質點在萬有引力下的運動軌跡

PhysicsIStheMostBeautiful\mathcal{Physics IStheMostBeautiful}

通過c++模擬質點在萬有引力下的運動軌跡

0.ForwardItroduction\mathcal{ForwardItroduction}

應某集團首領要求:

通過代碼實現天體間萬有引力作用的模擬運動軌跡
時限一晚上

通過模擬鍵盤和鼠標的點擊在畫圖中繪製圖像

1.PreKnowledge1ToControlKeyboard\mathcal{PreKnowledge1\to ToControlKeyboard}

直接上代碼

keybd_event(num/*Key_Code*/, 0, 0, 0);//按下按鍵
keybd_event(num/*Key_Code*/, 0, KEYEVENTF_KEYUP, 0);//放開按鍵

num是你所要按下的鍵的鍵碼,字母的鍵碼就是本身的Ascll碼,鍵碼錶如下(來自網絡):
cnblogs-萬一

2.PreKnowledge2ToControlMouse\mathcal{PreKnowledge2\to ToControlMouse}

仍然上代碼

POINT p;
GetCursorPos(&p);//獲取鼠標座標
printf("%d %d\n", p.x, p.y);

SetCursorPos(x, y);//移動到(x,y)
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);//按下左鍵
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);//放開左鍵
mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0);//按下右鍵
mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, 0);//放開右鍵

鼠標的座標(p.x,p.y)(p.x,p.y)以像素爲單位,且一定爲整數

3.PreKnowledge3UniversalGravitationsLaw\mathcal{PreKnowledge3\to UniversalGravitation'sLaw}

根據中學知識知道
Fg=Gm1m2R2F_g=\frac{Gm_1m_2}{R^2}
G6.67×1011Nm2kg2G\approx6.67\times 10^{-11}N\cdot m^2\cdot kg^{-2}
因爲GG的精度要求較大,所以可以適當縮放單位大小,如:

mkmm\to km,kg10000tkg\to 10000t

4.PreKnowledge4NewtonsSecondLaw\mathcal{PreKnowledge4\to Newton'sSecondLaw}

F=maF=ma
我們可以把時間劃分成很小很小的單位,如10ms10ms,每次以上一次的座標

#include <cstdio>
#include <iostream>
#include <cmath>
#include <windows.h>
#include <winuser.h>
#define N 100

char st[100];
const int ox = 795, oy = 492, unt = 10;
const long double G = 0.667408;

struct Node{
	long double x, y, vx, vy, fx, fy, m;
}a[N];

inline void cli_key(int id) {
	keybd_event(id, 0, 0, 0);
	keybd_event(id, 0, KEYEVENTF_KEYUP, 0);
}

inline void cli_mouse(int x, int y) {
	SetCursorPos(x, y);
	mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
	mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

inline void prt_st(char *st) {
	for (int i = 0; i < strlen(st); ++i) {
		int id;
		if (st[i] >= 'a' && st[i] <= 'z') id = st[i] - 'a' + 'A';
		else id = st[i];
		cli_key(id);
	}
}

inline void prt_line(int x1, int y1, int x2, int y2) {
	SetCursorPos(x1, y1);
	mouse_event(MOUSEEVENTF_LEFTDOWN, x1, y1, 0, 0);
	SetCursorPos(x2, y2);
	mouse_event(MOUSEEVENTF_LEFTUP, x2, y2, 0, 0);
}

inline void cli_pencil() {
	cli_mouse(269, 78);
}

inline void getxy() {
	Sleep(5000);
	POINT p;
	GetCursorPos(&p);
	printf("%d %d\n", p.x, p.y);
}

int main() {
	int n, time;
	puts("time(s)?");
	scanf("%d", &time);
	puts("The scale is 100metres");
	printf("The number of particle(n <= 20):");
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		printf("The X & Y(*100m) of %d-th particle:", i);
		std :: cin >> a[i].x >> a[i].y;
		printf("The Vx & Vy(m/s) of %d-th particle:", i);
		std :: cin >> a[i].vx >> a[i].vy;
		printf("The M(*10000000kg) of %d-th particle:", i);
		std :: cin >> a[i].m;

	}
	keybd_event(91, 0, 0, 0);
	keybd_event('R', 0, 0, 0);
	keybd_event('R', 0, KEYEVENTF_KEYUP, 0);
	keybd_event(91, 0, KEYEVENTF_KEYUP, 0);
	Sleep(1000);
	prt_st("mspaint");
	cli_key(13);
	Sleep(1000);
	keybd_event(18, 0, 0, 0);
	keybd_event(32, 0, 0, 0);
	keybd_event('X', 0, 0, 0);
	keybd_event('X', 0, KEYEVENTF_KEYUP, 0);
	keybd_event(32, 0, KEYEVENTF_KEYUP, 0);
	keybd_event(18, 0, KEYEVENTF_KEYUP, 0);
	Sleep(1000);
	keybd_event(17, 0, 0, 0);
	keybd_event('E', 0, 0, 0);
	keybd_event('E', 0, KEYEVENTF_KEYUP, 0);
	keybd_event(17, 0, KEYEVENTF_KEYUP, 0);
	Sleep(1000);
	prt_st("1580");
	cli_key(9);
	prt_st("660");
	cli_key(13);
	cli_pencil();
//	getxy();
	prt_line(5, 492, 1585, 492);
	prt_line(795, 162, 795, 822);
	prt_line(ox + unt, oy, ox + unt, oy - 7);
	for (int k = 1; k <= time / 0.01; ++k) {
		for (int i = 1; i <= n; ++i) {
			a[i].fx = a[i].fy = 0;
			for (int j = 1; j <= n; ++j) 
				if (i != j) {
					long double sqrR = (a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y);
					long double F = G * (a[i].m * a[j].m) / sqrR;
					if (F > 10000000) F = 10000000;
					a[i].fx += F * (a[j].x - a[i].x) / sqrt(sqrR);
					a[i].fy += F * (a[j].y - a[i].y) / sqrt(sqrR);
				}
		}
		for (int i = 1; i <= n; ++i) {
			printf("%.4Lf %.4Lf %.4Lf %.4Lf %.4Lf %.4Lf %.4Lf\n", a[i].x, a[i].y, a[i].vx, a[i].vy, a[i].m, a[i].fx, a[i].fy);
			a[i].vx += a[i].fx / a[i].m * 0.01;
			a[i].vy += a[i].fy / a[i].m * 0.01;
			long double xx = a[i].x + a[i].vx * 0.01;
			long double yy = a[i].y + a[i].vy * 0.01;
			int xx1 = (int)(a[i].x * unt) + ox, yy1 = (int)(a[i].y * unt) + oy, xx2 = (int)(xx * unt) + ox, yy2 = (int)(yy * unt) + oy;
			int flg = 0;
			if (xx2 <= 10) {
				a[i].x = 78;
				flg = 1;
			}
			if (xx2 >= 1580) {
				a[i].x = -78;
				flg = 1;
			}
			if (yy2 <= 167) {
				a[i].y = 32;
				flg = 1;
			}
			if (yy2 >= 817) {
				a[i].y = -32;
				flg = 1;
			}
			if (flg) continue;
			prt_line(xx1, yy1, xx2, yy2);
			a[i].x = xx;
			a[i].y = yy;
		}
		Sleep(10);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章