Hacker News new | ask | show | jobs
by ealloc 3184 days ago
A trick for dealing with periodic coordinates, not discussed in the article, is to convert each periodic coordinate (eg, an angle theta) into a pair of cartesian coordinates (x,y) on the circle, and then compute the distance in cartesian space. Ie, convert to x = cos(theta), y = sin(theta).

In the 2d example in the post, you would rescale the coordinates so the unit cell is from (-pi,pi) in both dimensions, and then the distance formula would be

    sqrt(((cos(x1) - cos(x2))**2 + ((sin(x1) - sin(x1))**2 + ((cos(y1) - cos(y2))**2 + ((sin(y1) - sin(y1))**2)
This works well for doing things like determining the "nearest" point to another point, and similar operations, and has some other nice properties.

(I forget the name of this system, but it is commonly used for calculations involving dihedral angles in MD simulations)

2 comments

For anyone curious, I’m guessing “MD simulation” means https://en.wikipedia.org/wiki/Molecular_dynamics

Folks who use this kind of distance measure might note that it also works well as a metric between angles (i.e. rotations) or between points on the projective line. For instance, see https://www.youtube.com/watch?v=oJAn--vsAzc (unfortunately not too self-contained a source)

It depends on how you define "distance". My initial thought when reading your comment was that you couldn't possibly use the Euclidean distance because you'd be drawing a path between two points on the circle that involves points _not_ on the circle.
Using the Euclidean distance (Cartesian distance? I dunno) works because it's a monotonic underestimate of the distance along the circle. So you know if cartesian_dist(A, B) > cartesian_dist(A, C) then circle_dist(A, B) > circle_dist(A, C).
The original problem, though, is about computing distance, not about comparing distances. Comparing distances is just used as an intermediate step in the initial method given.