跳到正文

个人记录

引力模拟

Hjr1141451931约 5 分钟
原文
文章信息与版本

文章信息

发布
2022年8月31日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 5 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#include<windows.h>
#define KeyDown(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000) ? 1:0) 
using namespace std;
HWND hWnd=GetConsoleWindow();
HDC hdc=GetDC(GetConsoleWindow()),Mem;
HBITMAP hBmp;HGDIOBJ hOldSel;
HPEN hpenwhite = CreatePen( PS_SOLID, 1, RGB(255,255,255) );
HPEN hpenblack = CreatePen( PS_SOLID, 1, RGB(0,0,0) );
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE),In=GetStdHandle(STD_INPUT_HANDLE);

mt19937 mrand(time(0));
int rnd(int x){
    return mrand()%x+1;
}

double tgx,tgy,eps=1e-5,G=6.67408*1e-11,M=5*1e13,V0=10;
struct Vector{
    double x,y;
    double px,py;
    double m;
};
Vector Pts[60];
Vector operator +(Vector a,Vector b){
    Vector c;
    c.x=a.x,c.y=a.y;
    c.px=a.px+b.px,c.py=a.py+b.py;
    return c;
}
Vector operator *(double a,Vector b){
    Vector c;
    c.x=b.x,c.y=b.y;
    c.px=a*b.px,c.py=a*b.py;
    return c;
}
void motivate(){
    while(1){
        for(int u=1;u<=50;u++){
            double dx=tgx-Pts[u].x,dy=tgy-Pts[u].y,m=Pts[u].m;
            double len=sqrt(dx*dx+dy*dy);
            Pts[u].x+=Pts[u].px+dx/len*(M*m*G/(len*len)),Pts[u].y+=Pts[u].py+dy/len*(M*m*G/(len*len));
            Pts[u].px+=dx/len*(M*m*G/(len*len)),Pts[u].py+=dy/len*(M*m*G/(len*len));
        }
        Sleep(10);
    }
}
void Print(){
    while(1){
 		Mem=CreateCompatibleDC(hdc);
 		hBmp=CreateCompatibleBitmap(hdc,1979,1079);
 		hOldSel = SelectObject(Mem,hBmp);   	
        for(int i=1;i<=50;i++){
            SelectObject(Mem,hpenwhite);
            MoveToEx(Mem,Pts[i].x+990,Pts[i].y+540,NULL);
            LineTo(Mem,Pts[i].x+Pts[i].px+990,Pts[i].y+Pts[i].py+540);
        }   
		BitBlt(hdc,0,0,1979,1079,Mem,0,0,SRCCOPY);
		SelectObject(Mem,hOldSel);
		DeleteDC(Mem);         
        Sleep(10);
        //system("cls");
    }
} 
void Cls(){
    while(1){
    //  system("cls");
        Sleep(200);
    }
}
int main(){
    ShowWindow(hWnd,SW_MAXIMIZE);
    DWORD mode;
    GetConsoleMode(In,&mode);
    mode &= ~ENABLE_QUICK_EDIT_MODE;
    SetConsoleMode(In,mode);
    CONSOLE_CURSOR_INFO A;
    GetConsoleCursorInfo(Out,&A);
    A.bVisible=false;
    SetConsoleCursorInfo(Out,&A);   

    thread P(Print),M(motivate),C(Cls);
    P.detach(),M.detach(),C.detach();
    while(1){
        if(KeyDown(VK_LBUTTON)){
            POINT p;
            GetCursorPos(&p);
            tgx=p.x-990,tgy=p.y-540;
            for(int i=1;i<=50;i++){
                Pts[i]=(Vector){1000-rnd(2000),500-rnd(1000),V0-rnd(V0*2),V0-rnd(V0*2),rnd(100)};
            }
            Sleep(1000);
        }
    }
    return 0;
}
原文