-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboidCollection.ts
More file actions
41 lines (36 loc) · 1.4 KB
/
boidCollection.ts
File metadata and controls
41 lines (36 loc) · 1.4 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
31
32
33
34
35
36
37
38
39
40
41
import type { Boid } from "./boids";
/** Representerer en samling boids, med muligheten for å hente naboer til en gitt boid basert på en deteksjonsradius */
export interface BoidCollection {
/** Henter alle boids innenfor deteksjonsradiusen til den gitte boiden */
getNeighbors(boid: Boid): Boid[];
/** Setter hvilke boids som er i samlingen */
setBoids(boids: Boid[]): void;
/** Setter deteksjonsradiusen brukt for å finne naboer */
setDetectionRadius(radius: number): void;
}
/** Naiv n^2 implementasjon av {@link BoidCollection} \
* For hver boid, sjekkes alle andre boids for å se om de er innenfor deteksjonsradiusen
*/
export class NaiveBoidCollection implements BoidCollection {
private _boids: Boid[];
private _detectionRadiusSq: number;
constructor(boids: Boid[], detectionRadius: number) {
this._boids = boids;
this._detectionRadiusSq = detectionRadius * detectionRadius;
}
private isInDetectionArea(boid: Boid, other: Boid): boolean {
const vectorToOther = other.position.sub(boid.position);
return vectorToOther.lengthSquared() <= this._detectionRadiusSq;
}
getNeighbors(boid: Boid): Boid[] {
return this._boids.filter(
(other) => other !== boid && this.isInDetectionArea(boid, other)
);
}
setDetectionRadius(radius: number): void {
this._detectionRadiusSq = radius * radius;
}
setBoids(newBoids: Boid[]): void {
this._boids = newBoids;
}
}