/** * Attempts to space out non-connected nodes from one another. This is achieved by adding a strong * repulsive force between non-connected nodes. Note that this produces n-squared forces so can be * slow for large networks where many nodes are not connected to each other. */ public void spaceNodes() { ArrayList<Particle> pList = new ArrayList<Particle>(nodes.values()); for (int i = 0; i < pList.size(); i++) { for (int j = 0; j < pList.size(); j++) { if (i > j) { Particle p1 = pList.get(i); Particle p2 = pList.get(j); // See if we have a connection between nodes for (Spring spring : edges.values()) { if (((spring.getOneEnd() == p1) && (spring.getTheOtherEnd() == p2)) || ((spring.getOneEnd() == p2) && (spring.getTheOtherEnd() == p1))) { // Do nothing as we already have an edge connecting these two particles } else { // Add a small repulsive force physics.makeAttraction(p1, p2, -1000, 0.1f); } } } } } }
/** * Creates a attractive or repulsive force between the two given nodes. If the two nodes already * have a force between them, it will be replaced by this one. * * @param node1 First of the two nodes to have a force between them. * @param node2 Second of the two nodes to have a force between them. * @param force Force to create between the two nodes. If positive, the nodes will attract each * other, if negative they will repulse. The larger the magnitude the stronger the force. * @param minDistance Minimum distance within which no force is applied. * @return True if the viewer contains the two nodes and a force between them has been created. */ public boolean addForce(N node1, N node2, float force, float minDistance) { Particle p1 = nodes.get(node1); if (p1 == null) { return false; } Particle p2 = nodes.get(node2); if (p2 == null) { return false; } // We may have to remove existing force if it exists between these two nodes. for (int i = 0; i < physics.getNumAttractions(); i++) { Attraction a = physics.getAttraction(i); if (((a.getOneEnd() == p1) && (a.getTheOtherEnd() == p2)) || ((a.getOneEnd() == p2) && (a.getTheOtherEnd() == p1))) { physics.removeAttraction(a); break; } } // Add the new force. physics.makeAttraction(p1, p2, force, minDistance); return false; }