//Copyright 2005 Sean McCullough //banksean at yahoo public class SpringEdge extends Edge { float k=0.1; //stiffness float a=100; //natural length. ehmm uh, huh huh stiffness. natural length ;-) float weight = 1; //This edge sublcass apples a spring force between the two nodes it connects //The spring force formula is F = k(currentLength-a) //This equation is one-dimensional, and applies to the straight line //between the two nodes. public SpringEdge(Node a, Node b) { super(a, b); } public void setWeight(float w) { weight = w; } public void setStiffness(float s) { k = s; } public void setNaturalLength(float l) { a = l; } public float getNaturalLength() { return a; } public Vector3D getForceTo() { float dx = dX(); float dy = dY(); float l = sqrt(dx*dx + dy*dy); float f = k*(l-a); return new Vector3D(-f*dx/l, -f*dy/l, 0); } public Vector3D getForceFrom() { float dx = dX(); float dy = dY(); float l = sqrt(dx*dx + dy*dy); float f = k*(l-a); return new Vector3D(f*dx/l, f*dy/l, 0); } public void draw() { float dx = dX(); float dy = dY(); Vector3D f = getForceFrom(); stroke(255,255,255,64); strokeWeight(weight); line(from.getX(), from.getY(), to.getX(), to.getY()); if (g.getSelectedNode() == from || g.getSelectedNode() == to) { strokeWeight(300/a); stroke(32,64,255,128); line(from.getX(), from.getY(), to.getX(), to.getY()); textFont(font, 10); fill(255,255,255,64); text(label, from.getX() + dx/4 - textWidth(label)/2, from.getY() + dy/4); textFont(font, 14); smooth(); } else if (g.getHoverNode() == from || g.getHoverNode() == to) { fill(255,255,255,64); line(from.getX(), from.getY(), to.getX(), to.getY()); textFont(font, 10); text(label, from.getX() + dx/4 - textWidth(label)/2, from.getY() + dy/4); textFont(font, 14); smooth(); } } }