KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > force > CircularWallForce


1 package prefuse.util.force;
2
3 /**
4  * Uses a gravitational force model to act as a circular "wall". Can be used to
5  * construct circles which either attract or repel items.
6  *
7  * @author <a HREF="http://jheer.org">jeffrey heer</a>
8  */

9 public class CircularWallForce extends AbstractForce {
10
11     private static String JavaDoc[] pnames = new String JavaDoc[] { "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     /**
21      * Create a new CircularWallForce.
22      * @param gravConst the gravitational constant to use
23      * @param x the center x-coordinate of the circle
24      * @param y the center y-coordinate of the circle
25      * @param r the radius of the circle
26      */

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     /**
38      * Create a new CircularWallForce with default gravitational constant.
39      * @param x the center x-coordinate of the circle
40      * @param y the center y-coordinate of the circle
41      * @param r the radius of the circle
42      */

43     public CircularWallForce(float x, float y, float r) {
44         this(DEFAULT_GRAV_CONSTANT,x,y,r);
45     }
46     
47     /**
48      * Returns true.
49      * @see prefuse.util.force.Force#isItemForce()
50      */

51     public boolean isItemForce() {
52         return true;
53     }
54     
55     /**
56      * @see prefuse.util.force.AbstractForce#getParameterNames()
57      */

58     protected String JavaDoc[] getParameterNames() {
59         return pnames;
60     }
61     
62     /**
63      * @see prefuse.util.force.Force#getForce(prefuse.util.force.ForceItem)
64      */

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         //System.out.println(dx/d+","+dy/d+","+dr+","+v);
81
}
82
83 } // end of class CircularWallForce
84
Popular Tags