-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
197 lines (152 loc) · 4.59 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Build an image according to a logical rule</title>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
<script src="build_logical_expr.js" type="text/javascript"></script>
</head>
<body>
<p style="margin: 18px 10px; font-family: monospace;">
The variable is a single latin letter <br><br>
The binary functions: <br>
~, ¬ negation <br>
*, ∧, & conjunction <br>
+, ∨ disjunction <br>
^, ⨁ exclusive disjunction <br>
|, ↑ alternative denial <br>
/, ↓ joint denial <br>
->, → implication <br>
<-, ← converse implication <br>
=, ≡ biconditional <br>
</p>
<textarea id="expression" type="text" spellcheck="false" placeholder="insert your logical expression here">a^b</textarea>
<div class="main">
<p id="w_names" class="vars_names">a</p>
<p id="h_names" class="vars_names">b</p>
<canvas id="display" width="512" height="512"></canvas>
</div>
<footer> <a href="https:/github.com/Mihahanya"> 2022 Mihahanya </a> </footer>
</body>
<script>
function display(bin_img, element) {
var width_shape = bin_img[0].length, height_shape = bin_img.length
var canvas = document.getElementById(element)
var ctx = canvas.getContext("2d")
if (height_shape < width_shape) canvas.height = canvas.width/(width_shape/height_shape)
else canvas.height = canvas.width
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = "rgba(0, 0, 0, 1)"
var pixel_size = canvas.width / width_shape
for (var i=0; i<height_shape; i++) {
for (var j=0; j<width_shape; j++) {
if (bin_img[i][j] == 0)
ctx.fillRect(j*pixel_size, i*pixel_size, pixel_size, pixel_size)
}
}
}
display([[0, 1], [1, 0]], 'display') // init result
function unique(str) {
return Array.from(new Set(str))
}
function get_vars_vals_by_ind(num, vars_names) {
/* (4, 'abcd') to [{name: 'a', val: 0}, {name: 'b', val: 1}, {name: 'c', val: 0}, {name: 'd', val: 0}]
because bin(4) = 0b0100 */
if (vars_names == '') return []
num = num.toString(2)
num = '0'.repeat(vars_names.length-num.length) + num
var vars = []
for (const i in vars_names)
vars.push({name: vars_names[i], val: num[i]})
return vars
}
function build_binary_image(expression) {
var only_vars = expression.replaceAll(/\W|[0-1]/g, '')
var vars_names = unique(only_vars).sort()
var w_names = vars_names.filter((_, i) => i % 2 == 0)
var h_names = vars_names.filter((_, i) => i % 2 == 1)
var res = []
for (var i=0; i<2**h_names.length; i++) {
var h_vars = get_vars_vals_by_ind(i, h_names)
var l = []
for (var j=0; j<2**w_names.length; j++) {
var vars = h_vars.concat(get_vars_vals_by_ind(j, w_names))
var pix = calculate_logical_expression(expression, vars)
l.push(pix)
if (pix == null) { return null }
}
res.push(l)
}
return {res: res, w_names: w_names, h_names: h_names}
}
$('#expression').on('keydown keyup change', function() {
var res = build_binary_image($(this).val())
if (res != null) {
display(res.res, 'display')
$('#w_names').html(res.w_names); $('#h_names').html(res.h_names)
// Normal borders
$(this).attr('class', '')
$("#display").attr('class', '')
}
else {
// Red borders
$(this).attr('class', 'synt_err')
$('#display').attr('class', 'synt_err')
}
})
//console.log(calculate_logical_expression('a + b * (b -> b) + a * (b * a ^ (~a^b+b) -> b)', [{name:'a', val:0}, {name:'b', val:1}]))
//console.log(calculate_logical_expression('a+(~b^c)->c', [{name:'a', val: 1}, {name:'b', val:0}, {name:'c', val:1}]))
</script>
<style>
body {
background-color: whitesmoke;
}
textarea {
position: absolute;
transform: translate(-50%, 0);
top: 25px;
left: 50%;
padding: 10px 12px;
height: 20px;
width: 50vmin; max-width: 50vmin; min-width: 50vmin;
background-color: #00000000;
border: 1px solid;
box-shadow: 5px 5px 15px 0px #0000001f;
text-align: left;
font-size: .95rem;
font-family: monospace;
}
textarea:focus-visible {
outline: 0;
border: 1px solid #3f51b5;
}
.synt_err {
border: 1px solid #e91e63 !important;
}
.main {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
footer {
position: absolute;
bottom: 10px;
left: 50%;
transform: translate(-50%, -50%);
}
.vars_names {
position: absolute;
margin: -30px 0;
font-family: monospace;
}
#h_names {
margin: 12px -36px;
transform: rotate(90deg);
}
#display {
width: 25vmin;
border: 1px solid;
}
</style>
</html>