/** * This helper method calculates the repulsive forces acting on a node from all the other nodes in * the graph. BIG O( number of nodes ) * * <p>There is a repulsive force between every nodes depending on the distance separating them and * their size. * * @param current node to calculate forces for. */ private void calculateNodeRepulsiveForces(Node current) { Iterator<Node> inner = controller.getNodeIterator(); int current_radius, n_radius, dx, dy; Node n; current_radius = (int) Math.sqrt(Math.pow(current.getWidth(), 2) + Math.pow(current.getHeight(), 2)); while (inner.hasNext()) { n = inner.next(); if (n != current) { dx = current.getX() - n.getX(); dy = current.getY() - n.getY(); n_radius = (int) Math.sqrt(Math.pow(n.getWidth(), 2) + Math.pow(n.getHeight(), 2)); // only repel if nodes are connected or within diameter * MAX_REPEL_DISTANCE // if (Math.sqrt(dx * dx + dy * dy) < (Math.max(current_radius, n_radius) * // MAX_REPEL_MULTIPLIER)) { double l = (dx * dx + dy * dy) + .1; if (l > 0) { // current.setVX(current.getVX() + dx * (current_radius * SPACING) / l); // current.setVY(current.getVY() + dy * (current_radius * SPACING) / l); current.accelx += (dx * REPULSION / (l)) / current.weight; current.accely += (dy * REPULSION / (l)) / current.weight; // current.accelx += (dx * SPACING / (l * .5)) / current.weight; // current.accely += (dy * SPACING / (l * .5)) / current.weight; // n.accelx += (dx * SPACING / (l * -0.5)) / n.weight; // n.accely += (dy * SPACING / (l * -0.5)) / n.weight; } // } } } }
/** * The width of a binary node is the sum of the widths of its two sub-trees. * * @return The width of the tree rooted at this node. */ public int getWidth() { return left.getWidth() + right.getWidth(); }