-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByte.cpp
More file actions
101 lines (82 loc) · 3.46 KB
/
Byte.cpp
File metadata and controls
101 lines (82 loc) · 3.46 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Byte.h"
#define GENE_SIZE 1
//-------------------------------------------------------
// constructor - initializea byte object
Byte::Byte(){
geneSize = GENE_SIZE;
fitness = 0;
}
//-------------------------------------------------------
// two functions can have the same name
// but different inputs and behavior.
// these achieve the same goal
// setup Byte with existing DNA or with genes to create new DNA
//-------------------------------------------------------
void Byte::setup(DNA _dna, int _bitNum){
bitNum = _bitNum;
dna = _dna;
geneSize = dna.geneSize;
numOfGenes = dna.numOfGenes;
calcPhenotype();
}
void Byte::setup(int _numOfGenes, int _bitNum){
bitNum = _bitNum;
numOfGenes = _numOfGenes;
dna.setup(numOfGenes, geneSize);
calcPhenotype();
}
//-------------------------------------------------------
// decode the normalized genotype data into scaled phenotype data
void Byte::calcPhenotype(){
startLength = ofMap(dna.genes[0],0,1, 150, 200);
startTheta = ofMap(dna.genes[1],0,1, 0, 40);
startWidth = ofMap(dna.genes[2],0,1, 2, 20);
minLength = ofMap(dna.genes[3],0,1, 13, 20);
branchReduce = ofMap(dna.genes[4],0,1, 0.65, 0.7);
thetaVariance = ofMap(dna.genes[5],0,1, 0, 50);
reduceVariance = ofMap(dna.genes[6],0,1, 0, 0.1);
startAngle = ofMap(dna.genes[7],0,1, 0, 30);
erosionFactor = ofMap(dna.genes[8],0,1, 0.3, 0.5);
leafSize = ofMap(dna.genes[9],0,1,10,13);
leafColorA = 150;
leafColorB = 75;
seed = ofRandom(1000,65000);
}
//-------------------------------------------------------
void Byte::branch(float b_length, float theta, float b_width, int depthRemaining, vector<bool> _bitStates){
// limit to 8 levels deep (1 byte == 8 bits)
if(depthRemaining > 0){
ofSetColor(leafColorB,leafColorA);
// current bit's path from previous bit
if(depthRemaining != 8) ofDrawLine(0,0,0,-b_length);
// if bitstate is off - color white, otherwise color gray
if(_bitStates[8-depthRemaining] == true) ofSetColor(255,150);
// bit
ofDrawEllipse(0,-b_length,leafSize+depthRemaining,leafSize+depthRemaining);
// 'child' bit 0
ofPushMatrix();
ofTranslate(0,-b_length);
ofRotate(-theta - (ofNoise(ofGetFrameNum()/(10*b_length))));
ofRotate(-theta - (ofNoise(ofGetFrameNum()/thetaVariance*0.5, ofGetFrameNum()/b_length)));
ofRotate(-theta - (ofNoise(ofGetFrameNum()/thetaVariance*0.5, ofGetFrameNum()/b_length)));
branch(b_length * (branchReduce + ofRandom(-reduceVariance, reduceVariance)), theta, b_width * erosionFactor, depthRemaining - 1, _bitStates);
ofPopMatrix();
// 'child' bit 1
ofPushMatrix();
ofTranslate(0,-b_length);
ofRotate(theta + (ofNoise(ofGetFrameNum()/(20*b_length))));
ofRotate(theta + (ofNoise(ofGetFrameNum()/thetaVariance, ofGetFrameNum()/b_length)));
ofRotate(theta + (ofNoise(ofGetFrameNum()/(10*b_length))));
branch(b_length * (branchReduce + ofRandom(-reduceVariance, reduceVariance)), theta, b_width * erosionFactor, depthRemaining - 1, _bitStates);
ofPopMatrix();
}
}
//--------------------------------------------------------
// Draw the bit
void Byte::draw(int x, int y, vector<bool> _bitStates){
ofSeedRandom(seed);
ofPushMatrix();
ofRotate(ofRandom(-startAngle/10, startAngle/10));
branch(startLength, startTheta,startWidth, 8, _bitStates);
ofPopMatrix();
}