-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
29 lines (26 loc) · 961 Bytes
/
Copy pathutils.js
File metadata and controls
29 lines (26 loc) · 961 Bytes
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
module.exports = function() {
return {
getFieldVector: function(x, y) {
// Get the field vector and strength at an abritrary position
// in the grid
vec = {x:0, y:0, strength:0}
for (var charge of charges) {
dx = (x - charge.x)
dx2 = dx * dx;
dy = (y - charge.y)
dy2 = dy * dy
r2 = dx2 + dy2
// Check to ensure r2 is > 0
if (r2 < 1) r2 = 1
// Calculate the field strength due to this charge
strength = k * charge.q / (r2)
// Add it to the field vectors
vec.x += strength * (dx2/r2) * Math.sign(dx)
vec.y += strength * (dy2/r2) * Math.sign(dy)
vec.strength += strength
// console.log(vec)
}
return vec
}
}
}