-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionSystemOLD.java
217 lines (185 loc) · 8.1 KB
/
CollisionSystemOLD.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/******************************************************************************
* Compilation: javac CollisionSystem.java
* Execution: java CollisionSystem n (n random particles)
* java CollisionSystem < input.txt (from a file)
* Dependencies: StdDraw.java Particle.java MinPQ.java
* Data files: https://algs4.cs.princeton.edu/61event/diffusion.txt
* https://algs4.cs.princeton.edu/61event/diffusion2.txt
* https://algs4.cs.princeton.edu/61event/diffusion3.txt
* https://algs4.cs.princeton.edu/61event/brownian.txt
* https://algs4.cs.princeton.edu/61event/brownian2.txt
* https://algs4.cs.princeton.edu/61event/billiards5.txt
* https://algs4.cs.princeton.edu/61event/pendulum.txt
*
* Creates n random particles and simulates their motion according
* to the laws of elastic collisions.
*
******************************************************************************/
import edu.princeton.cs.algs4.MinPQ;
import java.awt.Color;
/**
* The {@code CollisionSystem} class represents a collection of particles
* moving in the unit box, according to the laws of elastic collision.
* This event-based simulation relies on a priority queue.
* <p>
* For additional documentation,
* see <a href="https://algs4.cs.princeton.edu/61event">Section 6.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class CollisionSystemOLD {
private static final double HZ = 0.5; // number of redraw events per clock tick
private MinPQ<Event> pq; // the priority queue
private double t = 0.0; // simulation clock time
private ParticleOLD[] particles; // the array of particles
/**
* Initializes a system with the specified collection of particles.
* The individual particles will be mutated during the simulation.
*
* @param particles the array of particles
*/
public CollisionSystemOLD(ParticleOLD[] particles) {
this.particles = particles.clone(); // defensive copy
}
// updates priority queue with all new events for particle a
private void predict(ParticleOLD a, double limit) {
if (a == null) return;
// particle-particle collisions
for (int i = 0; i < particles.length; i++) {
double dt = a.timeToHit(particles[i]);
if (t + dt <= limit)
pq.insert(new Event(t + dt, a, particles[i]));
}
// particle-wall collisions
double dtX = a.timeToHitVerticalWall();
double dtY = a.timeToHitHorizontalWall();
if (t + dtX <= limit) pq.insert(new Event(t + dtX, a, null));
if (t + dtY <= limit) pq.insert(new Event(t + dtY, null, a));
}
// redraw all particles
private void redraw(double limit) {
StdDraw.clear();
for (int i = 0; i < particles.length; i++) {
particles[i].draw();
}
StdDraw.show();
StdDraw.pause(20);
if (t < limit) {
pq.insert(new Event(t + 1.0 / HZ, null, null));
}
}
/**
* Simulates the system of particles for the specified amount of time.
*
* @param limit the amount of time
*/
public void simulate(double limit) {
// initialize PQ with collision events and redraw event
pq = new MinPQ<Event>();
for (int i = 0; i < particles.length; i++) {
predict(particles[i], limit);
}
pq.insert(new Event(0, null, null)); // redraw event
// the main event-driven simulation loop
while (!pq.isEmpty()) {
// get impending event, discard if invalidated
Event e = pq.delMin();
if (!e.isValid()) continue;
ParticleOLD a = e.a;
ParticleOLD b = e.b;
// physical collision, so update positions, and then simulation clock
for (int i = 0; i < particles.length; i++)
particles[i].move(e.time - t);
t = e.time;
// process event
if (a != null && b != null) a.bounceOff(b); // particle-particle collision
else if (a != null && b == null) a.bounceOffVerticalWall(); // particle-wall collision
else if (a == null && b != null) b.bounceOffHorizontalWall(); // particle-wall collision
else if (a == null && b == null) redraw(limit); // redraw event
// update the priority queue with new collisions involving a or b
predict(a, limit);
predict(b, limit);
}
}
/***************************************************************************
* An event during a particle collision simulation. Each event contains
* the time at which it will occur (assuming no supervening actions)
* and the particles a and b involved.
*
* - a and b both null: redraw event
* - a null, b not null: collision with vertical wall
* - a not null, b null: collision with horizontal wall
* - a and b both not null: binary collision between a and b
*
***************************************************************************/
private static class Event implements Comparable<Event> {
private final double time; // time that event is scheduled to occur
private final ParticleOLD a, b; // particles involved in event, possibly null
private final int countA, countB; // collision counts at event creation
// create a new event to occur at time t involving a and b
public Event(double t, ParticleOLD a, ParticleOLD b) {
this.time = t;
this.a = a;
this.b = b;
if (a != null) countA = a.count();
else countA = -1;
if (b != null) countB = b.count();
else countB = -1;
}
// compare times when two events will occur
public int compareTo(Event that) {
return Double.compare(this.time, that.time);
}
// has any collision occurred between when event was created and now?
public boolean isValid() {
if (a != null && a.count() != countA) return false;
if (b != null && b.count() != countB) return false;
return true;
}
}
/**
* Unit tests the {@code CollisionSystem} data type.
* Reads in the particle collision system from a standard input
* (or generates {@code N} random particles if a command-line integer
* is specified); simulates the system.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
StdDraw.setCanvasSize(600, 600);
// enable double buffering
StdDraw.enableDoubleBuffering();
// the array of particles
ParticleOLD[] particles;
// create n random particles
if (args.length == 1) {
int n = Integer.parseInt(args[0]);
particles = new ParticleOLD[n];
for (int i = 0; i < n; i++)
particles[i] = new ParticleOLD();
}
// or read from standard input
else {
int n = StdIn.readInt();
particles = new ParticleOLD[n];
for (int i = 0; i < n; i++) {
double rx = StdIn.readDouble();
double ry = StdIn.readDouble();
double vx = StdIn.readDouble();
double vy = StdIn.readDouble();
double radius = StdIn.readDouble();
double mass = StdIn.readDouble();
int r = StdIn.readInt();
int g = StdIn.readInt();
int b = StdIn.readInt();
Color color = new Color(r, g, b);
particles[i] = new ParticleOLD(rx, ry, vx, vy, radius, mass, color);
}
}
// create collision system and advance
CollisionSystemOLD system = new CollisionSystemOLD(particles);
system.simulate(10000);
}
}