-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenome.c
64 lines (57 loc) · 1.52 KB
/
genome.c
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
63
64
#include <stdlib.h>
#include <math.h>
#include "genome.h"
/*
* Pick a integer between [min;max[
*/
int rand_between(int min, int max)
{
return rand()%(max-min) +min;
}
/*
* Translate a gene to make a protein
* (Return a circle definition made from a gene)
*/
void translate_gene(Gene gene, int screen_width, int screen_height, Protein* protein)
{
protein->x = (int)((float)gene.x / 255 * screen_width);
protein->y = (int)((float)gene.y / 255 * screen_height);
protein->color = gene.color;
//The radius is actually proportional to the half of screen diagonal
protein->length = (int)((float)gene.length / 255 * sqrt(screen_width * screen_width + screen_height * screen_height)) / 16;
}
/*
* Randomize a single gene
*/
void randomize_gene(Gene* gene)
{
gene->x = rand_between(0, 256);
gene->y = rand_between(0, 256);
Color c;
c.r = rand_between(0, 256);
c.g = rand_between(0, 256);
c.b = rand_between(0, 256);
c.a = rand_between(0, 256);
gene->color = c;
gene->length = rand_between(0, 256);
}
/*
* Mutate a single gene
*/
void mutate_gene(Gene* gene)
{
if(rand() % 1000 == 0 )
gene->x = rand_between(0,256);
if(rand() % 1000 == 0)
gene->y = rand_between(0,256);
if(rand() % 1000 == 0)
gene->color.r = rand_between(0,256);
if(rand() % 1000 == 0)
gene->color.g = rand_between(0,256);
if(rand() % 1000 == 0)
gene->color.b = rand_between(0,256);
if(rand() % 1000 == 0)
gene->color.a = rand_between(0,256);
if(rand() % 500 == 0)
gene->length = rand_between(0, 256);
}