Help me make this O(n^2) function faster?

You can get fast queries for points within a fixed radius via e.g.

However, you need a hybrid datastructure that combines interval trees and metric trees to get good perf. I don’t know how this structure needs to look like (and would be unsurprised if this turned out to be a fun little research problem).