/** * Updates the particle view. This should be called on each draw cycle in order to update the * positions of all nodes and edges in the viewer. If you need to update the positions of * particles without drawing it (e.g. to speed up movement, call updateParticles() instead. */ public void draw() { parent.pushStyle(); parent.pushMatrix(); zoomer.transform(); updateCentroid(); centroid.tick(); parent.translate(width / 2, height / 2); parent.scale(centroid.getZ()); parent.translate(-centroid.getX(), -centroid.getY()); if (!isPaused) { updateParticles(); } // Ensure that any selected element is positioned at the mouse location. if (selectedNode != null) { Particle p = nodes.get(selectedNode); p.makeFixed(); float mX = (zoomer.getMouseCoord().x - (width / 2)) / centroid.getZ() + centroid.getX(); float mY = (zoomer.getMouseCoord().y - (height / 2)) / centroid.getZ() + centroid.getY(); p.position().set(mX, mY, 0); } // Draw edges if we have positive stroke weight. if (parent.g.strokeWeight > 0) { parent.stroke(0, 180); parent.noFill(); for (Map.Entry<E, Spring> row : edges.entrySet()) { E edge = row.getKey(); Spring spring = row.getValue(); Vector3D p1 = spring.getOneEnd().position(); Vector3D p2 = spring.getTheOtherEnd().position(); edge.draw(parent, p1.x(), p1.y(), p2.x(), p2.y()); } } // Draw nodes. parent.noStroke(); parent.fill(120, 50, 50, 180); for (Map.Entry<N, Particle> row : nodes.entrySet()) { N node = row.getKey(); Vector3D p = row.getValue().position(); node.draw(parent, p.x(), p.y()); } parent.popMatrix(); parent.popStyle(); }
/** * 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; }