Distance between 2 points on a torus

Posted on 22 July 2009

Looking for a really handy function to calculate the distance between 2 points on torus (think 2-dimensional grid that wraps around)?

Here is a simple C# implementation:

public static double Distance(Point a, Point b, int size)
{
    int x = Math.Abs(b.X - a.X);
    int y = Math.Abs(b.Y - a.Y);

    int minX = Math.Min(x, (size - x));
    minX *= minX;

    int minY = Math.Min(y, (size - y));
    minY *= minY;

    return Math.Sqrt(minX + minY);
}

no comments:( nobody loves me, guess I'll go eat worms!

Leave a Response

Spam protection by WP Captcha-Free

Recent Posts

Tag Cloud

Meta

Homepage and blog of Steve Andrews from Guernsey

Powered by WordPress

Valid XHTML 1.0 Transitional

Copyright ©2009