Returns Euclidean distance on a torus (self-enclosed grid e.g., 2D wraparound map). - CSharp System

CSharp examples for System:Math Geometry

Description

Returns Euclidean distance on a torus (self-enclosed grid e.g., 2D wraparound map).

Demo Code


using System.Drawing;
using System;//from   ww  w .  ja v  a  2s . co  m

public class Main{
        /// <summary>
        /// Returns Euclidean distance on a torus (self-enclosed grid e.g., 2D wraparound map).
        /// </summary>
        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);
        }
}

Related Tutorials