boid.c 1.96 KB
Newer Older
Cassio Kirch's avatar
Cassio Kirch committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "define.h"
#include "boid.h"

unsigned int
getBoxID (struct Boid boid)
{
  unsigned int tmpBoxID;

  boid.position[X] += 0.50000*RANGE;
  boid.position[Y] += 0.50000*RANGE;

  tmpBoxID = (unsigned int) (boid.position[X] *                         \
                             ((double) BOXES_IN_EDGE/RANGE)) +     \
    BOXES_IN_EDGE * (unsigned int) (boid.position[Y] *                  \
                                   (double) BOXES_IN_EDGE/RANGE);
  if (boid.position[X] == RANGE)
    tmpBoxID -= 1;
  if (boid.position[Y] == RANGE)
    tmpBoxID -= BOXES_IN_EDGE;
  return tmpBoxID;
}

27 28 29 30 31 32
void
initializeCircleCorrectly(struct Boid* const boid)
{
  double angle, radius;
  angle  = RANDOM_0(PI*2.0);
  radius = sqrt(RANDOM_0(1.0)) *                \
33
    sqrt(0.137832224 * N) * ELASTIC_DISTANCE;
34 35 36 37 38
  /* R^2 = re^2 * N * sqrt(3) / (4 pi) */
  boid -> position[X] = radius * cos(angle);
  boid -> position[Y] = radius * sin(angle);
}

Cassio Kirch's avatar
Cassio Kirch committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
void
initializeCircle (struct Boid* const boid)
{
  double angle, radius;
  angle               = RANDOM_0(PI*2.0);
  radius              = sqrt(RANDOM_0(1.0)) * RANGE * PROP * 0.500;
  boid -> position[X] = radius*cos(angle);
  boid -> position[Y] = radius*sin(angle);
}

void
initializeRectangle (struct Boid* const boid)
{
  boid -> position[X] = -PROP*RANGE*0.5 + RANDOM_0(PROP * RANGE);
  boid -> position[Y] = -PROP*RANGE*0.5 + RANDOM_0(PROP * RANGE);
}

void
initializeBoid (struct Boid* const boid)
{
  static unsigned int boidCount = 0;
  static unsigned int endoBoids = (unsigned)(ENDOPROP*N);
61
  initializeCircleCorrectly (boid);
Cassio Kirch's avatar
Cassio Kirch committed
62 63 64 65 66 67
  boid -> velocity[X]  = 0.0;
  boid -> velocity[Y]  = 0.0;
  boid -> boxID        = getBoxID (*boid);
  boid -> next         = NULL;
  boid -> previous     = NULL;

68
#if defined (ENDO_GAMMA) || defined (ECTO_GAMMA)
Cassio Kirch's avatar
Cassio Kirch committed
69
  boid -> gamma = 0.0;
70 71
#endif
  
Cassio Kirch's avatar
Cassio Kirch committed
72 73 74 75 76 77
  if (boidCount < endoBoids)
    boid -> type = ENDODERM;
  else
    boid -> type = ECTODERM;
  ++boidCount;
}