-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPatternMatching.js
432 lines (348 loc) · 11.2 KB
/
PatternMatching.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
function appInit(){
var userGraph = document.getElementById("userGraph");
var masterGraph = document.getElementById("masterGraph");
var answerTxt = document.getElementById("answer");
/*Define global variables*/
//data for the marks to be rendered on canvases
markData = {
userGraph: { normal: [], //Members: x, y
wide: [], //Members: x
},
masterGraph: { normal: [], //Members: x, y
wide: [], //Members: x
index: [] //Members:
},
};
//Corresponds to settings in control panel
appSettings = {
sensitivity: 1,
rings: 31,
};
//Values to be displayed
answer = {
yearStart: 0,
yearEnd: 0,
}
//misc global variables
data = {
masterLengthFactor: 2,
masterYearStart: 0,
targetRingWidth: 50,
}
graphUnit = 6;
//Position draggable elements
answerTxt.style.left = "45px";
answerTxt.style.top = "30px";
userGraph.style.left = "15px";
userGraph.style.top = "50px";
populateRings();
renderGraphPaper(userGraph);
renderGraphPaper(masterGraph);
writeAnswer("undershoot");
redraw();
}
function renderGraphPaper(canvas) {
var height = canvas.height;
var width = canvas.width;
var mag = appSettings.graphMag;
var ctx = canvas.getContext("2d");
var i, txt, txtWidth, temp;
if(canvas == document.getElementById("userGraph")) {
//highlight upper 1/3 in grey
ctx.beginPath();
ctx.fillStyle = "rgb(204,204,204)";
ctx.fillRect(0,0, width, height/3);
ctx.closePath();
}
//Draw graph squares
ctx.lineWidth = 1;
//Draw vertical lines
//Leave 5 units for margin
for (i = graphUnit*5; i < width; i+=graphUnit) {
ctx.beginPath();
ctx.moveTo(i+.5, 0);
ctx.lineTo(i+.5, height);
ctx.strokeStyle = "rgb(102,255,102)";
ctx.stroke();
ctx.closePath();
}
//Draw horizontal lines
for (i = 0; i < height; i+=graphUnit) {
ctx.beginPath();
ctx.moveTo(graphUnit*5, i+.5); //Leave room for margin
ctx.lineTo(width, i+.5);
ctx.strokeStyle = "rgb(102,255,102)";
ctx.stroke();
ctx.closePath();
}
//Draw Dark Green Lines
for (i = graphUnit*5; i < width; i+=graphUnit*5) {
ctx.beginPath();
ctx.moveTo(i+.5, 0);
ctx.lineTo(i+.5, height);
ctx.strokeStyle = "rgb(000,204,051)";
ctx.stroke();
ctx.closePath();
}
for (i = 0; i < height; i+=graphUnit*5) {
ctx.beginPath();
ctx.moveTo(graphUnit*5, i+.5);
ctx.lineTo(width, i+.5);
ctx.strokeStyle = "rgb(000,204,051)"
ctx.stroke();
ctx.closePath();
}
//Draw special margins; depend on which graph it is
switch(canvas){
case document.getElementById("userGraph"):
//Write Ring Numbers
for (i=graphUnit*5; i<width; i+=10*graphUnit) {
ctx.font = "italic bold 15px Times";
ctx.fillStyle = "rgb(0,0,0)";
txt = i/graphUnit - 5;
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, i-txtWidth/2, 4*graphUnit + 5);
}
break;
case document.getElementById("masterGraph"):
//Draw black horizontal lines
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(graphUnit*5, graphUnit*10 + .5);
ctx.lineTo(width, graphUnit*10 + .5);
ctx.moveTo(graphUnit*5, graphUnit*20 + .5);
ctx.lineTo(width, graphUnit*20 + .5);
ctx.strokeStyle = "rgb(0,0,0)";
ctx.stroke();
ctx.closePath();
//Write Year Numbers
for (i=graphUnit*10; i<width; i+=10*graphUnit) {
ctx.font = "italic bold 15px Times";
temp = data.masterYearStart + i/graphUnit - 5;
if(temp%100 != 0){temp = temp%100;}
txt = temp;
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, i-txtWidth/2, 12*graphUnit);
}
//Note halfway mark on index plot
ctx.font = "bold 18px Times";
txt = "1.0";
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, 2.5*graphUnit - txtWidth/2, 21*graphUnit);
break;
}
}
function redraw(){
var userGraph = document.getElementById("userGraph");
var masterGraph = document.getElementById("masterGraph");
var ctxU = userGraph.getContext("2d");
var ctxM = masterGraph.getContext("2d");
var i, txt, txtWidth, x, y;
ctxU.clearRect(0, 0, userGraph.width, userGraph.height);
renderGraphPaper(userGraph);
ctxM.clearRect(0, 0, masterGraph.width, masterGraph.height);
renderGraphPaper(masterGraph);
//Make normal marks
ctxU.lineWidth = 3;
for(i = 0; i < markData.userGraph.normal.length; ++i) {
ctxU.beginPath();
ctxU.moveTo(markData.userGraph.normal[i].x+.5, Math.floor(userGraph.height - markData.userGraph.normal[i].y ) -.5);
ctxU.lineTo(markData.userGraph.normal[i].x+.5, userGraph.height);
ctxU.strokeStyle = "rgb(0,0,0)";
ctxU.stroke();
ctxU.closePath();
}
for(i = 0; i < markData.masterGraph.normal.length; ++i) {
ctxM.beginPath();
ctxM.moveTo(markData.masterGraph.normal[i].x+.5, markData.masterGraph.normal[i].y+1.5);
ctxM.lineTo(markData.masterGraph.normal[i].x+.5, 0);
ctxM.strokeStyle = "rgb(0,0,0)";
ctxM.stroke();
ctxM.closePath();
}
//Wide Marks
for(i = 0; i < markData.userGraph.wide.length; ++i) {
ctxU.font = "bold 16px Times ";
ctxU.fillStyle = "rgb(0,0,0)";
txt = "b";
txtWidth = ctxU.measureText(txt).width;
ctxU.fillText(txt, markData.userGraph.wide[i].x - txtWidth/2, 6.5*graphUnit);
}
for(i = 0; i < markData.masterGraph.wide.length; ++i) {
ctxM.font = "bold 16px Times";
ctxM.fillStyle = "rgb(0,0,0)";
txt = "b";
txtWidth = ctxM.measureText(txt).width;
ctxM.fillText(txt, markData.masterGraph.wide[i].x - txtWidth/2, 9.5*graphUnit);
}
//Master Index Plot
ctxM.lineWidth = 1;
for(i = 1; i < markData.masterGraph.index.length; ++i){
ctxM.beginPath();
ctxM.strokeStyle = "rgb(0,0,0)";
if(i == 1){
x = 5*graphUnit;
y = (2-markData.masterGraph.index[i-1])*graphUnit*10 + 10*graphUnit;
}
ctxM.moveTo(x+.5, y+.5);
x += graphUnit;
y = (2-markData.masterGraph.index[i])*graphUnit*10 + 10*graphUnit;
ctxM.lineTo(x+.5, y+.5);
ctxM.stroke();
ctxM.closePath();
}
}
function populateRings() {
var i, masterYearStartSeed, coreStart, newMark, newMarkUser, newX, newY, newWidth, year, falseYear;
var color = [];
var falses = 0; var absents = 0;
var coreLength = 0;
var index = markData.masterGraph.index;
var masterNormal = markData.masterGraph.normal;
var indexMin = 100;
var indexMax = 0;
var absoluteValueCutoff = 2;
var firstDifferenceCutoff = 2;
var masterBoneWideCutoff = 0.95;
var numMinPixel = 0;
//Populate Master Plot
masterYearStartSeed = Math.random() * 0.75 + .05;
index.length = data.masterLengthFactor*appSettings.rings; //index holds all ring data
//Randomly set start year of master plot
data.masterYearStart = Math.floor(1998 - index.length - masterYearStartSeed * 100);
data.masterYearStart -= Math.floor(1998 - index.length - masterYearStartSeed * 100)%10 - 5;
coreStart = Math.random() * 0.75 + 0.05;
answer.yearStart = data.masterYearStart + Math.floor(coreStart*index.length);
//Randomly set ring widths
for(i = 0; i < index.length; ++i) {
index[i] = 0;
//Width is averaged according to sensitivity
for(var replicate = 0; replicate<appSettings.sensitivity; ++replicate){
index[i] += Math.random()*2;
}
index[i] /= appSettings.sensitivity;
//Keep track of maximum and minimum indices
if(index[i] < indexMin) {
indexMin = index[i];
}
else if (index[i] > indexMax) {
indexMax = index[i];
}
}
for(i = 0; i < index.length; ++i) {
if(i > 0 && index[i] < (1-(1-indexMin)/absoluteValueCutoff) ||
index[i]-index[i-1] < (indexMin-indexMax)/firstDifferenceCutoff){
newX = (i+5)*graphUnit;
newY = 0;
if(absoluteValueCutoff>1) {
newY = 10 - index[i]/(1-(1-indexMin)/absoluteValueCutoff)*10;
}
if(firstDifferenceCutoff > 1 && index[i] < index[i-1]) {
newY = Math.max(newY, 10-((index[i] - index[i-1]) - (indexMin-indexMax)) /
((indexMin-indexMax)/firstDifferenceCutoff-(indexMin-indexMax))*10);
}
newMark = {x:newX, y:newY*graphUnit, year:i+data.masterYearStart};
masterNormal.push(newMark);
//Make marks on userGraph
if(i >= Math.floor(coreStart*index.length) && i < Math.floor(coreStart * index.length) + appSettings.rings) {
newX = ((i-Math.floor(coreStart*index.length) + 5) * graphUnit);
newMarkUser = {x:newX, y:newY*graphUnit};
markData.userGraph.normal.push(newMarkUser);
}
}
if(index[i] > masterBoneWideCutoff*indexMax) {
newX = (5 + i)*graphUnit;
markData.masterGraph.wide.push({x:newX});
//Make marks on userGraph
if(i >= Math.floor(coreStart*index.length) && i < Math.floor(coreStart * index.length) + appSettings.rings) {
newX = ((i-Math.floor(coreStart*index.length) + 5) * graphUnit);
markData.userGraph.wide.push({x:newX});
}
}
}
}
function checkAnswer() {
var userGraphLeft = parseInt(document.getElementById("userGraph").style.left);
var masterGraphLeft = parseInt(document.getElementById("masterGraph").style.left);
var tolerance = 0.75;
if((userGraphLeft - masterGraphLeft)/graphUnit > answer.yearStart - data.masterYearStart - tolerance) {
if((userGraphLeft - masterGraphLeft)/graphUnit < answer.yearStart - data.masterYearStart + tolerance) {
writeAnswer("correct");
}
}
if((userGraphLeft - masterGraphLeft)/graphUnit < answer.yearStart - data.masterYearStart - tolerance) {
writeAnswer("undershoot");
}
if((userGraphLeft - masterGraphLeft)/graphUnit > answer.yearStart - data.masterYearStart + tolerance) {
writeAnswer("overshoot");
}
}
function writeAnswer(position) {
var answerTxt = document.getElementById("answer");
switch(position) {
case "undershoot":
answerTxt.style.color = "rgb(0,0,255)";
answerTxt.innerHTML = "No match here.";
break;
case "overshoot":
answerTxt.style.color = "rgb(255,0,255)";
answerTxt.innerHTML = "No match here.";
break;
case "correct":
answerTxt.style.color = "rgb(255,0,0)";
answerTxt.innerHTML = "Here's the match!";
break;
}
}
//Mouse Action Functions
function handleClick(e) {
var targ = e.target ? e.target : e.srcElement;
if(e.offsetY < targ.height / 3) {
startDrag(e);
}
}
function startDrag(e) {
// IE uses srcElement, others use target
var targ = e.target ? e.target : e.srcElement;
document.dragTarg = targ;
// calculate event X, Y coordinates
offsetX = e.clientX;
offsetY = e.clientY;
// assign default values for top and left properties
if(targ.style.left == "") { targ.style.left='0px'};
if(targ.style.top == "") { targ.style.top='0px'};
// calculate integer values for top and left
// properties
coordX = parseInt(targ.style.left);
coordY = parseInt(targ.style.top);
// move element
document.onmouseup = stopDrag;
if (targ == document.getElementById("coreStrip")){ document.onmousemove = dragHorizLimited;}
else {document.onmousemove=dragHoriz;}
return false;
}
function dragHoriz(e) {
var answer = document.getElementById("answer");
var targ = document.dragTarg;
// move element AND answer text
targ.style.left=coordX+e.clientX-offsetX+'px';
answer.style.left = coordX+e.clientX-offsetX+5*graphUnit+'px';
limitDrag(e);
checkAnswer();
return false;
}
function limitDrag(e) {
var targ = document.dragTarg;
if(parseInt(targ.style.left) + parseInt(targ.width) < 50) {
targ.style.left=50 - parseInt(targ.width) + "px";
}
if(parseInt(targ.style.left) > 420){
targ.style.left = "420px";
}
return false;
}
function stopDrag() {
document.onmousemove = null;
document.onmouseup = null;
document.dragTarg = null;
}