-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface.js
95 lines (81 loc) · 2.87 KB
/
face.js
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
// Daniel Shiffman
// https://www.kadenze.com/courses/the-nature-of-code
// http://natureofcode.com/
// Session 5: Evolutionary Computing
// Interactive Selection
// http://www.genarts.com/karl/papers/siggraph91.html
// The class for our "face", contains DNA sequence, fitness value, position on screen
// Fitness Function f(t) = t (where t is "time" mouse rolls over face)
// Create a new face
function Face(dna_, x_, y_) {
this.rolloverOn = false; // Are we rolling over this face?
this.dna = dna_; // Face's DNA
this.x = x_; // Position on screen
this.y = y_;
this.wh = 70; // Size of square enclosing face
this.fitness = 1; // How good is this face?
// Using java.awt.Rectangle (see: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Rectangle.html)
this.r = new Rectangle(this.x-this.wh/2, this.y-this.wh/2, this.wh, this.wh);
// Display the face
this.display = function() {
// We are using the face's DNA to pick properties for this face
// such as: head size, color, eye position, etc.
// Now, since every gene is a floating point between 0 and 1, we map the values
var genes = this.dna.genes;
var r = map(genes[0],0,1,0,70);
var c = color(genes[1],genes[2],genes[3]);
var eye_y = map(genes[4],0,1,0,5);
var eye_x = map(genes[5],0,1,0,10);
var eye_size = map(genes[5],0,1,0,10);
var eyecolor = color(genes[4],genes[5],genes[6]);
var mouthColor = color(genes[7],genes[8],genes[9]);
var mouth_y = map(genes[5],0,1,0,25);
var mouth_x = map(genes[5],0,1,-25,25);
var mouthw = map(genes[5],0,1,0,50);
var mouthh = map(genes[5],0,1,0,10);
// Once we calculate all the above properties, we use those variables to draw rects, ellipses, etc.
push();
translate(this.x, this.y);
noStroke();
// Draw the head
fill(c);
ellipseMode(CENTER);
ellipse(0, 0, r, r);
// Draw the eyes
fill(eyecolor);
rectMode(CENTER);
rect(-eye_x, -eye_y, eye_size, eye_size);
rect( eye_x, -eye_y, eye_size, eye_size);
// Draw the mouth
fill(mouthColor);
rectMode(CENTER);
rect(mouth_x, mouth_y, mouthw, mouthh);
// Draw the bounding box
stroke(0.25);
if (this.rolloverOn) fill(0, 0.25);
else noFill();
rectMode(CENTER);
rect(0, 0, this.wh, this.wh);
pop();
// Display fitness value
textAlign(CENTER);
if (this.rolloverOn) fill(0);
else fill(0.25);
text('' + floor(this.fitness), this.x, this.y+55);
}
this.getFitness = function() {
return this.fitness;
}
this.getDNA = function() {
return this.dna;
}
// Increment fitness if mouse is rolling over face
this.rollover = function(mx, my) {
if (this.r.contains(mx, my)) {
this.rolloverOn = true;
this.fitness += 0.25;
} else {
this.rolloverOn = false;
}
}
}