OpenRA.MapGrid.CreateTilesByDistance C# (CSharp) Method

CreateTilesByDistance() private method

private CreateTilesByDistance ( ) : OpenRA.CVec[][]
return OpenRA.CVec[][]
        CVec[][] CreateTilesByDistance()
        {
            var ts = new List<CVec>[MaximumTileSearchRange + 1];
            for (var i = 0; i < MaximumTileSearchRange + 1; i++)
                ts[i] = new List<CVec>();

            for (var j = -MaximumTileSearchRange; j <= MaximumTileSearchRange; j++)
                for (var i = -MaximumTileSearchRange; i <= MaximumTileSearchRange; i++)
                    if (MaximumTileSearchRange * MaximumTileSearchRange >= i * i + j * j)
                        ts[Exts.ISqrt(i * i + j * j, Exts.ISqrtRoundMode.Ceiling)].Add(new CVec(i, j));

            // Sort each integer-distance group by the actual distance
            foreach (var list in ts)
            {
                list.Sort((a, b) =>
                {
                    var result = a.LengthSquared.CompareTo(b.LengthSquared);
                    if (result != 0)
                        return result;

                    // If the lengths are equal, use other means to sort them.
                    // Try the hash code first because it gives more
                    // random-appearing results than X or Y that would always
                    // prefer the leftmost/topmost position.
                    result = a.GetHashCode().CompareTo(b.GetHashCode());
                    if (result != 0)
                        return result;

                    result = a.X.CompareTo(b.X);
                    if (result != 0)
                        return result;

                    return a.Y.CompareTo(b.Y);
                });
            }

            return ts.Select(list => list.ToArray()).ToArray();
        }