-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
229 lines (203 loc) · 5.44 KB
/
utils.js
File metadata and controls
229 lines (203 loc) · 5.44 KB
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
class Stack {
constructor() {
this.array = [];
}
push(element) {
this.array.push(element);
}
pop() {
if (!this.isEmpty()) return this.array.pop();
else return null;
}
top() {
if (!this.isEmpty()) return this.array[this.array.length - 1];
else return null;
}
isEmpty() {
return this.array.length === 0;
}
}
const drawRect=(autoPlayBtn,text, ctx)=>{
const x = autoPlayBtn.x;
const y = autoPlayBtn.y;
const width = autoPlayBtn.width;
const height = autoPlayBtn.height;
// console.log(x, y, width, height);
ctx.beginPath();
ctx.font = "20px Georgia";
ctx.fillRect(
x,
y,
width,
height
);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "yellow";
ctx.fillText(text, x+width/2, y+height/2);
ctx.fillStyle = "black";
ctx.stroke();
}
const getRextCenter=(rect)=>{
const x = autoPlayBtn.x;
const y = autoPlayBtn.y;
const width = autoPlayBtn.width;
const height = autoPlayBtn.height;
return {x:x+width/2, y:y+height/2};
}
// glue p1 with p2
const glue = (p1, p2) => {
p1.x = p2.x;
p1.y = p2.y;
};
const movePointWithMouse = (point) => {
document.onmousemove = (ev) => {
glue(point, ev);
};
};
function findDistance(p1, p2) {
return Math.hypot(p1.x - p2.x, p1.y - p2.y);
}
function nearestPoint(point, fixed_points) {
let a = 10000000;
let index = 0;
let nearest_point = fixed_points[0];
for (let i = 0; i < fixed_points.length; i++) {
const Distance = findDistance(point, fixed_points[i]);
if (Distance < a) {
a = Distance;
nearest_point = fixed_points[i];
index = i;
}
}
return [nearest_point, index];
}
const draw = (point, ctx) => {
ctx.beginPath();
ctx.arc(point.x, point.y, 10, 0, Math.PI * 2);
ctx.stroke();
};
function remover(array, element) {
let index = null;
for (let i = 0; i < array.length; i++) {
if ((array[i].index = element.index)) {
index = i;
}
}
console.log(index);
return array.splice(index, 1);
}
function pointInRegion(point, region, ctx) {
const padding = region.padding ? region.padding : 0;
const x = region.x - padding;
const y = region.y - padding;
const width = region.width + padding * 2 * region.length;
const height = region.height + padding * 2;
if (ctx) {
ctx.beginPath();
ctx.strokeStyle = "blue";
ctx.rect(x, y, width, height);
ctx.stroke();
}
return (
x < point.x && point.x < x + width && y < point.y && point.y < y + height
);
}
function pointInRegion2(point, region) {
const padding = region.padding ? region.padding : 0;
const x = region.x - padding;
const y = region.y + padding;
const height =
(region.length + 1) * region.symbolProps.height +
padding * 2 * (region.length + 1);
const width = region.symbolProps.width + padding * 2;
return (
x < point.x && point.x < x + width && y > point.y && point.y > y - height
);
}
// function infixToPostfixConverter(infix) {
// const expression = ["[", ...infix, "]"];
// const postfix = new Stack();
// const stack = new Stack();
// 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 bracesOperater = (b, stack, postfix) => {
// if (!braces(b)) return false;
// const [braceIndex, isOpening] = braces(b);
// if (isOpening) return stack.push(b);
// else {
// while (1) {
// const poped = stack.pop();
// if (!braces(poped)) postfix.push(poped);
// else {
// const [bI, iO] = braces(poped);
// if (bI === braceIndex && iO === true) return;
// }
// }
// }
// };
// const operator = (o) => {
// if (o === "+" || o === "-") return 1;
// else if (o === "*" || o === "/") return 2;
// else if (o === "^") return 3;
// else return false;
// };
// const operateOperater = (o, stack, postfix) => {
// const optrIndex = operator(o);
// if (!optrIndex) return false;
// let optrIndexStackTop = operator(stack.top());
// if (!optrIndexStackTop || optrIndexStackTop < optrIndex)
// return stack.push(o);
// else {
// while (1) {
// if (optrIndexStackTop > optrIndex) postfix.push(stack.pop());
// else {
// stack.push(o);
// return;
// }
// }
// }
// };
// const alpNumOperation = (alpNum, stack, postfix) => {
// if (!braces(alpNum) && !operator(alpNum)) postfix.push(alpNum);
// };
// for (const symbol of expression) {
// alpNumOperation(symbol, stack, postfix);
// bracesOperater(symbol, stack, postfix);
// operateOperater(symbol, stack, postfix);
// }
// return postfix;
// }
// function movePointWithMouse(point) {
// document.onmousemove = (event) => {
// point.x = event.x;
// point.y = event.y;
// };
// }
// document.onmousedown = (ev) => {
// const nearest_point = nearestPoint(mouse, all_points);
// if (findDistance(nearest_point, mouse) < 10) {
// movePointWithMouse(nearest_point);
// }
// };
// document.onmouseup = (e) => {
// movePointWithMouse(mouse);
// };
// function moveFromTo(i, f) {
// i.x = f.x;
// i.y = f.y;
// // init
// t = 0;
// // while (t <= 1) {
// // i.x = lerp(i.x, f.x, t);
// // i.y = lerp(i.y, f.y, t);
// // t = t + 0.01;
// // }
// }
function lerp(a, b, t) {
return a + (b - a) * t;
}