/** * 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; } // } } } }
/** * This helper calculates all the attractive forces onto a node. Attractive forces from from the * edges pulling nodes towards each other, this will love through the edges of this node. * * @param current Node to calculate forces for. */ private void calculateNodeAttractiveForce(Node current) { // get all the edges of the current node ArrayList<Edge> nodeEdgeList = current.getEdgeList(); // of each of this node's edge do attactive forces Iterator<Edge> nodeEdges = nodeEdgeList.iterator(); /// int numOfEdgesWeight = 10 * (int) Math.round(nodeEdgeList.size() + SPACING); // Loop through edges and find edges containing current node while (nodeEdges.hasNext()) { Edge e = nodeEdges.next(); double edgeStrength = e.getStrength(); Node n; int dx, dy; double sign = 1.0; if (current == e.getNode1()) { n = e.getNode2(); dx = current.getX() - n.getX(); dy = current.getY() - n.getY(); } else { n = e.getNode1(); dx = current.getX() - n.getX(); dy = current.getY() - n.getY(); sign = -1.0; } double distance = Math.sqrt(dx * dx + dy * dy) + .1; // multiply by the strength of edge // current.setVX(current.getVX() - dx / numOfEdgesWeight * edgeStrength); // current.setVY(current.getVY() - dy / numOfEdgesWeight * edgeStrength); current.accelx += sign * (dx * STIFFNESS * (SPRING_LENGTH - distance)) / current.weight; current.accely += sign * (dy * STIFFNESS * (SPRING_LENGTH - distance)) / current.weight; // current.accelx += sign * (dx * (e.getStrength() + 1) * (SPRING_LENGTH - distance) * 0.5) / // current.weight; // current.accely += sign * (dy * (e.getStrength() + 1) * (SPRING_LENGTH - distance) * 0.5) / // current.weight; // n.accelx += sign * (dx * (e.getStrength() + 1) * (SPRING_LENGTH - distance) * 0.5) / // n.weight; // n.accely += sign * (dy * (e.getStrength() + 1) * (SPRING_LENGTH - distance) * 0.5) / // n.weight; } }