use of edu.princeton.cs.algs4.Particle in project AlgorithmsSolutions by Allenskoo856.
the class CollisionSystem method main.
public static void main(String[] args) {
StdDraw.show(0);
int N = Integer.parseInt(args[0]);
Particle[] particles = new Particle[N];
for (int i = 0; i < N; i++) {
particles[i] = new Particle();
}
CollisionSystem system = new CollisionSystem(particles);
system.simulate(10000, 0.5);
}
use of edu.princeton.cs.algs4.Particle in project AlgorithmsSolutions by Allenskoo856.
the class CollisionSystem method simulate.
public void simulate(double limit, double Hz) {
pq = new MinPQ<Event>();
for (int i = 0; i < particles.length; i++) {
predictCollisions(particles[i], limit);
}
pq.insert(new Event(0, null, null));
while (!pq.isEmpty()) {
Event event = pq.delMin();
if (!event.isValid()) {
continue;
}
for (int i = 0; i < particles.length; i++) {
particles[i].move(event.time - t);
}
t = event.time;
Particle a = event.a, b = event.b;
if (a != null && b != null) {
a.bounceOff(b);
} else if (a != null && b == null) {
a.bounceOffVerticalWall();
} else if (a == null && b != null) {
b.bounceOffHorizontalWall();
} else if (a == null && b == null) {
redraw(limit, Hz);
}
predictCollisions(a, limit);
predictCollisions(b, limit);
}
}
Aggregations