Advertisement
Guest User

Untitled

a guest
Jun 29th, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <unistd.h>
  5. #include <SDL/SDL.h>
  6.  
  7. typedef struct
  8. {
  9.     double x, y;
  10.     double tx, ty;
  11.     int z;
  12. } per;
  13.  
  14. int n;
  15. per *mass;
  16. double rad;
  17. double ra;
  18. double vh, vz;
  19.  
  20.  
  21. void zombie_field(int k, double *fx, double *fy)
  22. {
  23.     int i;
  24.     double ffx, ffy;
  25.     double l;
  26.     double x = mass[k].x;
  27.     double y = mass[k].y;
  28.     *fx = 0;
  29.     *fy = 0;
  30.     for (i = 0; i < n; i++)
  31.     {
  32.         if (mass[i].z == 1)
  33.         {
  34.             ffx = mass[i].x - x;
  35.             ffy = mass[i].y - y;
  36.             l = sqrt(ffx*ffx + ffy*ffy);
  37.             if (l > 1e-10)
  38.             {
  39.                 ffx /= -l;
  40.                 ffy /= -l;
  41.             }
  42.             *fx += ffx;
  43.             *fy += ffy;
  44.         }
  45.     }
  46. }
  47.  
  48. void human_field(int k, double *fx, double *fy)
  49. {
  50.     int i;
  51.     double l;
  52.     double ffx, ffy;
  53.     double x = mass[k].x;
  54.     double y = mass[k].y;
  55.     *fx = 0;
  56.     *fy = 0;
  57.     for (i = 0; i < n; i++)
  58.     {
  59.         if (mass[i].z == 0)
  60.         {
  61.             ffx = mass[i].x - x;
  62.             ffy = mass[i].y - y;
  63.             l = sqrt(ffx*ffx + ffy*ffy);
  64.  
  65.             if (l > 1e-6)
  66.             {
  67.                 ffx /= l*l;
  68.                 ffy /= l*l;
  69.             }
  70.             *fx += ffx;
  71.             *fy += ffy;
  72.         }
  73.     }
  74. }
  75.  
  76. void space_field(int k, double *fx, double *fy)
  77. {
  78.     int i;
  79.     double l;
  80.     double ffx, ffy;
  81.     double x = mass[k].x;
  82.     double y = mass[k].y;
  83.     *fx = 0;
  84.     *fy = 0;
  85.     for (i = 0; i < n; i++)
  86.     {
  87.         if (mass[i].z == mass[k].z)
  88.         {
  89.             ffx = mass[i].x - x;
  90.             ffy = mass[i].y - y;
  91.             l = ffx*ffx + ffy*ffy;
  92.             if (l > 1e-6)
  93.             {
  94.                 ffx /= -l;
  95.                 ffy /= -l;
  96.             }
  97.             *fx += ffx;
  98.             *fy += ffy;
  99.         }
  100.     }
  101. }
  102.  
  103. void extra_field(int k, double *fx, double *fy)
  104. {
  105.     int i;
  106.     double l;
  107.     double ffx, ffy;
  108.     double x = mass[k].x;
  109.     double y = mass[k].y;
  110.  
  111.     l = sqrt(x*x+y*y);
  112.     if (l > rad)
  113.     {
  114.         *fx = -x;
  115.         *fy = -y;
  116.     }
  117.    
  118.        
  119. }
  120.  
  121. int if_victim(int k)
  122. {
  123.     int i;
  124.     for (i = 0; i < n; i++)
  125.     {
  126.         if (mass[i].z == 1)
  127.         {
  128.             double x, y, zx, zy, l;
  129.             x = mass[k].x;
  130.             y = mass[k].y;
  131.             zx = mass[i].x;
  132.             zy = mass[i].y;
  133.             l = sqrt((x-zx)*(x-zx) + (y-zy)*(y-zy));
  134.             if (l < ra)
  135.                 return 1;
  136.         }
  137.     }
  138.     return 0;
  139. }
  140.  
  141. void go(double dt)
  142. {
  143.     int i;
  144.     for (i = 0; i < n; i++)
  145.     {
  146.         double fx, fy, sx=0, sy=0, l;
  147.         if (mass[i].z == 0)
  148.         {
  149.             zombie_field(i, &fx, &fy);
  150.         }  
  151.         else
  152.         {
  153.             human_field(i, &fx, &fy);
  154.         }
  155.  
  156.         sx += fx;
  157.         sy += fy;
  158.        
  159.         space_field(i, &fx, &fy);
  160.        
  161.         sx += fx;
  162.         sy += fy;
  163.        
  164.         extra_field(i, &fx, &fy);
  165.        
  166.         sx += fx;
  167.         sy += fy;
  168.  
  169.         l = sqrt(sx*sx+sy*sy);
  170.         if (l > 1e-6)
  171.         {
  172.             sx /= l;
  173.             sy /= l;
  174.         }
  175.        
  176.         if (mass[i].z == 0)
  177.         {
  178.             sx *= vh*dt;
  179.             sy *= vh*dt;
  180.         }
  181.         else
  182.         {
  183.             sx *= vz*dt;
  184.             sy *= vz*dt;
  185.         }
  186.  
  187.         mass[i].tx = mass[i].x + sx;
  188.         mass[i].ty = mass[i].y + sy;
  189.     }
  190.     for (i = 0; i < n; i++)
  191.     {
  192.         double l;
  193.         mass[i].x = mass[i].tx;
  194.         mass[i].y = mass[i].ty;
  195.     }
  196.     for (i = 0; i < n; i++)
  197.     {
  198.         if (mass[i].z == 0)
  199.             mass[i].z = if_victim(i);
  200.     }
  201. }
  202.  
  203.  
  204. Uint8 colour_h, colour_z;  
  205.  
  206. void draw_pixel(int X, int Y, Uint8 color, SDL_Surface *screen)
  207. {
  208.     Uint8 *pixmem;
  209.     if (X < 10)
  210.         X = 10;
  211.     if (Y < 10)
  212.         Y = 10;
  213.     if (X >= 790)
  214.         X = 789;
  215.     if (Y >= 790)
  216.         Y = 789;
  217.  
  218.     pixmem = (Uint8 *)screen->pixels + Y * screen->pitch + X * screen->format->BytesPerPixel;      
  219.     *pixmem = color;
  220. }
  221.  
  222. void draw_per(double x, double y, int z, SDL_Surface *screen)
  223. {
  224.     Uint8 color;
  225.     int X, Y;
  226.     if (z)
  227.         color = colour_z;
  228.     else   
  229.         color = colour_h;
  230.     X = x/(rad*2)*400 + 400;
  231.     Y = y/(rad*2)*400 + 400;
  232.    
  233.     draw_pixel(X-1, Y-1, color, screen);
  234.     draw_pixel(X-1, Y, color, screen);
  235.     draw_pixel(X-1, Y+1, color, screen);
  236.     draw_pixel(X, Y-1, color, screen);
  237.     draw_pixel(X, Y, color, screen);
  238.     draw_pixel(X, Y+1, color, screen);
  239.     draw_pixel(X+1, Y-1, color, screen);
  240.     draw_pixel(X+1, Y, color, screen);
  241.     draw_pixel(X+1, Y+1, color, screen);
  242.    
  243.        
  244. }
  245.  
  246. int main(void)
  247. {
  248.     double t, t1;
  249.     double dt = 0.1;
  250.     int i;
  251.     int keypress = 0;
  252.     SDL_Surface *screen;
  253.     SDL_Event event;
  254.  
  255.     Uint8 *pixmem;
  256.    
  257.     srand(time(NULL));
  258.  
  259.     n = 20;
  260.     rad = 10;
  261.     ra = 0.5;
  262.  
  263.     vh = 1;
  264.     vz = 2;
  265.  
  266.     t1 = 10;   
  267.  
  268.     SDL_Init(SDL_INIT_VIDEO);
  269.     screen = SDL_SetVideoMode(800, 800,8,SDL_HWSURFACE|SDL_DOUBLEBUF);
  270.  
  271.     colour_h = SDL_MapRGB( screen->format, 0, 255, 0 );
  272.     colour_z = SDL_MapRGB( screen->format, 255, 0, 0 );
  273.    
  274.  
  275.     mass = malloc(sizeof(per)*n);
  276.  
  277.     for (i = 0; i < n; i++)
  278.     {
  279.         double l, a;
  280.        
  281.         l = (rand()%RAND_MAX)/((double)RAND_MAX)*rad;
  282.         a = (rand()%RAND_MAX)/((double)RAND_MAX)*2*3.141;
  283.         mass[i].x = l*cos(a);
  284.         mass[i].y = l*sin(a);
  285.        
  286.         mass[i].z = 0;
  287.     }
  288.    
  289.     mass[0].z = 1;
  290.  
  291.  
  292.     while(!keypress)
  293.     {
  294.         go(dt);
  295.             usleep(dt*1e6);
  296.         printf("t = %lf\n", t);
  297.  
  298.         SDL_FillRect(screen, NULL, 0x000000);
  299.         for (i = 0; i < n; i++)
  300.         {
  301.             draw_per(mass[i].x, mass[i].y, mass[i].z, screen);
  302.             printf("%s %lf %lf\n", mass[i].z?"z":"h", mass[i].x, mass[i].y);
  303.         }
  304.  
  305.         SDL_Flip(screen);
  306.  
  307.         while(SDL_PollEvent(&event))
  308.             {      
  309.                 switch (event.type)
  310.                     {  
  311.                 case SDL_QUIT:
  312.                     keypress = 1;
  313.                     break;
  314.                 case SDL_KEYDOWN:
  315.                     keypress = 1;
  316.                     break;
  317.             }
  318.         }
  319.         t+= dt;
  320.     }
  321.  
  322.  
  323.     SDL_Quit();
  324.     free(mass);
  325.     return 0;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement