-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHw2Controller.js
More file actions
163 lines (163 loc) · 6.58 KB
/
Hw2Controller.js
File metadata and controls
163 lines (163 loc) · 6.58 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
// Copyright © Sam Savage 2016
var length = 400;
var Hw2Controller = (function (_super) {
__extends(Hw2Controller, _super);
function Hw2Controller(elementId) {
var _this = this;
_super.call(this, elementId);
this.currentRow = [];
this.data = [];
// http://stackoverflow.com/a/20066663
//this.currentRow = Array.apply(null, { length: length }).map(Function.call, Math.random);
for (var i = 0; i < length; ++i) {
//this.currentRow.push(Math.random() < 0.5);
this.currentRow.push((i == (length / 2)));
}
var svg = d3.select("main").append("canvas");
svg.attr("width", length * 2).attr("height", length * 20);
svg.on("mousemove", function () { return _this.onMouse(); });
this.svg = svg;
this.graphRow(this.currentRow, 0);
this.statsBox = d3.select("main").append("div");
this.statsBox.attr("id", "hw2stats");
return;
}
Hw2Controller.prototype.onMouse = function () {
var width = parseInt(this.svg.style("width"));
var height = parseInt(this.svg.style("height"));
var mouse_event = d3.event["currentTarget"];
if (mouse_event) {
var mouse_pos = d3.mouse(mouse_event);
var col_number = Math.floor(mouse_pos[0] / 2);
var row_number = Math.floor(mouse_pos[1] / 2);
var width = 3;
if (row_number < this.data.length) {
var stats = this.getStats(this.data, row_number, col_number, width);
this.printStats(row_number, col_number, width, stats);
}
}
return;
};
Hw2Controller.prototype.dostuff = function () {
var next_row = this.nextRow(this.currentRow);
this.data.push(this.currentRow);
this.graphRow(next_row, this.data.length);
this.currentRow = next_row;
return;
};
Hw2Controller.prototype.realMod = function (n, m) {
// javascript mod doesnt work with negative numbers
//http://stackoverflow.com/a/17323608
return ((n % m) + m) % m;
};
Hw2Controller.prototype.nextRow = function (row) {
var rval = Array(row.length);
for (var i = 0; i < row.length; ++i) {
var previous = row[this.realMod(i - 1, row.length)];
var current = row[this.realMod(i, row.length)];
var next = row[this.realMod(i + 1, row.length)];
rval[i] = this.lookupRule(previous, current, next);
}
return rval;
};
Hw2Controller.prototype.lookupRule = function (p, c, n) {
// https://en.wikipedia.org/wiki/Rule_110
// current state: 111 110 101 100 011 010 001 000
// new state: 0 1 1 0 1 1 1 0
var states = [false, true, true, true, false, true, true, false];
var index = (p ? 4 : 0) + (c ? 2 : 0) + (n ? 1 : 0);
return (index < states.length ? states[index] : false);
// or return this.poly(p?1:0,c?1:0,n?1:0) == 1;
};
Hw2Controller.prototype.graphRow = function (row, yIndex) {
var total_width = parseInt(this.svg.style("width"));
var rec_width = total_width / row.length;
var y_index = yIndex * rec_width;
var context = this.svg.node().getContext("2d");
row.forEach(function (d, i) {
context.beginPath();
context.rect(i * rec_width, y_index, rec_width, rec_width);
if (row[i]) {
context.fillStyle = "#000";
context.fill();
}
context.closePath();
});
};
Hw2Controller.prototype.getStats = function (data, rowIndex, colIndex, width) {
var rval = Array(Math.pow(2, width));
for (var i = 0; i < rowIndex; ++i) {
var row = data[i];
var local_width = Math.min(Math.floor(width), row.length);
var start_index = colIndex - Math.floor(local_width / 2);
var value = this.getNumber(row, start_index, local_width);
rval[value] = (rval[value] ? rval[value] + 1 : 1);
}
return rval;
};
Hw2Controller.prototype.getNumber = function (data, start, length) {
var rval = 0;
for (var i = start; i < (start + length); ++i) {
rval += (data[this.realMod(i, data.length)] ? (1 << (i - start)) : 0);
}
return rval;
};
Hw2Controller.prototype.printStats = function (row, col, width, stats) {
this.statsBox.selectAll("p").remove();
var count_p = this.statsBox.append("p");
var statstr = "count for row " + row + ", col " + col + " (length " + width + "): ";
count_p.text(statstr + stats.toString());
var total_count = 0;
var weights = Array(stats.length);
var entropy = 0;
for (var i = 0; i < stats.length; ++i) {
if (stats[i]) {
weights[i] = stats[i] / row;
entropy += weights[i] * (Math.log(weights[i]) / Math.log(2));
}
total_count += (stats[i] ? stats[i] : 0);
}
if (total_count != row) {
this.statsBox.append("p").text("total count != row: " + total_count.toString() + " != " + row.toString());
alert("totalcount != row");
}
var fract_p = this.statsBox.append("p");
statstr = "weights: [";
for (var i = 0; i < weights.length; ++i) {
if (weights[i]) {
statstr += weights[i].toFixed(3);
}
if (i < weights.length - 1) {
statstr += ",";
}
}
statstr += "]";
fract_p.text(statstr);
var entropy_p = this.statsBox.append("p");
entropy_p.text("shannon's entropy: " + (0 - entropy));
return;
};
// taken from Blair's ALife1Dim Java program
Hw2Controller.prototype.binarize = function (x) {
var acc = 0;
var den = 0.5;
for (var k = 0; k < 14; ++k) {
if (acc + den <= x) {
acc += den;
}
den *= 0.5;
}
return acc;
};
// taken from Blair's ALife1Dim Java program
Hw2Controller.prototype.poly = function (u, x, v) {
return this.binarize(0.5 - 0.5 * Math.cos(Math.PI * (x + v - x * v - u * x * v)));
};
return Hw2Controller;
})(BaseTimer);
//# sourceMappingURL=Hw2Controller.js.map