-
Notifications
You must be signed in to change notification settings - Fork 0
/
force.js
66 lines (57 loc) · 1.26 KB
/
force.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
let mags = [{
mag: 0,
angle: 0
},
{
mag: 0,
angle: 0
},
{
mag: 0,
angle: 0
},
{
mag: 0,
angle: 0
}
]
function calcResultant() {
let resultant = {
mag: null,
angle: null,
x: 0,
y: 0,
color: '#f00'
}
mags.forEach(el => {
resultant.x += calcX(el.angle, el.mag)
resultant.y += calcY(el.angle, el.mag)
}, this)
resultant.mag = pythag(resultant.x, resultant.y)
resultant.angle = atand(resultant.y / resultant.x)
resultant.angle = resultant.angle < 0 ? 360 + resultant.angle : resultant.angle
resultant.angle += 180
return resultant
}
function calcX(angle, magnitude) {
return cosd(angle) * magnitude
}
function calcY(angle, magnitude) {
return sind(angle) * magnitude
}
function sind(deg) {
return Math.sin(deg * (Math.PI / 180))
}
function cosd(deg) {
return Math.cos(deg * (Math.PI / 180))
}
function tand(deg) {
return Math.tan(deg * (Math.PI / 180))
}
function atand(deg) {
return Math.atan(deg) * (180/ Math.PI)
}
function pythag(a, b) {
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))
}
draw(mags)