1 package prefuse.util.force; 2 3 9 public class CircularWallForce extends AbstractForce { 10 11 private static String [] pnames = new String [] { "GravitationalConstant" }; 12 13 public static final float DEFAULT_GRAV_CONSTANT = -0.1f; 14 public static final float DEFAULT_MIN_GRAV_CONSTANT = -1.0f; 15 public static final float DEFAULT_MAX_GRAV_CONSTANT = 1.0f; 16 public static final int GRAVITATIONAL_CONST = 0; 17 18 private float x, y, r; 19 20 27 public CircularWallForce(float gravConst, 28 float x, float y, float r) 29 { 30 params = new float[] { gravConst }; 31 minValues = new float[] { DEFAULT_MIN_GRAV_CONSTANT }; 32 maxValues = new float[] { DEFAULT_MAX_GRAV_CONSTANT }; 33 this.x = x; this.y = y; 34 this.r = r; 35 } 36 37 43 public CircularWallForce(float x, float y, float r) { 44 this(DEFAULT_GRAV_CONSTANT,x,y,r); 45 } 46 47 51 public boolean isItemForce() { 52 return true; 53 } 54 55 58 protected String [] getParameterNames() { 59 return pnames; 60 } 61 62 65 public void getForce(ForceItem item) { 66 float[] n = item.location; 67 float dx = x-n[0]; 68 float dy = y-n[1]; 69 float d = (float)Math.sqrt(dx*dx+dy*dy); 70 float dr = r-d; 71 float c = dr > 0 ? -1 : 1; 72 float v = c*params[GRAVITATIONAL_CONST]*item.mass / (dr*dr); 73 if ( d == 0.0 ) { 74 dx = ((float)Math.random()-0.5f) / 50.0f; 75 dy = ((float)Math.random()-0.5f) / 50.0f; 76 d = (float)Math.sqrt(dx*dx+dy*dy); 77 } 78 item.force[0] += v*dx/d; 79 item.force[1] += v*dy/d; 80 } 82 83 } | Popular Tags |