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
/
decipher.js
182 lines (148 loc) · 3.65 KB
/
decipher.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
let inorout;
let sh1Arr;
let sh2Arr;
let animValue = 0; //animation of
let c = 20; //cols
let r = 20; //rows
let size = 10;
let canva;
let raisedError = 0;
let address = window.location.href;
function setup() {
}
function draw(){
background(255);
stroke(0);
if(typeof sh1Arr !== 'undefined' && typeof(sh2Arr !== 'undefined')){
if(typeof inorout !== 'undefined'){
//drawing input/output
for(let i = 0; i < c/2; i++){
for(let j = 0; j < r/2; 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; i++){
for(let j = 0; j < r; j++){
//share1
let x = i * (size/2);
let y = j * (size/2);
x += size*c/2 + size;
if(sh1Arr[i][j] == 1){
fill(0);
}
else{
noFill();
}
rect(x,y,(size/2)-1,(size/2)-1);
}
}
for(let i = 0; i < c; i++){
for(let j = 0; j < r; j++){
//share2
let x = i * (size/2);
let y = j * (size/2);
x += size*c + 2*size - animValue;
if(sh2Arr[i][j] == 1){
fill(0);
}
else{
noFill();
}
rect(x,y,(size/2)-1,(size/2)-1);
}
}
}
}
function decipher(){
animValue = 0;
let sh1 = document.getElementById("share1");
let sh2 = document.getElementById("share2");
if(sh1.value.length <= 0){
raisedError = 1;
document.getElementById("errors").innerHTML = "Nie podano pierwszego udziału!";
}
else if(sh2.value.length <= 0){
raisedError = 2;
document.getElementById("errors").innerHTML = "Nie podano drugiego udziału!";
}
else if(sh1.value.length != sh2.value.length){
raisedError = 3;
document.getElementById("errors").innerHTML = "Podane udziały mają różną długość!";
}
else{
raisedError = 0;
document.getElementById("errors").innerHTML = "";
}
//checking shares
if(raisedError == 0){
let sh1Rows = sh1.value.split(",");
let sh2Rows = sh2.value.split(",");
c = sh1Rows.length;
r = sh1.value.search(",");
sh1Arr = [...Array(c).keys()].map(i => Array(r));
sh2Arr = [...Array(c).keys()].map(i => Array(r));
let sh1Helper;
let sh2Helper;
for(let i = 0; i < c; i++){
sh1Helper = sh1Rows[i].split("");
sh2Helper = sh2Rows[i].split("");
for(let j = 0; j < r; j++){
if(sh1Helper[j] == 1) sh1Arr[i][j] = 1;
else sh1Arr[i][j] = 0;
if(sh2Helper[j] == 1) sh2Arr[i][j] = 1;
else sh2Arr[i][j] = 0;
}
}
//setup
//creating background canva
canva = createCanvas(3*c/2*size+2*size,r/2*size);
canva.parent('sketch-holder');
//creating table
if(c%2 || r%2){
raisedError=4;
document.getElementById("errors").innerHTML = "Ktoryś z wymiarów jest nieparzysty.";
}
inorout = [...Array(ceil(c/2)).keys()].map(i => Array(ceil(r/2)));
cleanInorout();
}
//locking "Overlay shares" button
if(!raisedError) document.getElementById("overlay-btn").disabled = false;
else document.getElementById("overlay-btn").disabled = true;
}
function overlay(){
animValue = c/2*size+size;
pushtoInorout();
}
function cleanInorout(){
//writing "empty" values to input/output
for(let i = 0; i < c/2; i++){
for(let j = 0; j < r/2; j++){
inorout[i][j] = 0;
}
}
}
function pushtoInorout(){
//writing values to input/output
for(let i = 0; i < c/2; i++){
for(let j = 0; j < r/2; j++){
let blacked = 0;
if(sh1Arr[i*2][j*2] || sh2Arr[i*2][j*2]) blacked++;
if(sh1Arr[i*2+1][j*2] || sh2Arr[i*2+1][j*2]) blacked++;
if(sh1Arr[i*2][j*2+1] || sh2Arr[i*2][j*2+1]) blacked++;
if(sh1Arr[i*2+1][j*2+1] || sh2Arr[i*2+1][j*2+1]) blacked++;
if (blacked == 4) inorout[i][j] = 1;
else inorout[i][j] = 0;
}
}
}