Ejemplo n.º 1
0
        static CVec[][] InitTilesByDistance(int max)
        {
            var ts = new List <CVec> [max + 1];

            for (var i = 0; i < max + 1; i++)
            {
                ts [i] = new List <CVec>();
            }

            for (var j = -max; j <= max; j++)
            {
                for (var i = -max; i <= max; i++)
                {
                    if (max * max >= 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 hashcode 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());
        }