1 package prefuse.util.force; 2 3 import java.awt.geom.Line2D ; 4 5 11 public class WallForce extends AbstractForce { 12 13 private static String [] pnames = new String [] { "GravitationalConstant" }; 14 15 public static final float DEFAULT_GRAV_CONSTANT = -0.1f; 16 public static final float DEFAULT_MIN_GRAV_CONSTANT = -1.0f; 17 public static final float DEFAULT_MAX_GRAV_CONSTANT = 1.0f; 18 public static final int GRAVITATIONAL_CONST = 0; 19 20 private float x1, y1, x2, y2; 21 private float dx, dy; 22 23 31 public WallForce(float gravConst, 32 float x1, float y1, float x2, float y2) 33 { 34 params = new float[] { gravConst }; 35 minValues = new float[] { DEFAULT_MIN_GRAV_CONSTANT }; 36 maxValues = new float[] { DEFAULT_MAX_GRAV_CONSTANT }; 37 38 this.x1 = x1; this.y1 = y1; 39 this.x2 = x2; this.y2 = y2; 40 dx = x2-x1; 41 dy = y2-y1; 42 float r = (float)Math.sqrt(dx*dx+dy*dy); 43 if ( dx != 0.0 ) dx /= r; 44 if ( dy != 0.0 ) dy /= r; 45 } 46 47 54 public WallForce(float x1, float y1, float x2, float y2) { 55 this(DEFAULT_GRAV_CONSTANT,x1,y1,x2,y2); 56 } 57 58 62 public boolean isItemForce() { 63 return true; 64 } 65 66 69 protected String [] getParameterNames() { 70 return pnames; 71 } 72 73 76 public void getForce(ForceItem item) { 77 float[] n = item.location; 78 int ccw = Line2D.relativeCCW(x1,y1,x2,y2,n[0],n[1]); 79 float r = (float)Line2D.ptSegDist(x1,y1,x2,y2,n[0],n[1]); 80 if ( r == 0.0 ) r = (float)Math.random() / 100.0f; 81 float v = params[GRAVITATIONAL_CONST]*item.mass / (r*r*r); 82 if ( n[0] >= Math.min(x1,x2) && n[0] <= Math.max(x1,x2) ) 83 item.force[1] += ccw*v*dx; 84 if ( n[1] >= Math.min(y1,y2) && n[1] <= Math.max(y1,y2) ) 85 item.force[0] += -1*ccw*v*dy; 86 } 87 88 } | Popular Tags |