1 package prefuse.util.force; 2 3 9 public class SpringForce extends AbstractForce { 10 11 private static String [] pnames 12 = new String [] { "SpringCoefficient", "DefaultSpringLength" }; 13 14 public static final float DEFAULT_SPRING_COEFF = 1E-4f; 15 public static final float DEFAULT_MAX_SPRING_COEFF = 1E-3f; 16 public static final float DEFAULT_MIN_SPRING_COEFF = 1E-5f; 17 public static final float DEFAULT_SPRING_LENGTH = 50; 18 public static final float DEFAULT_MIN_SPRING_LENGTH = 0; 19 public static final float DEFAULT_MAX_SPRING_LENGTH = 200; 20 public static final int SPRING_COEFF = 0; 21 public static final int SPRING_LENGTH = 1; 22 23 30 public SpringForce(float springCoeff, float defaultLength) { 31 params = new float[] { springCoeff, defaultLength }; 32 minValues = new float[] 33 { DEFAULT_MIN_SPRING_COEFF, DEFAULT_MIN_SPRING_LENGTH }; 34 maxValues = new float[] 35 { DEFAULT_MAX_SPRING_COEFF, DEFAULT_MAX_SPRING_LENGTH }; 36 } 37 38 41 public SpringForce() { 42 this(DEFAULT_SPRING_COEFF, DEFAULT_SPRING_LENGTH); 43 } 44 45 49 public boolean isSpringForce() { 50 return true; 51 } 52 53 56 protected String [] getParameterNames() { 57 return pnames; 58 } 59 60 65 public void getForce(Spring s) { 66 ForceItem item1 = s.item1; 67 ForceItem item2 = s.item2; 68 float length = (s.length < 0 ? params[SPRING_LENGTH] : s.length); 69 float x1 = item1.location[0], y1 = item1.location[1]; 70 float x2 = item2.location[0], y2 = item2.location[1]; 71 float dx = x2-x1, dy = y2-y1; 72 float r = (float)Math.sqrt(dx*dx+dy*dy); 73 if ( r == 0.0 ) { 74 dx = ((float)Math.random()-0.5f) / 50.0f; 75 dy = ((float)Math.random()-0.5f) / 50.0f; 76 r = (float)Math.sqrt(dx*dx+dy*dy); 77 } 78 float d = r-length; 79 float coeff = (s.coeff < 0 ? params[SPRING_COEFF] : s.coeff)*d/r; 80 item1.force[0] += coeff*dx; 81 item1.force[1] += coeff*dy; 82 item2.force[0] += -coeff*dx; 83 item2.force[1] += -coeff*dy; 84 } 85 86 } | Popular Tags |