Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为什么在我的2d数组中x值垂直,y值水平_Arrays_Swift_Multidimensional Array - Fatal编程技术网

Arrays 为什么在我的2d数组中x值垂直,y值水平

Arrays 为什么在我的2d数组中x值垂直,y值水平,arrays,swift,multidimensional-array,Arrays,Swift,Multidimensional Array,我有一个2d数组,我试图用它来表示我游戏中的网格。它是5x3,看起来像: [0,0][0,1][0,2][0,3][0,4] [1,0][1,1][1,2][1,3][1,4] [2,0][2,1][2,2][2,3][2,4] 问题是,我想将某个点转换到屏幕上的某个位置,但在我的2d数组中,x似乎是垂直值,y是水平值。如果我想在x=1,y=2处添加一个节点,那么我实际上是在-right-2和-down-1之间。但当我想到x时,我想到了左/右水平方向的值。我的设计有问题吗?如何设计2d阵列,使

我有一个2d数组,我试图用它来表示我游戏中的网格。它是5x3,看起来像:

[0,0][0,1][0,2][0,3][0,4]
[1,0][1,1][1,2][1,3][1,4]
[2,0][2,1][2,2][2,3][2,4]
问题是,我想将某个点转换到屏幕上的某个位置,但在我的2d数组中,x似乎是垂直值,y是水平值。如果我想在x=1,y=2处添加一个节点,那么我实际上是在-right-2和-down-1之间。但当我想到x时,我想到了左/右水平方向的值。我的设计有问题吗?如何设计2d阵列,使x值对应于左/右移动和y向上/向下移动

我正在创建阵列,如下所示:

    init(width: Int, height: Int) {
        self.width = width
        self.height = height


        for y in 0..<height {
            map.append([Tile]())

            for _ in 0..<width {
                map[y].append(Tile(blocked: false))
            }
        }
    }

瓦片只是一个物体,保持它的位置和一些其他无关的东西。谢谢

这是存储矩阵的问题

Row-major是指当迭代外部数组产生行时,迭代内部数组产生行内的元素。由于文件或终端的文本输出是逐行完成的,因此这对于打印目的更为可取。然而,这意味着,当以a[b][c]的形式编制索引时,第一个索引b是通常称为y的垂直坐标,第二个索引c是通常称为x的水平坐标,它不遵循通常的x-then-y惯例。但是,您可以通过编写自定义下标运算符轻松解决这一问题,该运算符可以翻转两个索引:

struct Tile {
    let blocked: Bool

    init(blocked: Bool = false) { self.blocked = blocked }
}

extension Tile: CustomDebugStringConvertible {
    var debugDescription: String {
        return self.blocked ? "X" : "-"
    }
}

struct Gameboard {
    var tiles: [[Tile]]

    init(tiles: [[Tile]]) {
        let width = tiles.first?.count
        assert(!tiles.contains(where:) { $0.count != width }, "The tiles must be a square matrix (having all rows of equal length)!")
        self.tiles = tiles
    }

    init(width: Int, height: Int) {
        self.init(tiles: (0..<height).map { row in
            (0..<width).map { column in Tile() }
        })
    }

    subscript(x x: Int, y y: Int) -> Tile {
        return self.tiles[y][x]
    }
}

extension Gameboard: CustomDebugStringConvertible {
    var debugDescription: String {
        let header = (self.tiles.first ?? []).indices.map(String.init).joined(separator: "\t")
        let body = self.tiles.enumerated().map { rowNumber, row in
            let rowText = row.map { $0.debugDescription }.joined(separator: "\t")
            return "\(rowNumber)\t\(rowText)"
        }.joined(separator: "\n")

        return "\t\(header)\n\(body)"
    }
}

let g = Gameboard(width: 5, height: 3)
print(g)
// Example indexing:
let (x, y) = (2, 3)
g[x: x, y; y]

您可以使用列主顺序来回避交换索引的问题,但这意味着如果您希望逐行打印它或使用嵌套数组文字定义它,则需要使用列主顺序。

只需使用arr[y][x]@vacawama访问您的数组。这对我来说有点违反直觉,因为我已经习惯了x,yx和y在图形/几何上不是任何东西-它们只是数组中的值。将这些值转换为视图的部分在哪里?您可以从构建数组的方式声明或反向访问行和列,但我们看不到这一部分。如果你说[1][2],那么你是向下1,向右2,因为这就是数组访问的工作方式,你在这里构建它-行,然后列。你认为1,2是笛卡尔的x,y,但事实并非如此。您真正要做的是访问[row][column]。Swift实际上没有多维数组。您只是在构建一个数组。将它们构建为行的集合是很自然的,因此arr[row][col]是自然顺序,而row是垂直维度。将数组更改为[0,0]、[1,0]、[2,0]。。。如果这对你那么重要的话。
let matrix = [ // Outer array hold rows
   [1, 2, 3] // The inner arrays hold elements within rows
   [4, 5, 6]
   [7, 8, 9]
] // Thus, this matrix is in row-major order.