-
Notifications
You must be signed in to change notification settings - Fork 0
/
sixes.js
293 lines (246 loc) · 6.77 KB
/
sixes.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
var bkg; var outside; var line;
var RADIUS; var histep;
var field; //replace old w a hash of the board, palette, subfields
var fieldsize;
var ORIGIN;
var coordString = function (x,y,z) {
return parseInt(x).toString()+","+parseInt(y).toString()+","+parseInt(z).toString();
}
//CELL OBJECT
var Cell = function (x,y,z) {
this.x = x;this.y = y;this.z = z;
this.radius = RADIUS;
this.color_=bkg; this.line=line;
var high = sqrt(3)/2 * (2*this.radius);
var wide = this.radius * 2;
this.high = high; this.wide = wide;
if ((x+y+z) !== 0) {
this.exists = false;
} else {
var x0 = width / 2;
var y0 = height / 2;
var cartX = x0 + (this.x * wide * 0.75);
var cartY = y0 + ( ( this.z - this.y ) * high * 0.5);
};
//console.log("cartX "+cartX+" cartY "+cartY+" w h "+width);
if (cartX > width || cartY > height || cartX < 0 || cartY < 0) {
this.exists = false;
} else {
this.cx = cartX;
this.cy = cartY;
this.exists = true;
};
this.n = coordString(x,y+1,z-1);
this.ne = coordString(x+1, y, z-1);
this.se = coordString(x+1, y-1, z);
this.s = coordString(x, y-1, z+1);
this.sw = coordString(x-1, y, z-1);
this.nw = coordString(x-1, y+1, z);
};
Cell.prototype.setColor = function (newcol) {
this.color_ = newcol;
this.create();
};
Cell.prototype.setLineColor = function (newline) {
this.line = newline;
this.create();
this.line = line;
};
Cell.prototype.setRadius = function (newrad) {
this.radius = newrad;
};
Cell.prototype.create = function () {
if (!this.exists) {return false};
stroke(this.line);
fill(this.color_);
var angle = radians(60);
var sx; var sy;
beginShape();
for (var a = 0; a < 6; a++) {
sx = this.cx + (cos(a*angle) * this.radius);
sy = this.cy + (sin(a*angle) * this.radius);
vertex(sx,sy);
};
endShape(CLOSE);
return true;
};
Cell.prototype.reset = function () {
this.color_ = bkg;
this.occupant = null;
return create();
};
Cell.prototype.findDistanceTo = function (dest) {
return max(abs(this.x - dest.x), abs(this.y - dest.y), abs(this.z - dest.z));
};
Cell.prototype.lerpTo = function (dest, t) {
return [this.x + (dest.x - this.x) * t, this.y + (dest.y - this.y) * t, this.z + (dest.z - this.z) * t];
};
Cell.prototype.drawLineTo = function (dest) {
var n = this.findDistanceTo(dest);
var results = []; var lerp = [];
for (var i = 0; i < n + 1; i++) {
lerp = this.lerpTo(dest, (1.0/n) * i);
results.push(this.cubeRound(lerp[0],lerp[1],lerp[2]));
};
return results;
};
Cell.prototype.findPathTo = function (dest) {
var frontier = [];
var came_from = {};
frontier.push(this);
came_from[this] = null;
var curr; var neighbs;
while (frontier.length > 0) {
curr = frontier.shift();
neighbs = curr.neighbors();
neighbs.forEach(function(next) {
var p1block = sets['p1-locs'].includes(next.coords());
var p2block = sets['p2-locs'].includes(next.coords());
if (!came_from.keys().includes(next) && !p1block && !p2block && next != null) {
frontier.push(next);
came_from[next] = curr;
};
}, this);
};
curr = dest;
var path = [];
path.push(curr);
while (curr !== this) {
curr = came_from[curr];
path.push(curr);
};
path.reverse();
return path;
};
Cell.prototype.cubeRound = function(x_,y_,z_,fieldname) {
if (x_ === null || y_ === null || z_ === null || fieldname === null) {
console.log("Missing argument cubeRound")
}
var rx = round(x_);
var ry = round(y_);
var rz = round(z_);
//console.log(coordString(rx,ry,rz));http://ebook.3m.com/library/multnomahcountylibrary/Featured?media=all&src=lib
var dx = abs(rx - x_);
var dy = abs(ry - y_);
var dz = abs(rz - z_);
if (dx > dy && dx > dz) {
rx = -ry - rz;
} else if (dy > dz) {
ry = -rx - rz;
} else {
rz = -rx - ry;
};
if (Object.keys(fields[fieldname]).includes(coordString(rx,ry,rz))) {
return fields[fieldname][coordString(rx,ry,rz)];
} else {return null;};
};
Cell.prototype.absToMap = function(cx_,cy_,fieldname) {
//console.log(cx_+","+cy_);
cx_ = cx_ - (width/2);
cy_ = cy_ - (height/2);
var q = (cx_ * (2.0/3.0)) / this.radius;
var s = (-cx_ / 3.0 + (sqrt(3)/3.0) * cy_) / this.radius;
var r = -q - s;
return this.cubeRound(q,r,s,fieldname);
};
Cell.prototype.neighbors = function(fieldname) {
var neighbs = [this.n,this.ne,this.se,this.s,this.sw,this.nw];
var out = []; var x; var check;
for (var i=0;i<6;i++) {
x = neighbs[i];
check = fields[fieldname][coordString(x[0],x[1],x[2])];
if (check !== null) {
out[i] = check;
} else {
out[i] = null;
};
};
return out;
};
Cell.prototype.coords = function() {
return coordString(this.x,this.y,this.z);
};
Cell.prototype.colorProfile = function () {
var profile = {};
profile['fill'] = this.color_;
profile['border'] = this.line;
profile['token'] = this.occupant;
return profile;
};
// ************END CELL OBJECT
// ************BEGIN COLORPATH OBJECT
var ColorPath = function (total) {
this.total = total;
this.step = 1/total;
console.log(total);
this.i = 0;
var middle = createVector(0,0,0);
this.begin = this.genPoint(middle);
this.end = this.genPoint(this.begin);
this.curr = this.begin;
console.log(this.begin, this.end);
}
ColorPath.prototype.genPoint = function (invec) {
var outvec = invec;
var a, b, c;
while (outvec.dist(invec) < 120) {
a = round(random(255));
b = round(random(255));
c = round(random(255));
outvec = createVector(a,b,c);
};
return outvec;
};
ColorPath.prototype.genColor = function () {
this.i += 1;
var a = round(random(30)) - 15;
var b = round(random(30)) - 15;
var c = round(random(30)) - 15;
var ranvec = createVector(a,b,c);
var stepvec = p5.Vector.lerp(this.begin,this.end,this.i*this.step);
//console.log(this.step);
stepvec.add(ranvec);
this.curr = stepvec;
return (color(this.curr.x,this.curr.y,this.curr.z));
};
//*****************SETUP****************
function setup() {
if (random()>0.5) {
colorMode(HSB,255);
}
var g = createCanvas(800,800);
fill(40,100,255);
background(60);
stroke(0);
bkg = color(102); outside = color(60); line = color(0); highlight = color(144);
RADIUS = 20;
//board init
fieldsize = 9;
field = {};
for (var aa = -fieldsize; aa < fieldsize + 1; aa++) {
for (var ab = -fieldsize; ab < fieldsize + 1; ab++) {
for (var ac = -fieldsize; ac < fieldsize + 1; ac++) {
if ((aa+ab+ac) === 0) {
field[coordString(ab,aa,ac)] = new Cell (ab,aa,ac);
field[coordString(ab,aa,ac)].create();
};
};
};
};
var path = new ColorPath(Object.keys(field).length);
Object.keys(field).forEach(function (c) {
var cor = path.genColor();
field[c].setColor(cor);
field[c].setLineColor(cor);
})
ORIGIN = field[coordString(0,0,0)];
};
//*****************END SETUP*****************
//*****************BEGIN DRAW****************
function draw() {
update(mouseX,mouseY);
//testing
};
function update(x,y) {
};
//*****************END DRAW********************