Skip to content

Commit 4e5be40

Browse files
committed
Added temperature.
1 parent d9819db commit 4e5be40

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

layouts/force-directed-layout.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Layout.ForceDirected = function(graph, options) {
4242
var options = options || {};
4343

4444
this.layout = options.layout || "2d";
45-
this.attraction_multiplier = options.attraction || 5;
45+
this.attraction_multiplier = options.attraction || 0.75;
4646
this.repulsion_multiplier = options.repulsion || 0.75;
4747
this.max_iterations = options.iterations || 1000;
4848
this.graph = graph;
@@ -74,10 +74,10 @@ Layout.ForceDirected = function(graph, options) {
7474
attraction_constant = this.attraction_multiplier * forceConstant;
7575
repulsion_constant = this.repulsion_multiplier * forceConstant;
7676
};
77-
77+
7878
this.generate = function() {
7979
// TODO: stop if total force reached 0
80-
if(layout_iterations < this.max_iterations) {
80+
if(layout_iterations < this.max_iterations && temperature > 0.000001) {
8181
var start = new Date().getTime();
8282

8383
// calculate repulsion
@@ -139,12 +139,12 @@ Layout.ForceDirected = function(graph, options) {
139139
node_u.layout.offset_z = 0;
140140
}
141141
}
142-
node_u.layout.offset_x += -((delta_x / delta_length) * force);
143-
node_u.layout.offset_y += -((delta_y / delta_length) * force);
142+
node_u.layout.offset_x -= (delta_x / delta_length) * force;
143+
node_u.layout.offset_y -= (delta_y / delta_length) * force;
144144

145145
if(this.layout === "3d") {
146146
node_v.layout.offset_z += (delta_z / delta_length_z) * force_z;
147-
node_u.layout.offset_z += -((delta_z / delta_length_z) * force_z);
147+
node_u.layout.offset_z -= (delta_z / delta_length_z) * force_z;
148148
}
149149
}
150150
}
@@ -199,23 +199,31 @@ Layout.ForceDirected = function(graph, options) {
199199
node.layout.tmp_pos_z += (node.layout.offset_z / delta_length_z) * Math.min(delta_length_z, temperature);
200200
}
201201

202-
var c = 200;
203-
var updated = false;
204-
if(node.position.x < (node.layout.tmp_pos_x - c) || node.position.x > (node.layout.tmp_pos_x + c)) {
205-
node.position.x -= (node.position.x-node.layout.tmp_pos_x)/10;
206-
updated = true;
207-
}
208-
if(node.position.y < (node.layout.tmp_pos_y - c) || node.position.y > (node.layout.tmp_pos_y + c)) {
202+
var updated = true;
203+
node.position.x -= (node.position.x-node.layout.tmp_pos_x)/10;
209204
node.position.y -= (node.position.y-node.layout.tmp_pos_y)/10;
210-
updated = true;
211-
}
205+
212206
if(this.layout === "3d") {
213-
if(node.position.z < (node.layout.tmp_pos_z - c) || node.position.z > (node.layout.tmp_pos_z + c)) {
214-
node.position.z -= (node.position.z-node.layout.tmp_pos_z)/10;
215-
updated = true;
216-
}
207+
node.position.z -= (node.position.z-node.layout.tmp_pos_z)/10;
217208
}
218209

210+
// var c = 200;
211+
// var updated = false;
212+
// if(node.position.x < (node.layout.tmp_pos_x - c) || node.position.x > (node.layout.tmp_pos_x + c)) {
213+
// node.position.x -= (node.position.x-node.layout.tmp_pos_x)/10;
214+
// updated = true;
215+
// }
216+
// if(node.position.y < (node.layout.tmp_pos_y - c) || node.position.y > (node.layout.tmp_pos_y + c)) {
217+
// node.position.y -= (node.position.y-node.layout.tmp_pos_y)/10;
218+
// updated = true;
219+
// }
220+
// if(this.layout === "3d") {
221+
// if(node.position.z < (node.layout.tmp_pos_z - c) || node.position.z > (node.layout.tmp_pos_z + c)) {
222+
// node.position.z -= (node.position.z-node.layout.tmp_pos_z)/10;
223+
// updated = true;
224+
// }
225+
// }
226+
219227
if(updated && typeof callback_positionUpdated === 'function') {
220228
callback_positionUpdated(node);
221229
}
@@ -224,7 +232,9 @@ Layout.ForceDirected = function(graph, options) {
224232
mean_time += end - start;
225233
// info.innerHTML = "node_force: " + parseInt(node_force) + "<br>edge_force: " + edge_force + "<br>div: " + (node_force-edge_force);
226234

227-
// temperature *= (1.0 - (layout_iterations / this.max_iterations));
235+
temperature *= (1 - (layout_iterations / this.max_iterations));
236+
// temperature -= 1/100;
237+
// console.log(temperature);
228238
layout_iterations++;
229239
} else {
230240
if(!this.finished) {

0 commit comments

Comments
 (0)