/** * Adds the given edge to those to be displayed in the viewer. Note that the edge must connect * nodes that have already been added to the viewer. This version will use the locations of the * two nodes to calculate their distance of separation. * * @param edge Edge to add to the display. * @return True if edge was added successfully. False if edge contains nodes that have not been * added to the viewer. */ public boolean addEdge(E edge) { Particle p1 = nodes.get(edge.getNode1()); if (p1 == null) { System.err.println("Warning: Node1 not found when creating edge."); return false; } Particle p2 = nodes.get(edge.getNode2()); if (p2 == null) { System.err.println("Warning: Node2 not found when creating edge."); return false; } // Only add edge if it does not already exist in the collection if (!edges.containsKey(edge)) { float x1 = p1.position().x(); float y1 = p1.position().y(); float x2 = p2.position().x(); float y2 = p2.position().y(); // Strength, damping, reset length edges.put( edge, physics.makeSpring( p1, p2, EDGE_STRENGTH, DAMPING, (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)))); } return true; }
/** * Creates a spring between the two given nodes with the given strength. If the two nodes not * directly connected by an edge already have a spring between them, it will be replaced by this * one. * * @param node1 First of the two nodes to have a spring between them. * @param node2 Second of the two nodes to have a spring between them. * @param length The length of this spring (natural rest distance at which the two nodes would * sit). * @param strength The strength of this new spring. * @return True if the viewer contains the two nodes and a spring between them has been created. */ public boolean addSpring(N node1, N node2, float length, float strength) { 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 spring if it exists between these two nodes. for (int i = 0; i < physics.getNumSprings(); i++) { Spring spring = physics.getSpring(i); if ((((spring.getOneEnd() == p1) && (spring.getTheOtherEnd() == p2)) || ((spring.getOneEnd() == p2) && (spring.getTheOtherEnd() == p1))) && (spring.strength() != EDGE_STRENGTH)) { physics.removeSpring(spring); break; } } // Add the new force. physics.makeSpring(p1, p2, strength, DAMPING, length); return false; }
/** * Adds the given edge to those to be displayed in the viewer. Note that the edge must connect * nodes that have already been added to the viewer. This version will fix the distance of * separation between nodes to the given value * * @param edge Edge to add to the display. * @return True if edge was added successfully. False if edge contains nodes that have not been * added to the viewer. */ public boolean addEdge(E edge, float distance) { Particle p1 = nodes.get(edge.getNode1()); if (p1 == null) { System.err.println("Warning: Node1 not found when creating edge."); return false; } Particle p2 = nodes.get(edge.getNode2()); if (p2 == null) { System.err.println("Warning: Node2 not found when creating edge."); return false; } // Only add edge if it does not already exist in the collection if (!edges.containsKey(edge)) { // Strength, damping, reset length edges.put(edge, physics.makeSpring(p1, p2, EDGE_STRENGTH, DAMPING, distance)); } return true; }
/** * Tethers the given node to its location with the given strength. * * @param node The node to be tethered. * @param strength Strength of the tether. * @return True if the viewer contains the given node and it was tethered successfully. */ public boolean tether(N node, float strength) { Particle p1 = nodes.get(node); if (p1 == null) { return false; } // Grab the tethering stake if it has already been created, otherwise create a new one. Particle stake = stakes.get(node); if (stake == null) { stake = physics.makeParticle(1, node.getLocation().x, node.getLocation().y, 0); stake.makeFixed(); stakes.put(node, stake); } // Grab the tether if it has already been created, otherwise create a new one. Spring tether = tethers.get(stake); if (tether == null) { tether = physics.makeSpring(stake, p1, strength, DAMPING, Float.MIN_VALUE); tethers.put(stake, tether); } else { tether.setStrength(strength); } return true; }