-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflock.py
More file actions
30 lines (25 loc) · 1.01 KB
/
flock.py
File metadata and controls
30 lines (25 loc) · 1.01 KB
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
from boid import Boid
from typing import List
import numpy as np
from itertools import compress
class Flock:
boids: List[Boid]
protected_range: int
visible_range: int
def __init__(
self,
boids: List[Boid],
protected_range: int = 20,
visible_range: int = 20
) -> None:
self.boids = boids
self.protected_range = protected_range
self.visible_range = visible_range
def update(self, x_dim: int, y_dim: int, margin: int) -> None:
for i, boid in enumerate(self.boids):
dist = np.array([boid.manhattan_dist(other_b) for other_b in self.boids])
dist[i] = self.visible_range + self.protected_range
boid.check_separation(list(compress(self.boids, dist < self.protected_range)))
boid.check_alignment(list(compress(self.boids, dist < self.visible_range)))
boid.check_cohesion(list(compress(self.boids, dist < self.visible_range)))
boid.update(x_dim, y_dim, margin)