gen_hetero.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>



/* taille de la grille 2D représentant l'océan: */
#define OCEAN_SIZE 1000

/* nombre de requins: */
#define SHARK_NB 3000

/* nombre de sardines */
#define SARDINE_NB 1500

/* age maximum des poissons */
#define AGE_MAX 10

/***********************************/

/* nombre de 'hot spots' */
#define HOT_SPOT_NB 5

#define max(x,y) ((x>y) ? x : y)
#define D_rand(imax) ((int) (((double) imax)*rand()/(RAND_MAX+1.0)))
#define D_rand_int(imin,imax) (D_rand(imax-imin)+imin)


int main(int argc, char **argv) {
  char ocean[OCEAN_SIZE*OCEAN_SIZE];
  int i,j;
  int x,y;
  int age;

  int nb_of_skark_per_spot[HOT_SPOT_NB];
  int nb_of_sardine_per_spot[HOT_SPOT_NB];

  int sum_of_sharks,sum_of_sardines;

  srand(time(NULL));

  sum_of_sharks = 0;
  sum_of_sardines = 0;
  for(i=0;i<HOT_SPOT_NB-1;i++) {
    nb_of_skark_per_spot[i] = D_rand(SHARK_NB/HOT_SPOT_NB);
    sum_of_sharks += nb_of_skark_per_spot[i];
    nb_of_sardine_per_spot[i] = D_rand(SARDINE_NB/HOT_SPOT_NB);
    sum_of_sardines += nb_of_sardine_per_spot[i];
    fprintf(stderr,"hot spot %i: %d sharks, %d sardines\n",i,nb_of_skark_per_spot[i],nb_of_sardine_per_spot[i]);
  }

  nb_of_skark_per_spot[i] = SHARK_NB - sum_of_sharks;
  nb_of_sardine_per_spot[i] = SARDINE_NB - sum_of_sardines;
  fprintf(stderr,"hot spot %i: %d sharks, %d sardines\n",i,nb_of_skark_per_spot[i],nb_of_sardine_per_spot[i]);

  memset(ocean, 0, sizeof(char)*OCEAN_SIZE*OCEAN_SIZE);
  fprintf(stdout,"%d\n",OCEAN_SIZE);


  for(i=0;i<HOT_SPOT_NB;i++) {
    int hot_spot_x,hot_spot_y;
    double hot_spot_radius;
    hot_spot_x = D_rand(OCEAN_SIZE);
    hot_spot_y = D_rand(OCEAN_SIZE);
    hot_spot_radius = D_rand_int(0.01*OCEAN_SIZE,0.2*OCEAN_SIZE);
    hot_spot_radius = max(hot_spot_radius,max(2*sqrt(nb_of_sardine_per_spot[i]),2*sqrt(nb_of_skark_per_spot[i])));
    fprintf(stderr,"hot spot %d: radius %lf\n",i,hot_spot_radius);
    for(j=0; j<nb_of_skark_per_spot[i]; j++) {
      do {
	double angle,radius;
	angle = rand()/((double) RAND_MAX) * 360.0;
	radius = rand()/ ((double) RAND_MAX) * hot_spot_radius;
	x = (int) (cos(angle) * radius + hot_spot_x);
	y = (int) (sin(angle) * radius + hot_spot_y);
	x = (x + OCEAN_SIZE) % OCEAN_SIZE;
	y = (y + OCEAN_SIZE) % OCEAN_SIZE;
      } while (ocean[(y*OCEAN_SIZE)+x] != 0);
      ocean[(y*OCEAN_SIZE)+x] = 1;
      age = D_rand(AGE_MAX);
      fprintf(stdout,"%d %d shark %d\n",x,y,age);
    }
    
    for(j=0; j<nb_of_sardine_per_spot[i]; j++) {
      do {
	double angle,radius;
	angle = rand()/((double) RAND_MAX) * 360.0;
	radius = rand()/ ((double) RAND_MAX) * hot_spot_radius;
	x = (int) (cos(angle) * radius + hot_spot_x);
	y = (int) (sin(angle) * radius + hot_spot_y);
	x = (x + OCEAN_SIZE) % OCEAN_SIZE;
	y = (y + OCEAN_SIZE) % OCEAN_SIZE;
      } while (ocean[(y*OCEAN_SIZE)+x] != 0);
      ocean[(y*OCEAN_SIZE)+x] = 1;
      age = D_rand(AGE_MAX);
      fprintf(stdout,"%d %d sardine %d\n",x,y,age);
    }
  }
    

  
  return 0;
}

Generated by GNU enscript 1.6.3.