This repository was archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
42 lines (41 loc) · 1.3 KB
/
Copy pathutils.js
File metadata and controls
42 lines (41 loc) · 1.3 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
module.exports = {
createAddress(client) {
return client && client.remoteAddress && client.remotePort ? (client.remoteAddress + ':' + client.remotePort).slice(7) : undefined
},
createPosition(position) {
const data = position.split(';');
switch (data.length) {
case 2:
return {
x: parseInt(data[0]),
y: parseInt(data[1])
}
case 3:
return {
x: parseInt(data[0]),
y: parseInt(data[1]),
angle: parseFloat(data[2])
}
default:
return undefined
}
},
getAngle(from, to) {
const b = to.x - from.x
const a = to.y - from.y
let alpha = 0
if (b !== 0 && a !== 0) {
const c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))
let w = Math.pow(a, 2) - Math.pow(b, 2) - Math.pow(c, 2)
let z = -2 * Math.abs(b) * Math.abs(c)
alpha = Math.acos(w/z) * 180 / Math.PI
alpha = Math.round(alpha)
} else {
alpha = b >= 0 ? 0 : 180
}
return a >= 0 ? alpha : -alpha
},
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}