Permalink
Browse files

implement force-based grpah prettification. it does wierd things, but…

… i guess it looks alright.
  • Loading branch information...
1 parent 140d80d commit a4f3b9f445321816c8685d18e7c3dd5412a4ea18 Daniel Thornton committed Apr 28, 2012
Showing with 88 additions and 4 deletions.
  1. +44 −0 web/js/board.js
  2. +44 −4 web/js/reductions.js
View
@@ -528,6 +528,50 @@ var Node = ScreenObject.extend({
return resistors;
},
+ coulomb: function(self, node) {
+ var force = [0,0]; // force vector
+ var dist = [0,0]; // relative position vector
+ var r; // radius
+ var k = 100000; // proportionality constant
+
+ dist = [(self.x - node.x), (self.y - node.y)];
+ r = Math.sqrt(Math.pow(dist[0], 2) + Math.pow(dist[1], 2));
+ r = k / Math.pow(r, 3);
+ force = [(r * dist[0]), (r * dist[1])];
+
+ return force;
+ },
+
+ hooke: function(self, element) {
+ var force = [0, 0]; // force vector
+ var dist = [0, 0]; // relative position vector
+ var nodes = []; // node array
+ var r; // radius
+ var n; // node
+ var i; // iterator
+ var ideal = 100; // ideal length of resistors
+ var k = 0.1; // spring constant
+
+ nodes = element.nodes();
+ for (i = 0; i < nodes.length; i++) {
+ n = nodes[i]
+ if (!(n === self)) {
+ break;
+ }
+ }
+
+ if ((n === self) || (nodes.length != 2)) {
+ return force;
+ }
+
+ dist = [(self.x - n.x), (self.y - n.y)];
+ r = Math.sqrt(Math.pow(dist[0], 2) + Math.pow(dist[1], 2));
+ r = (k * (ideal -r))/r;
+ force = [(r * dist[0]), (r * dist[1])];
+
+ return force;
+ },
+
remove: function(self) {
var index;
index = self.board.nodes.indexOf(self);
View
@@ -402,14 +402,18 @@ var Reduction = Class.extend({
},
prettify_force_graph: function(self) {
- var i; // iterator
+ var i, j; // iterator
var nodes = []; // node array
var n; // node
var resistors = []; // resistor array
- var r; // resistor
+ var elements = []; // element array
+ var kinetic, kmax = 0.001; // kinetic energy
+ var force; // net forces on a node
+ var damping = 0.5; // damping force on nodes (aka friction)
- for (i = 0; i < self.board.nodes.length; i++) {
- self.prettify_node(self.board.nodes[i]);
+ nodes = self.board.nodes;
+ for (i = 0; i < nodes.length; i++) {
+ self.prettify_node(nodes[i]);
}
resistors = self.board.resistors();
@@ -419,8 +423,44 @@ var Reduction = Class.extend({
// force-iterate
+ // reset forces
+ nodes = self.board.nodes;
+ for (i = 0; i < nodes.length; i++) {
+ nodes[i].velocity = [0,0];
+ }
+
+ do {
+ kinetic = 0;
+ // for every node
+ for (i = 0; i < nodes.length; i++) {
+ n = nodes[i];
+ force = [0,0];
+
+ for (j = 0; j < nodes.length; j++) {
+ if (! (nodes[j] === n)) {
+ force = self.vector_sum(force, n.coulomb(nodes[j]));
+ }
+ }
+
+ elements = n.elements();
+ for (j = 0; j < elements.length; j++) {
+ force = self.vector_sum(force, n.hooke(elements[j]));
+ }
+
+ n.velocity = self.vector_sum(n.velocity, force);
+ n.velocity = [n.velocity[0]*damping, n.velocity[1]*damping];
+ n.x += n.velocity[0];
+ n.y += n.velocity[1];
+ kinetic += (Math.pow(n.velocity[0], 2) + Math.pow(n.velocity[1], 2));
+ }
+ } while (kinetic > kmax)
+
return true;
},
+
+ vector_sum: function(self, a, b) {
+ return [(a[0] + b[0]), (a[1] + b[1])];
+ },
});
// tool for series reductions

0 comments on commit a4f3b9f

Please sign in to comment.