-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFR.js
96 lines (75 loc) · 2.31 KB
/
FR.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// https://github.com/kamilniedzinski/graph-drawing
define(function () {
"use strict";
function FRLayout(settings, graph) { // Fruchterman – Reingold algorithm.
if (!(this instanceof FRLayout)) {
return new FRLayout(settings, graph);
}
this.graph = graph;
this.width = settings.width;
this.height = settings.height;
this.area = this.width * this.height;
this.distConst = 0.4;
this.iterStep = 0.1;
this.reset();
this.graph.forEachVertex(function (v) {
v.pos.rand(0.0, this.width, 0.0, this.height);
}, this);
}
FRLayout.MAX_ITERS = 400;
FRLayout.prototype = {
constructor: FRLayout,
attraction: function (d) {
return Math.pow(d, 2) / this.optDist;
},
repulsion: function (d) {
return Math.pow(this.optDist, 2) / d;
},
calcAttraction: function () {
this.graph.forEachEdge(function (u, v) {
var delta = u.pos.sub(v.pos),
distVec = delta.normalize().multiplyConst(this.attraction(delta.mag()));
u.disp = u.disp.sub(distVec);
v.disp = v.disp.add(distVec);
}, this);
},
calcRepulsion: function () {
this.graph.forEachVertex(function (u) {
u.disp.setValues(0.0, 0.0);
this.graph.forEachVertex(function (v) {
var delta;
if (!u.equals(v)) {
delta = u.pos.sub(v.pos);
u.disp = u.disp.add(delta.normalize().multiplyConst(this.repulsion(delta.mag())));
}
}, this);
}, this);
},
calcDisplacement: function () {
this.graph.forEachVertex(function (v) {
v.pos = v.pos.add(v.disp.normalize().multiplyConst(Math.min(v.disp.mag() / 100, this.temp * 0.1))); // ?
v.pos.x = Math.min(this.width, Math.max(0.0, v.pos.x));
v.pos.y = Math.min(this.height, Math.max(0.0, v.pos.y));
}, this);
},
cool: function () {
this.temp *= (1 - this.currIter / FRLayout.MAX_ITERS);
},
updatePhysics: function () {
this.currIter += this.iterStep;
this.calcRepulsion();
this.calcAttraction();
this.calcDisplacement();
this.cool();
},
isDone: function () {
return (this.temp < 0.5 || this.currIter > FRLayout.MAX_ITERS);
},
reset: function () {
this.optDist = this.distConst * Math.sqrt(this.area / this.graph.getSize());
this.temp = this.width / 10;
this.currIter = 0;
}
};
return FRLayout;
});