-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
287 lines (271 loc) · 8.15 KB
/
main.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
const myCanvas = document.getElementById("myCanvas");
myCanvas.height = window.innerHeight;
myCanvas.width = window.innerWidth;
const ctx = myCanvas.getContext("2d");
const expression = "(m-n)*(p+q)";
const [postfix, moves] = infixToPostfixConverter(expression);
console.log(postfix, moves);
const containerManager = new ContainerManager(expression, {
width: 30,
height: 30,
});
containerManager.draw(ctx);
containerManager.update(ctx);
mover();
async function mover() {
for (let i = 0; i < moves.length; i++) {
let t = 0;
let { before, after } = moves[i];
if (!after) after = containerManager.dustbin;
const element = containerManager.ELEMENT.find(
(value) => value.index === before.index
);
const distance = findDistance(before, after);
const deltaT = (Math.round((2.5 / distance) * 1000) / 1000);
console.log(deltaT);
while (t < 1) {
t += deltaT;
const newPoint = {
x: lerp(before.x, after.x, Math.sin(t * Math.PI/2)),
y: lerp(before.y, after.y, Math.sin(t * Math.PI/2)),
};
glue(element, newPoint);
await sleep(10);
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
containerManager.draw(ctx);
containerManager.update(ctx);
}
glue(element, after);
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
containerManager.draw(ctx);
containerManager.update(ctx);
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// var t = 0;
// setTimeout(() => {
// mover(moves);
// }, 10);
// animate();
// function animate() {
// myCanvas.height = window.innerHeight;
// myCanvas.width = window.innerWidth;
// ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
// containerManager.draw(ctx);
// containerManager.update(ctx);
// requestAnimationFrame(animate);
// }
// function mover(moves) {
// if (moves.length) {
// setTimeout(() => {
// const move = moves.shift();
// let { before, after } = move;
// const element = containerManager.ELEMENT.find(
// (value) => value.index === before.index
// );
// let x = 60;
// if (!after) after = { x: x * moves.length, y: 60 };
// const moving = () => {
// t += 0.01;
// glue(element, {
// x: lerp(before.x, after.x, t),
// y: lerp(before.y, after.y, t),
// });
// if (t <= 1) {
// setTimeout(() => {
// moving();
// }, 10);
// }
// };
// if (t <= 1) moving();
// else {
// mover(moves);
// t = 0;
// }
// console.log(t);
// }, 0);
// }
// }
function infixToPostfixConverter(infix) {
const containerManager = new ContainerManager(infix, {
width: 30,
height: 30,
});
const elements = [
{ label: "[" },
...JSON.parse(JSON.stringify(containerManager.ELEMENT)),
{ label: "]" },
];
const moves = [];
const postfix = new Stack();
const stack = new Stack();
const infixToStack = (element) => {
if (element.x) {
const before = { ...element };
containerManager.arrayInfixContainer.remove(element);
containerManager.stackContainer.add(element);
moves.push({
before: before,
after: { ...element },
type: "infixToStack",
});
}
};
const infixToDestroy = (element) => {
if (element.x) {
const before = { ...element };
containerManager.arrayInfixContainer.remove(element);
moves.push({ before: before, after: null, type: "infixToDestroy" });
}
};
const stackToPostfix = (element) => {
if (element.x) {
const before = { ...element };
containerManager.stackContainer.remove(element);
containerManager.arrayPostfixContainer.add(element);
moves.push({
before: before,
after: { ...element },
type: "stackToPostfix",
});
}
};
const stackToDestroy = (element) => {
if (element.x) {
const before = { ...element };
containerManager.stackContainer.remove(element);
moves.push({ before: before, after: null, type: "stackToDestroy" });
}
};
const infixToPostfix = (element) => {
if (element.x) {
const before = { ...element };
containerManager.arrayInfixContainer.remove(element);
containerManager.arrayPostfixContainer.add(element);
moves.push({
before: before,
after: { ...element },
type: "infixToPostfix",
});
}
};
const braces = (b) => {
if (b === "(" || b === ")") return [1, b === "("];
else if (b === "{" || b === "}") return [2, b === "{"];
else if (b === "[" || b === "]") return [3, b === "["];
else return false;
};
const operator = (o) => {
if (o === "+" || o === "-") return 1;
else if (o === "*" || o === "/") return 2;
else if (o === "^") return 3;
else return false;
};
const bracesOperater = (element, stack, postfix) => {
const b = element.label;
if (!braces(b)) return false;
const [braceIndex, isOpening] = braces(b);
if (isOpening) {
// 1. remove from infix and add to stack -> 1
infixToStack(element);
return stack.push(b);
} else {
// 2. remove from infix and destroy -> 2
infixToDestroy(element);
while (1) {
// remove from stack
let popedElement = containerManager.stackContainer.top;
const poped = stack.pop();
if (!braces(poped)) {
// 3. add to postfix-> 3
let popedElement = containerManager.stackContainer.top;
stackToPostfix(popedElement);
postfix.push(poped);
} else {
const [bI, iO] = braces(poped);
if (bI === braceIndex && iO === true) {
// 4. destroy -> 4
if (popedElement) stackToDestroy(popedElement);
return;
}
}
}
}
// else {
// // 2. remove from infix and destroy -> 2
// infixToDestroy(element);
// while (1) {
// const poped = stack.pop();
// if (!braces(poped)) {
// // 3. remove from stack and add to postfix-> 3
// stackToPostfix(element);
// postfix.push(poped);
// } else {
// const [bI, iO] = braces(poped);
// if (bI === braceIndex && iO === true) {
// // 4. remove from stack and destroy -> 4
// stackToDestroy(element);
// return;
// }
// }
// }
// }
};
const operateOperater = (element, stack, postfix) => {
const o = element.label;
const optrIndex = operator(o);
if (!optrIndex) return false;
let optrIndexStackTop = operator(stack.top());
if (!optrIndexStackTop || optrIndexStackTop < optrIndex) {
// 5. remove from infix and add to stack -> 1
infixToStack(element);
return stack.push(o);
} else {
while (1) {
let optrIndexStackTop = operator(stack.top());
if (optrIndexStackTop > optrIndex) {
// 6. remove from stack and add to postfix -> 3
const popedElement = containerManager.stackContainer.top;
// if (popedElement)
stackToPostfix(popedElement);
postfix.push(stack.pop());
} else {
// 7. remove from infix and add to stack -> 1
infixToStack(element);
stack.push(o);
return;
}
}
}
// else {
// while (1) {
// if (optrIndexStackTop > optrIndex) {
// // 6. remove from stack and add to postfix -> 3
// stackToPostfix(element);
// postfix.push(stack.pop());
// } else {
// // 7. remove from infix and add to stack -> 1
// infixToStack(element);
// stack.push(o);
// return;
// }
// }
// }
};
const alpNumOperation = (element, stack, postfix) => {
const alpNum = element.label;
if (!braces(alpNum) && !operator(alpNum)) {
// 8. remove from infix and add to postfix -> 5 <-
infixToPostfix(element);
postfix.push(alpNum);
}
};
for (const element of elements) {
alpNumOperation(element, stack, postfix);
bracesOperater(element, stack, postfix);
operateOperater(element, stack, postfix);
}
return [postfix, moves];
}