TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Calculating the Distance Between Points in Wrap Around (Toroidal) Space

106 pointsby Atrix256over 7 years ago

7 comments

eallocover 7 years 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).<p>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<p><pre><code> sqrt(((cos(x1) - cos(x2))**2 + ((sin(x1) - sin(x1))**2 + ((cos(y1) - cos(y2))**2 + ((sin(y1) - sin(y1))**2) </code></pre> This works well for doing things like determining the &quot;nearest&quot; point to another point, and similar operations, and has some other nice properties.<p>(I forget the name of this system, but it is commonly used for calculations involving dihedral angles in MD simulations)
评论 #15381407 未加载
评论 #15381387 未加载
herbsteinover 7 years ago
I really enjoy this kind of post. It&#x27;s short, simple and something most people could figure out by looking at the problem. Even then, it&#x27;s still something that makes you go &#x27;huh, how <i>would</i> you do that?&#x27;. Until the intuition about toroidal space kicks in, that is.
phkahlerover 7 years ago
The may be a simpler way yet. If the coordinates are normalized from [0..1) and represented as 16bit integers with all bits fractional, you can simply take the difference x1-x2 and treat the result as signed. The result will be in the range [-0.5 .. 0.5). If you then square that number to get a 32bit result it will be correct for that component of the distance formula with no conditional code at all. Representation can be everything.
评论 #15381827 未加载
评论 #15381392 未加载
yosypover 7 years ago
Isn&#x27;t the topological manifold of the &quot;wrap around&quot; space described in the post spherical? A toroidal manifold would more complicated since the two generalized coordinates do not commute (order matters).
评论 #15381371 未加载
评论 #15380829 未加载
评论 #15380844 未加载
评论 #15380919 未加载
评论 #15380805 未加载
vogover 7 years ago
This article essentially describes a modulo (%) operator for floats, which is well-defined and completely analogous to the integer modulo operation. I always wondered why these aren&#x27;t part of the FPU command set, or at least part of modern programming languages, e.g.:<p><pre><code> 1.0 % 5.0 = 1.0 6.0 % 5.0 = 1.0 7.0 % 5.0 = 2.0 7.4 % 5.0 = 2.4 7.4 % 1.0 = 0.4 </code></pre> With that in place, the torodial distance becomes a one-liner without any case distinctions (i.e. without ifs).
评论 #15383204 未加载
评论 #15383953 未加载
hellofunkover 7 years ago
This is one of my favorite things to do on the train while on the way to work. I select two random passing objects out the window, and use a general assumption about the train&#x27;s speed. Then I visualize these objects on a toroidal space, and after a few minutes, I draw the rest of the owl.
tomahuntover 7 years ago
This type of calculation is used on a very regular basis in molecular dynamics simulation. The wrap-around space is implemented using periodic boundary conditions. Calculating the distance between points is most often done using what is called the minimum image convention. In one dimension for a &quot;box&quot; of length L the minimum image distance between particles i and j: xij = xj - xi can be calculated through xij &lt;- xij - L*nint(xij&#x2F;L). This avoids any if statements.