KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.util.force;
2
3 import java.awt.geom.Line2D JavaDoc;
4
5 /**
6  * Uses a gravitational force model to act as a "wall". Can be used to
7  * construct line segments which either attract or repel items.
8  *
9  * @author <a HREF="http://jheer.org">jeffrey heer</a>
10  */

11 public class WallForce extends AbstractForce {
12
13     private static String JavaDoc[] pnames = new String JavaDoc[] { "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     /**
24      * Create a new WallForce.
25      * @param gravConst the gravitational constant of the wall
26      * @param x1 the first x-coordinate of the wall
27      * @param y1 the first y-coordinate of the wall
28      * @param x2 the second x-coordinate of the wall
29      * @param y2 the second y-coordinate of the wall
30      */

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     /**
48      * Create a new WallForce with default gravitational constant.
49      * @param x1 the first x-coordinate of the wall
50      * @param y1 the first y-coordinate of the wall
51      * @param x2 the second x-coordinate of the wall
52      * @param y2 the second y-coordinate of the wall
53      */

54     public WallForce(float x1, float y1, float x2, float y2) {
55         this(DEFAULT_GRAV_CONSTANT,x1,y1,x2,y2);
56     }
57     
58     /**
59      * Returns true.
60      * @see prefuse.util.force.Force#isItemForce()
61      */

62     public boolean isItemForce() {
63         return true;
64     }
65     
66     /**
67      * @see prefuse.util.force.AbstractForce#getParameterNames()
68      */

69     protected String JavaDoc[] getParameterNames() {
70         return pnames;
71     }
72     
73     /**
74      * @see prefuse.util.force.Force#getForce(prefuse.util.force.ForceItem)
75      */

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 } // end of class WallForce
89
Popular Tags