From 3c0a3b8413ee58a3e5678075c862e95f7c8395c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80=20=D0=9B=D1=8C=D0=B2=D0=BE=D0=B2?= Date: Mon, 27 Mar 2023 11:25:27 +0300 Subject: =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simulations/universe.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 simulations/universe.c (limited to 'simulations/universe.c') diff --git a/simulations/universe.c b/simulations/universe.c new file mode 100644 index 0000000..d6933af --- /dev/null +++ b/simulations/universe.c @@ -0,0 +1,123 @@ +#include +#include +#include + +#include +#include + +#define NUM_BODIES 500 +#define PARTSIZE 10 +#define WIDTH 512 +#define HEIGHT 512 + +struct body { double x, y, dx, dy; }; +typedef struct vec { float x, y; } vector; +struct body B[NUM_BODIES]; + +void init() +{ + srand(time(NULL)); + for(int i = 0; i < NUM_BODIES; i++) { + B[i].x = WIDTH/2 + rand() % 20 - 40; + B[i].y = HEIGHT/2 + rand() % 20 - 40; + B[i].dx = rand() % 50 - 100; + B[i].dy = rand() % 50 - 100; + } +} + +float geomdist(int i, int j) +{ + return sqrt(pow( B[i].x - B[j].x, 2 ) + pow( B[i].y - B[j].y, 2 )); +} + +void updatePos() { + for(int i = 0; i < NUM_BODIES; i++) { + B[i].x += B[i].dx; + B[i].y += B[i].dy; + printf("%f %f |", B[i].dx, B[i].dy); + + vector mass_centre_vec; + for(int j = 0; j < NUM_BODIES; j++) { + if(i == j) continue; + mass_centre_vec.x += (B[j].x - B[i].x) /geomdist(i, j); + mass_centre_vec.y += (B[j].y - B[i].y) /geomdist(i, j); + } + mass_centre_vec.x /= NUM_BODIES - 1; + mass_centre_vec.y /= NUM_BODIES - 1; + + B[i].dx = mass_centre_vec.x; + B[i].dy = mass_centre_vec.y; + } +} + +int processEvents(SDL_Window *window) +{ + SDL_Event event; + + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_WINDOWEVENT_CLOSE: + if(window) + { + SDL_DestroyWindow(window); + window = NULL; + return 1; + } + break; + + case SDL_KEYDOWN: + switch(event.key.keysym.sym) { + case SDLK_ESCAPE: + return 1; + } + } + } + return 0; +} + +void render(SDL_Renderer *renderer, SDL_Rect particles[NUM_BODIES]) +{ + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // фон + SDL_RenderClear(renderer); + SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); + + for(int i = 0; i < NUM_BODIES; i++) { + particles[i].h = PARTSIZE; + particles[i].w = PARTSIZE; + particles[i].x = B[i].x - PARTSIZE/2; + particles[i].y = B[i].y - PARTSIZE/2; + SDL_RenderDrawRect(renderer, &particles[i]); + } + + SDL_RenderPresent(renderer); +} + +int main(int argc, char *argv[]) +{ + SDL_Window *window = NULL; + SDL_Renderer *renderer = NULL; + + init(); + SDL_Init(SDL_INIT_VIDEO); + SDL_Rect particles[NUM_BODIES]; + + window = SDL_CreateWindow("Universe simulation", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + WIDTH, + HEIGHT, + 0); + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + + while(1) { + if(processEvents(window) == 1) + break; + render(renderer, particles); + updatePos(renderer); + SDL_Delay(10); + } + + return 0; +} \ No newline at end of file -- cgit v1.2.3