boid.c 1.56 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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;
}

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);
  initializeCircle (boid);
  boid -> velocity[X]  = 0.0;
  boid -> velocity[Y]  = 0.0;
  boid -> boxID        = getBoxID (*boid);
  boid -> next         = NULL;
  boid -> previous     = NULL;

  boid -> gamma = 0.0;
  if (boidCount < endoBoids)
    boid -> type = ENDODERM;
  else
    boid -> type = ECTODERM;
  ++boidCount;
}