-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
57 lines (48 loc) · 1.43 KB
/
utils.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
'use strict';
exports.sleep = function(ms){
return new Promise(resolve=>{
if(ms && ms > 0) setTimeout(resolve,ms)
else resolve();
})
}
exports.normAngle = function(angle){
while(angle>180) angle -= 360;
while(angle<-180) angle += 360;
return angle;
}
exports.angleInRange = function(startAngle, endAngle, angle){
if(endAngle > startAngle)
return startAngle <= angle && angle <= endAngle
else
return (startAngle <= angle && angle <= 180) || (endAngle >= angle && angle >= -180)
}
exports.getLineAngle = function(x1,y1,x2,y2){
let dx = x2-x1;
let dy = y2-y1;
let rads = Math.atan2(dy,dx)
rads += Math.PI/2;
rads %= 2*Math.PI;
let degs = rads * 180/Math.PI; // to degrees
degs -= 90;
if(degs>180) degs = -360+degs
if(degs<-180) degs = 360+degs
return degs;
}
exports.getDistance = function(x1,y1,x2,y2){
let dx = x1-x2;
let dy = y1-y2;
return Math.sqrt(dx*dx + dy*dy);
}
exports.setLineAngle = function(x1,y1,x2,y2, angle){
let rads = angle*Math.PI/180;
let dx = x1-x2;
let dy = y1-y2;
let length = Math.sqrt(dx*dx + dy*dy);
let rotX = x1 + length*Math.cos(rads);
let rotY = y1 + length*Math.sin(rads);
return {x:rotX, y:rotY};
}
exports.rotateLine = function(x1,y1,x2,y2, angle){
let currentAngle = module.exports.getLineAngle(x1,y1,x2,y2);
return module.exports.setLineAngle(x1,y1,x2,y2, currentAngle+angle)
}