KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.util.force;
2
3 import java.util.Iterator JavaDoc;
4
5 /**
6  * Updates velocity and position data using Euler's Method. This is the
7  * simplest and fastest method, but is somewhat inaccurate and less smooth
8  * than more costly approaches.
9  *
10  * @author <a HREF="http://jheer.org">jeffrey heer</a>
11  * @see RungeKuttaIntegrator
12  */

13 public class EulerIntegrator implements Integrator {
14     
15     /**
16      * @see prefuse.util.force.Integrator#integrate(prefuse.util.force.ForceSimulator, long)
17      */

18     public void integrate(ForceSimulator sim, long timestep) {
19         float speedLimit = sim.getSpeedLimit();
20         Iterator JavaDoc iter = sim.getItems();
21         while ( iter.hasNext() ) {
22             ForceItem item = (ForceItem)iter.next();
23             item.location[0] += timestep * item.velocity[0];
24             item.location[1] += timestep * item.velocity[1];
25             float coeff = timestep / item.mass;
26             item.velocity[0] += coeff * item.force[0];
27             item.velocity[1] += coeff * item.force[1];
28             float vx = item.velocity[0];
29             float vy = item.velocity[1];
30             float v = (float)Math.sqrt(vx*vx+vy*vy);
31             if ( v > speedLimit ) {
32                 item.velocity[0] = speedLimit * vx / v;
33                 item.velocity[1] = speedLimit * vy / v;
34             }
35         }
36     }
37
38 } // end of class EulerIntegrator
39
Popular Tags