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/Makefile | 41 +++++++++ simulations/boids.c | 237 +++++++++++++++++++++++++++++++++++++++++++++++++ simulations/boids.o | Bin 0 -> 283320 bytes simulations/outp | Bin 0 -> 190288 bytes simulations/universe.c | 123 +++++++++++++++++++++++++ simulations/universe.o | Bin 0 -> 270640 bytes 6 files changed, 401 insertions(+) create mode 100644 simulations/Makefile create mode 100644 simulations/boids.c create mode 100644 simulations/boids.o create mode 100755 simulations/outp create mode 100644 simulations/universe.c create mode 100644 simulations/universe.o (limited to 'simulations') diff --git a/simulations/Makefile b/simulations/Makefile new file mode 100644 index 0000000..9435c91 --- /dev/null +++ b/simulations/Makefile @@ -0,0 +1,41 @@ +# A simple Makefile for compiling small SDL projects + +# set the compiler +CC := gcc + +# set the compiler flags +CFLAGS := `sdl2-config --libs --cflags` -ggdb3 -O3 --std=c11 -Wall -lSDL2_image -lm +# add header files here +HDRS := + +# add source files here +SRCS := universe.c + +# generate names of object files +OBJS := $(SRCS:.c=.o) + +# name of executable +EXEC := outp + +# default recipe +all: $(EXEC) + +showfont: showfont.c Makefile + $(CC) -o $@ $@.c $(CFLAGS) $(LIBS) + +glfont: glfont.c Makefile + $(CC) -o $@ $@.c $(CFLAGS) $(LIBS) + +# recipe for building the final executable +$(EXEC): $(OBJS) $(HDRS) Makefile + $(CC) -o $@ $(OBJS) $(CFLAGS) + +# recipe for building object files +#$(OBJS): $(@:.o=.c) $(HDRS) Makefile +# $(CC) -o $@ $(@:.o=.c) -c $(CFLAGS) + +# recipe to clean the workspace +clean: + rm -f $(EXEC) $(OBJS) + +.PHONY: all clean diff --git a/simulations/boids.c b/simulations/boids.c new file mode 100644 index 0000000..84afc21 --- /dev/null +++ b/simulations/boids.c @@ -0,0 +1,237 @@ +#ifdef _WIN32 // грубый способ определения; на деле разница не в ОС, а в компиляторe + #include + #include + #include +#else + #include + #include + #include +#endif +#include +#include + +#define SCALE 1 +#define BOIDNUM 30 +#define SPEED 1 +#define CRANGE 30 +#define WIDTH 1280 +#define HEIGHT 640 +#define PI 3.1415926535 + +struct boid { double x, y, dx, dy, th; }; +typedef struct vec { float x, y; } vector; +struct boid B[BOIDNUM]; +char color[3] = {0, 255, 0}; + +int init() +{ + srand(time(NULL)); + + for(int i = 0; i CRANGE) {continue;} + if(b == n) {continue;} + sum.x += B[b].x; + sum.y += B[b].y; + } + + sum.x /= BOIDNUM - 1; + sum.y /= BOIDNUM - 1; + + sum.x -= B[n].x; + sum.y -= B[n].y; + sum.x /= factor; + sum.y /= factor; + + return sum; +} + +vector separation(int n, float dist, int factor) +{ + vector c = {0, 0}; + c.x = 0; + c.y = 0; + + for(int b = 0; b < BOIDNUM; b++) { + if(geomdist(n, b) > CRANGE) {continue;} + if(b == n) {continue;} + if(geomdist(n, b) < dist) { + c.x -= B[b].x - B[n].x; + c.y -= B[b].y - B[n].y; + } + } + + c.x /= factor; + c.y /= factor; + + return c; +} + +vector alignment(int n, int factor) +{ + vector pv = {0, 0}; + + for(int b = 0; b < BOIDNUM; b++) { + if(geomdist(n, b) > CRANGE) {continue;} + if(b == n) {continue;} + pv.x += B[b].dx; + pv.y += B[b].dy; + } + + pv.x /= BOIDNUM - 1; + pv.y /= BOIDNUM - 1; + + pv.x -= B[n].dx; + pv.y -= B[n].dy; + pv.x /= factor; + pv.y /= factor; + + return pv; +} + +int updatePos(SDL_Renderer *renderer) { + vector v[3]; + for(int i = 0; i WIDTH) + B[i].x -= WIDTH; + // B[i].dx = 0; + if(B[i].y > HEIGHT) + B[i].y -= HEIGHT; + // B[i].dy = 0; + if(B[i].x < 0) + B[i].x += WIDTH; + // B[i].dx = 0; + if(B[i].y < 0) + B[i].y += HEIGHT; + // B[i].dy = 0; + + v[0] = cohesion(i, 40); + v[1] = separation(i, 20, 200); + // v[0].x = 0; + // v[0].y = 0; + v[2] = alignment(i, 80); + // v[2].x = 0; + // v[2].y = 0; + + for(int j = 0; j<3; j++) { + if(i == 0) { + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderDrawLine(renderer, B[i].x, B[i].y, B[i].x-v[j].x, B[i].y - v[j].y); + SDL_RenderPresent(renderer); + printf("|vx = %f, vy = %f", v[j].x, v[j].y); + } + + B[i].dx += v[j].x / 1000; + B[i].dy += v[j].y / 1000; + } + // if (B[i].dx > sqrt(50)) B[i].dx = sqrt(50); + // if (B[i].dy > sqrt(50)) B[i].dy = sqrt(50); + B[i].th = acos(B[i].dx / sqrt(B[i].dx * B[i].dx + B[i].dy * B[i].dy) + 0.0001); + // if(i == 0) { + // printf("%f|", B[i].th); + // } + } + return 0; +} + +int main(int argc, char *argv[]) +{ + SDL_Window *window = NULL; + SDL_Renderer *renderer = NULL; + + init(); + SDL_Init(SDL_INIT_VIDEO); + + window = SDL_CreateWindow("Boids", + 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); + updatePos(renderer); + SDL_Delay(10); + } + + return 0; +} \ No newline at end of file diff --git a/simulations/boids.o b/simulations/boids.o new file mode 100644 index 0000000..7243741 Binary files /dev/null and b/simulations/boids.o differ diff --git a/simulations/outp b/simulations/outp new file mode 100755 index 0000000..f015d28 Binary files /dev/null and b/simulations/outp differ 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 diff --git a/simulations/universe.o b/simulations/universe.o new file mode 100644 index 0000000..8a2d703 Binary files /dev/null and b/simulations/universe.o differ -- cgit v1.2.3