Use broadcasting to return a square matrix of Boolean values with True at position [i,j] and [j,i] if circles i and j intersect.
(x, y, R)
| 33 | import time |
| 34 | |
| 35 | def collision_matrix(x, y, R): |
| 36 | """ |
| 37 | Use broadcasting to return a square matrix of Boolean |
| 38 | values with True at position [i,j] and [j,i] if |
| 39 | circles i and j intersect. |
| 40 | """ |
| 41 | dx = x[:,np.newaxis] - x[np.newaxis,:] |
| 42 | dy = y[:,np.newaxis] - y[np.newaxis,:] |
| 43 | sep = np.sqrt( dx**2 + dy**2 ) |
| 44 | sum_r = R[:,np.newaxis] + R[np.newaxis,:] |
| 45 | return sep < sum_r |
| 46 | |
| 47 | def run_sim(N, n_iter): |
| 48 | box_x, box_y = 10., 6. |