将点阵列排列成正方形

时间:2018-03-22 00:40:06

标签: c# coordinates coordinate-systems

所以,如果我有一个均匀放置的点数组。如何将数组排序为正方形,以便points[0]points[n]points[n]points[n+n]为正方形。

我的(不工作)尝试使用C#:

class Point
{
    public int x;
    public int y;
}

class Program
{
    static void Main(string[] args)
    {
        var points = new List<Point>();
        int width = 6;
        int height = 6;
        int n = 9;

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                points.Add(new Point() { x = x, y = y });
            }
        }

        points = points.OrderBy((point) => (point.x - (point.x % (n / 2))) + (point.y - (point.y % (n / 2)))).ToList();

        foreach (Point point in points.Take(n))
        {
            Console.Write($"{point.x},{point.y} ");
        }
        Console.Write("\n");
        // outputs 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0
        // should output 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2

    }
}

0 个答案:

没有答案