Skip to content

Commit

Permalink
Merge pull request #4 from nbvdkamp/optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
cubedhuang authored Nov 17, 2024
2 parents 43950c0 + 2b977a9 commit 48238dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
22 changes: 14 additions & 8 deletions js/boid.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ class Boid extends V2D {
}

neighbors(flock) {
const cand = flock.candidates(this);
const cands = flock.candidates(this);
const ns = [];
const ds = [];

let step = opt.accuracy === 0 ? 1 : Math.ceil(cand.length / opt.accuracy);
const candidate_count = cands.map(c => c.length).reduce((a, b) => a + b, 0);
let step = opt.accuracy === 0 ? 1 : Math.ceil(candidate_count / opt.accuracy);
let i = Math.floor(random(step));

for (let i = Math.floor(random(step)); i < cand.length; i += step) {
if (!cand[i]) break;
const d = this.sqrDist(cand[i]);
if (d < g.sqVis && this !== cand[i]) {
ns.push(cand[i]);
ds.push(d);
for (const c of cands) {
for (; i < c.length; i += step) {
if (this === c[i]) continue;

const d = this.sqrDist(c[i]);
if (d < g.sqVis) {
ns.push(c[i]);
ds.push(d);
}
}
i -= c.length;
}

return [ns, ds];
Expand Down
3 changes: 2 additions & 1 deletion js/flock.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ class Flock {
}

_b(r, c, a) {
if (this.buckets[r]?.[c]) a.push(...this.buckets[r][c]);
if (this.buckets[r]?.[c]) a.push(this.buckets[r][c]);
}

// Returns a list of lists of boids, where each sublist contains the boids in a nearby cell
candidates(boid) {
const cand = [];

Expand Down

0 comments on commit 48238dc

Please sign in to comment.