-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrig_vector.js
More file actions
72 lines (60 loc) · 1.78 KB
/
trig_vector.js
File metadata and controls
72 lines (60 loc) · 1.78 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
var repulsion_constant = 500;
var attraction_constant = 0.1;
var springLength = 75;
function reposition()
{
let repellingForce, attractingForce;
for(let a=0; a < nodes.length; a++)
{
if(nodes[a].visibility == "false")
continue;
for(let b=a+1; b < nodes.length; b++)
{
if(nodes[b].visibility == "false")
continue;
repellingForce = replusionForce (nodes[a], nodes[b]);
nodes[a].sumVectors(repellingForce, "add");
nodes[b].sumVectors(repellingForce, "minus");
}
}
for(let a=0; a < edges.length; a++)
{
let s = edges[a].source, t = edges[a].target;
attractingForce = attractionForce(nodes[s], nodes[t]);
nodes[s].sumVectors(attractingForce, "add");
nodes[t].sumVectors(attractingForce, "minus");
}
for(let a=0; a < nodes.length; a++)
{
if(nodes[a].visibility == "false")
continue;
nodes[a].newPosition(nodes[a].velocity);
nodes[a].velocity.reset();
}
}
function replusionForce(a, b)
{
let d = Math.max(1, distance (a.location, b.location));
let norm = new Point (0,0);
let force = repulsion_constant / Math.pow(d, 2) ;
norm.x = (a.location.x - b.location.x) / d;
norm.y = (a.location.y - b.location.y) / d;
let fx = force * norm.x, fy = force * norm.y ;
return new Vector(fx, fy);
}
function attractionForce(a, b)
{
let d = Math.max(1, distance (a.location, b.location));
let norm = new Point (0,0);
let force = -attraction_constant * Math.max(0, d - springLength) ;
norm.x = (a.location.x - b.location.x) / d;
norm.y = (a.location.y - b.location.y) / d;
let fx = force * norm.x, fy = force * norm.y ;
return new Vector(fx, fy);
}
function distance(a, b)
{
let x = a.x - b.x;
let y = a.y - b.y;
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}