forked from frontdevops/returnTrue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruth-table.js
74 lines (68 loc) · 1.63 KB
/
truth-table.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
const vals = [
["-Infinity", -Infinity ],
["-1", -1 ],
['"-1"', "-1"],
['""', ""],
["[[]]", [[]] ],
["[]", [] ],
["false", false ],
["0", 0 ],
["null", null ],
['"0"', "0" ],
["[0]", [0] ],
["[1]", [1] ],
['"1"', "1" ],
["1", 1 ],
["true", true ],
["Infinity", Infinity ],
["{}", {} ],
['"false"', "false" ],
['"true"', "true" ],
["undefined", undefined ],
["NaN", NaN ],
["\\t\\r\\n", "\t\r\n"]
];
const cmp = (v1, v2) => v1 == v2;
const canvas = document.getElementById("drawCanvas");
const ctx = canvas.getContext("2d");
const n = vals.length;
const r = 20; // diameter of grid squares
const p = 60; // padding space for labels
const f = 12; // font size
// color grid cells
for (let i = 0; i < n; i++) {
let v1 = vals[i][1];
for (let j = 0; j < n; j++) {
let v2 = vals[j][1],
eq = cmp(v1, v2);
ctx.fillStyle = eq ? "orange" : "white";
ctx.fillRect(p+i*r,p+j*r,r,r);
}
}
// draw labels
ctx.fillStyle = "black";
ctx.font = f + "px Helvetica";
for (let i = 0; i < n; i++) {
let s = vals[i][0],
w = ctx.measureText(s).width;
ctx.save();
ctx.translate(p+i*r+r/2-f*0.4,p-w-2);
ctx.rotate(3.14159/2);
ctx.fillText(s, 0, 0);
ctx.restore();
}
for (let i = 0; i < n; i++) {
let s = vals[i][0],
w = ctx.measureText(s).width;
ctx.fillText(s, p-w-2, p+i*r+r/2+f*0.4);
}
// draw grid lines
ctx.beginPath();
ctx.strokeStyle = "black";
for (let i = 0; i <= n; i++) {
ctx.moveTo(p+r*i, p);
ctx.lineTo(p+r*i, p+r*n);
ctx.moveTo(p, p+r*i);
ctx.lineTo(p+r*n, p+r*i);
}
ctx.stroke();