boids
boids copied to clipboard
nClosestBoids: use Schwartzian transform
I just watched Destin's video. Pretty cool how you explained Boid's algorithm. I got curious and had a look at boid.js. This TODO caught my attention even though the function is never called. But yeah, I changed it to use a Schwartzian transform.
My Javascript skills are mediocre at best, so don't expect the most beautiful and idiomatic syntax. I wish the Javascript standard had something like Ruby's sort_by
, but apparently that's not the case.
Stay safe.
Another easy improvement is to use the square of the distance instead of the distance.
We don't need all those sqrt
s.
Also, in the distance function there are repeated subtractions, but the interpreter might optimize that out - I'm not sure.