public static Square[,] SquareGrid(int[,] map, float squareSize)
    {
        int   nodeCountX = map.GetLength(0);
        int   nodeCountY = map.GetLength(1);
        float mapWidth   = nodeCountX * squareSize;
        float mapHeight  = nodeCountY * squareSize;

        ControlNode[,] controlNodes = new ControlNode[nodeCountX, nodeCountY];
        for (int x = 0; x < controlNodes.GetLength(0); x++)
        {
            for (int y = 0; y < controlNodes.GetLength(1); y++)
            {
                Vector3 pos = new Vector3(-mapWidth / 2 + x * squareSize + squareSize / 2, -mapHeight / 2 + y * squareSize + squareSize / 2);
                controlNodes[x, y] = new ControlNode(pos, map[x, y] == 1, squareSize);
            }
        }

        Square[,] squares = new Square[nodeCountX - 1, nodeCountY - 1];
        for (int x = 0; x < squares.GetLength(0); x++)
        {
            for (int y = 0; y < squares.GetLength(1); y++)
            {
                squares[x, y] = new Square(controlNodes[x, y + 1], controlNodes[x + 1, y + 1], controlNodes[x + 1, y], controlNodes[x, y]);
            }
        }

        return(squares);
    }
        private ControlNode[,] CreateControlNodes(int[,] map, float squareSize)
        {
            int   horizontalNodeCount = map.GetLength(0);
            int   verticalNodeCount   = map.GetLength(1);
            float mapWidth            = horizontalNodeCount * squareSize;
            float mapHeight           = verticalNodeCount * squareSize;

            ControlNode[,] result = new ControlNode[horizontalNodeCount, verticalNodeCount];

            for (int i = 0; i < result.GetLength(0); i++)
            {
                for (int j = 0; j < result.GetLength(1); j++)
                {
                    Vector3 position = new Vector3(
                        -mapWidth / 2 + i * squareSize + squareSize / 2,
                        0,
                        -mapHeight / 2 + j * squareSize + squareSize / 2);

                    result[i, j] = new ControlNode(position, map[i, j] == 1, squareSize);
                }
            }

            return(result);
        }