This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.js
324 lines (274 loc) · 5.99 KB
/
cipher.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
let inorout;
let share1;
let share2;
let animValue = 0; //animation of
let c = 20; //cols
let r = 20; //rows
let size = 10;
let canva;
let address = window.location.href;
function setup() {
if(address.search("c=") != -1){
c = 0;
let begin = address.search("c=")+2;
let end = address.search("&");
let factor = 1;
for(let i = end-1; i >= begin; i--){
c += address[i]*factor;
factor *= 10;
}
}
document.getElementById("colsText").value = c;
if(address.search("&r=") != -1){
r = 0;
let begin = address.search("r=")+2;
let end = address.length;
let factor = 1;
for(let i = end-1; i >= begin; i--){
r += address[i]*factor;
factor *= 10;
}
}
document.getElementById("rowsText").value = r;
//create background cavna
canva = createCanvas(3*c*size+2*size,r*size);
canva.parent('sketch-holder');
//tables creation
inorout = [...Array(c).keys()].map(i => Array(r));
share1 = [...Array(2*c).keys()].map(i => Array(2*r));
share2 = [...Array(2*c).keys()].map(i => Array(2*r));
cleanInorout();
cleanShares();
}
function draw(){
background(255);
stroke(0);
//drawing input/output
for(let i = 0; i < c; i++){
for(let j = 0; j < r; j++){
//inorout
let x = i * size;
let y = j * size;
if(inorout[i][j] == 1){
fill(0);
}
else{
fill(255);
}
rect(x,y,size-1,size-1);
}
}
//drawing shares
for(let i = 0; i < c*2; i++){
for(let j = 0; j < r*2; j++){
//share1
let x = i * (size/2);
let y = j * (size/2);
x += size*c + size;
if(share1[i][j] == 1){
fill(0);
}
else{
noFill();
}
rect(x,y,(size/2)-1,(size/2)-1);
}
}
for(let i = 0; i < c*2; i++){
for(let j = 0; j < r*2; j++){
//share2
let x = i * (size/2);
let y = j * (size/2);
x += 2*size*c + 2*size - animValue;
if(share2[i][j] == 1){
fill(0);
}
else{
noFill();
}
rect(x,y,(size/2)-1,(size/2)-1);
}
}
}
function mouseClicked(){
let xMatrixPos = floor(mouseX/size);
let yMatrixPos = floor(mouseY/size);
//marking pixels
if( xMatrixPos < c ){
if( yMatrixPos < r ){
if(inorout[xMatrixPos][yMatrixPos] == 0){
inorout[xMatrixPos][yMatrixPos] = 1;
}
else{
inorout[xMatrixPos][yMatrixPos] = 0
};
}
}
}
function cipher(){
cleanShares();
// [x*2][y*2] //uleft
// [x*2+1][y*2] //uright
// [x*2][y*2+1] //bleft
// [x*2+1][y*2+1] //bright
//checking values of input/output and ciphering onto shares
for(let i = 0; i < c; i++){
for(let j = 0; j < r; j++){
//black pixel
if(inorout[i][j] == 1){
switch(floor(random(6))){
//lines
//11
//22
case 0:
share1[i*2][j*2] = share1[i*2+1][j*2] = 1;
share2[i*2][j*2+1] = share2[i*2+1][j*2+1] = 1;
break;
//22
//11
case 1:
share1[i*2][j*2+1] = share1[i*2+1][j*2+1] = 1;
share2[i*2][j*2] = share2[i*2+1][j*2] = 1;
break;
//12
//12
case 2:
share1[i*2][j*2] = share1[i*2][j*2+1] = 1;
share2[i*2+1][j*2] = share2[i*2+1][j*2+1] = 1;
break;
//21
//21
case 3:
share1[i*2+1][j*2] = share1[i*2+1][j*2+1] = 1;
share2[i*2][j*2] = share2[i*2][j*2+1] = 1;
break;
//crosses
//12
//21
case 4:
share1[i*2][j*2] = share1[i*2+1][j*2+1] = 1;
share2[i*2+1][j*2] = share2[i*2][j*2+1] = 1;
break;
//21
//12
case 5:
share1[i*2+1][j*2] = share1[i*2][j*2+1] = 1;
share2[i*2][j*2] = share2[i*2+1][j*2+1] = 1;
break;
}
}
//white pixel
else{
switch(floor(random(10))){
//every 2 px
//lines
//OO
//xx
case 0:
share1[i*2][j*2] = share1[i*2+1][j*2] = share2[i*2][j*2] = share2[i*2+1][j*2] = 1;
break;
//xx
//OO
case 1:
share1[i*2][j*2+1] = share1[i*2+1][j*2+1] = share2[i*2][j*2+1] = share2[i*2+1][j*2+1] = 1;
break;
//Ox
//Ox
case 2:
share1[i*2][j*2] = share1[i*2][j*2+1] = share2[i*2][j*2] = share2[i*2][j*2+1] = 1;
break;
//xO
//xO
case 3:
share1[i*2+1][j*2] = share1[i*2+1][j*2+1] = share2[i*2+1][j*2] = share2[i*2+1][j*2+1] = 1;
break;
//crosses
//Ox
//xO
case 4:
share1[i*2][j*2] = share1[i*2+1][j*2+1] = share2[i*2][j*2] = share2[i*2+1][j*2+1] = 1;
break;
//xO
//Ox
case 5:
share1[i*2+1][j*2] = share1[i*2][j*2+1] = share2[i*2+1][j*2] = share2[i*2][j*2+1] = 1;
break;
//every 1 px
//lines
//12 21
//xx xx
case 6:
if(floor(random(2))){
share1[i*2][j*2] = share2[i*2+1][j*2] = 1;
}
else{
share2[i*2][j*2] = share1[i*2+1][j*2] = 1;
}
break;
//xx xx
//12 21
case 7:
if(floor(random(2))){
share1[i*2][j*2+1] = share2[i*2+1][j*2+1] = 1;
}
else{
share2[i*2][j*2+1] = share1[i*2+1][j*2+1] = 1;
}
break;
//1x 2x
//2x 1x
case 8:
if(floor(random(2))){
share1[i*2][j*2] = share2[i*2][j*2+1] = 1;
}
else{
share2[i*2][j*2] = share1[i*2][j*2+1] = 1;
}
break;
//x1 x2
//x2 x1
case 9:
if(floor(random(2))){
share1[i*2+1][j*2] = share2[i*2+1][j*2+1] = 1;
}
else{
share2[i*2+1][j*2] = share1[i*2+1][j*2+1] = 1;
}
break;
}
}
}
}
animValue = 0;
}
function overlay(){
animValue = c*size+size;
}
function cleanShares(){
//writing 'empty' values onto shares
for(let i = 0; i < c*2; i++){
for(let j = 0; j < r*2; j++){
share1[i][j] = 0;
share2[i][j] = 0;
}
}
}
function cleanInorout(){
//writing 'empty' values onto input/output
for(let i = 0; i < c; i++){
for(let j = 0; j < r; j++){
inorout[i][j] = 0;
}
}
}
function setColsRows(){
var cols = document.getElementById("colsText");
var rows = document.getElementById("rowsText");
let cIndex = address.search("c=")
if(cIndex != -1){
window.location.href = address.slice(0, cIndex-1) + "?c=" + cols.value + "&r=" + rows.value;
}
else{
window.location.href = address + "?c=" + cols.value + "&r=" + rows.value;
}
}