Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <unistd.h>
- #include <SDL/SDL.h>
- typedef struct
- {
- double x, y;
- double tx, ty;
- int z;
- } per;
- int n;
- per *mass;
- double rad;
- double ra;
- double vh, vz;
- void zombie_field(int k, double *fx, double *fy)
- {
- int i;
- double ffx, ffy;
- double l;
- double x = mass[k].x;
- double y = mass[k].y;
- *fx = 0;
- *fy = 0;
- for (i = 0; i < n; i++)
- {
- if (mass[i].z == 1)
- {
- ffx = mass[i].x - x;
- ffy = mass[i].y - y;
- l = sqrt(ffx*ffx + ffy*ffy);
- if (l > 1e-10)
- {
- ffx /= -l;
- ffy /= -l;
- }
- *fx += ffx;
- *fy += ffy;
- }
- }
- }
- void human_field(int k, double *fx, double *fy)
- {
- int i;
- double l;
- double ffx, ffy;
- double x = mass[k].x;
- double y = mass[k].y;
- *fx = 0;
- *fy = 0;
- for (i = 0; i < n; i++)
- {
- if (mass[i].z == 0)
- {
- ffx = mass[i].x - x;
- ffy = mass[i].y - y;
- l = sqrt(ffx*ffx + ffy*ffy);
- if (l > 1e-6)
- {
- ffx /= l*l;
- ffy /= l*l;
- }
- *fx += ffx;
- *fy += ffy;
- }
- }
- }
- void space_field(int k, double *fx, double *fy)
- {
- int i;
- double l;
- double ffx, ffy;
- double x = mass[k].x;
- double y = mass[k].y;
- *fx = 0;
- *fy = 0;
- for (i = 0; i < n; i++)
- {
- if (mass[i].z == mass[k].z)
- {
- ffx = mass[i].x - x;
- ffy = mass[i].y - y;
- l = ffx*ffx + ffy*ffy;
- if (l > 1e-6)
- {
- ffx /= -l;
- ffy /= -l;
- }
- *fx += ffx;
- *fy += ffy;
- }
- }
- }
- void extra_field(int k, double *fx, double *fy)
- {
- int i;
- double l;
- double ffx, ffy;
- double x = mass[k].x;
- double y = mass[k].y;
- l = sqrt(x*x+y*y);
- if (l > rad)
- {
- *fx = -x;
- *fy = -y;
- }
- }
- int if_victim(int k)
- {
- int i;
- for (i = 0; i < n; i++)
- {
- if (mass[i].z == 1)
- {
- double x, y, zx, zy, l;
- x = mass[k].x;
- y = mass[k].y;
- zx = mass[i].x;
- zy = mass[i].y;
- l = sqrt((x-zx)*(x-zx) + (y-zy)*(y-zy));
- if (l < ra)
- return 1;
- }
- }
- return 0;
- }
- void go(double dt)
- {
- int i;
- for (i = 0; i < n; i++)
- {
- double fx, fy, sx=0, sy=0, l;
- if (mass[i].z == 0)
- {
- zombie_field(i, &fx, &fy);
- }
- else
- {
- human_field(i, &fx, &fy);
- }
- sx += fx;
- sy += fy;
- space_field(i, &fx, &fy);
- sx += fx;
- sy += fy;
- extra_field(i, &fx, &fy);
- sx += fx;
- sy += fy;
- l = sqrt(sx*sx+sy*sy);
- if (l > 1e-6)
- {
- sx /= l;
- sy /= l;
- }
- if (mass[i].z == 0)
- {
- sx *= vh*dt;
- sy *= vh*dt;
- }
- else
- {
- sx *= vz*dt;
- sy *= vz*dt;
- }
- mass[i].tx = mass[i].x + sx;
- mass[i].ty = mass[i].y + sy;
- }
- for (i = 0; i < n; i++)
- {
- double l;
- mass[i].x = mass[i].tx;
- mass[i].y = mass[i].ty;
- }
- for (i = 0; i < n; i++)
- {
- if (mass[i].z == 0)
- mass[i].z = if_victim(i);
- }
- }
- Uint8 colour_h, colour_z;
- void draw_pixel(int X, int Y, Uint8 color, SDL_Surface *screen)
- {
- Uint8 *pixmem;
- if (X < 10)
- X = 10;
- if (Y < 10)
- Y = 10;
- if (X >= 790)
- X = 789;
- if (Y >= 790)
- Y = 789;
- pixmem = (Uint8 *)screen->pixels + Y * screen->pitch + X * screen->format->BytesPerPixel;
- *pixmem = color;
- }
- void draw_per(double x, double y, int z, SDL_Surface *screen)
- {
- Uint8 color;
- int X, Y;
- if (z)
- color = colour_z;
- else
- color = colour_h;
- X = x/(rad*2)*400 + 400;
- Y = y/(rad*2)*400 + 400;
- draw_pixel(X-1, Y-1, color, screen);
- draw_pixel(X-1, Y, color, screen);
- draw_pixel(X-1, Y+1, color, screen);
- draw_pixel(X, Y-1, color, screen);
- draw_pixel(X, Y, color, screen);
- draw_pixel(X, Y+1, color, screen);
- draw_pixel(X+1, Y-1, color, screen);
- draw_pixel(X+1, Y, color, screen);
- draw_pixel(X+1, Y+1, color, screen);
- }
- int main(void)
- {
- double t, t1;
- double dt = 0.1;
- int i;
- int keypress = 0;
- SDL_Surface *screen;
- SDL_Event event;
- Uint8 *pixmem;
- srand(time(NULL));
- n = 20;
- rad = 10;
- ra = 0.5;
- vh = 1;
- vz = 2;
- t1 = 10;
- SDL_Init(SDL_INIT_VIDEO);
- screen = SDL_SetVideoMode(800, 800,8,SDL_HWSURFACE|SDL_DOUBLEBUF);
- colour_h = SDL_MapRGB( screen->format, 0, 255, 0 );
- colour_z = SDL_MapRGB( screen->format, 255, 0, 0 );
- mass = malloc(sizeof(per)*n);
- for (i = 0; i < n; i++)
- {
- double l, a;
- l = (rand()%RAND_MAX)/((double)RAND_MAX)*rad;
- a = (rand()%RAND_MAX)/((double)RAND_MAX)*2*3.141;
- mass[i].x = l*cos(a);
- mass[i].y = l*sin(a);
- mass[i].z = 0;
- }
- mass[0].z = 1;
- while(!keypress)
- {
- go(dt);
- usleep(dt*1e6);
- printf("t = %lf\n", t);
- SDL_FillRect(screen, NULL, 0x000000);
- for (i = 0; i < n; i++)
- {
- draw_per(mass[i].x, mass[i].y, mass[i].z, screen);
- printf("%s %lf %lf\n", mass[i].z?"z":"h", mass[i].x, mass[i].y);
- }
- SDL_Flip(screen);
- while(SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- keypress = 1;
- break;
- case SDL_KEYDOWN:
- keypress = 1;
- break;
- }
- }
- t+= dt;
- }
- SDL_Quit();
- free(mass);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement