xcode swift 3D 阵列
xcode swift 3D arrays
我正在尝试构建 3D 阵列。这是我从二维数组修改的代码。 3D 数组不会根据我定义的值打印出我的数组。我的下标中的 getter 和 setter 有问题。有人可以给我建议吗?
import UIKit
class Array3D {
var zs:Int, ys:Int, xs:Int
var matrix: [Int]
init(zs: Int, ys:Int, xs:Int) {
self.zs = zs
self.ys = ys
self.xs = xs
matrix = Array(count:zs*ys*xs, repeatedValue:0)
}
subscript(z:Int, ys:Int, xs:Int) -> Int {
get {
return matrix[ zs * ys * xs + ys ]
}
set {
matrix[ zs* ys * xs + ys ] = newValue
}
}
func zsCount() -> Int {
return self.zs
}
func colCount() -> Int {
return self.ys
}
func rowCount() -> Int {
return self.xs
}
}
var dungeon = Array3D(zs: 5, ys: 5, xs: 5)
dungeon[1,0,0] = 1
dungeon[0,4,0] = 2
dungeon[0,0,4] = 3
dungeon[0,4,4] = 4
print("You created a dungeon with \(dungeon.zsCount()) z value \(dungeon.colCount()) columns and \(dungeon.rowCount()) rows. Here is the dungeon visually:\n\n")
for z in 0..<5 {
for y in 0..<5 {
for x in 0..<5 {
print(String(dungeon[z,x,y]))
}
print("\n")
}
print("\n")
}
下标中索引的计算方式有误。要将 (z, y, x)
转换为线性坐标,您可以使用:
z * ys * xs + y * xs + x
所以下标应该固定如下:
subscript(z:Int, y:Int, x:Int) -> Int {
get {
return matrix[ z * ys * xs + y * xs + x ]
}
set {
matrix[ z * ys * xs + y * xs + x ] = newValue
}
}
注意循环内 print 语句中的索引是倒置的:
print(String(dungeon[z,x,y]))
应该是[z, y, x]
而不是
进行这些更改后,这是我在操场上获得的输出:
00003
00000
00000
00000
20004
10000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
附录
zs
、ys
和 xs
表示 3d 矩阵的大小:有 xs
列、ys
行和 zs
平面。每列的大小为xs
,每个平面的大小为xs * ys
.
对于 y = 0 和 z = 0,数组中对应于 x = 3 的元素是第 4 个元素,索引为 3(数组从零开始,以及 3d 坐标)。如果y = 1,则元素为3 + 一行的大小,即3 + 5 = 8。为了更通用
x + xs * y = 3 + 1 * 5 = 8
每个平面的大小改为 xs * ys,对应于 25。因此 z 坐标必须乘以该大小。这导致了我使用的公式:
z * ys * xs + y * xs + x
在 z = 0 的情况下,坐标 (0, 1, 3):
0 + 1 * 5 + 3 = 8
若z = 1,则坐标为(1, 1, 3),计算结果为:
1 * 5 * 5 + 1 * 5 + 3 = 33
等等。
如果这听起来很复杂,请查看代码生成的输出,它由 5 个块组成,每行 5 行,每个块包含 5 个数字。如果将其转换为单行(通过删除换行符),则可以通过从零到所需元素(从左到右)计数来获取数组索引。您可以通过向索引添加 5 跳转到下一行,通过添加 25 跳转到下一个平面。
希望能说明一点。
我正在尝试构建 3D 阵列。这是我从二维数组修改的代码。 3D 数组不会根据我定义的值打印出我的数组。我的下标中的 getter 和 setter 有问题。有人可以给我建议吗?
import UIKit
class Array3D {
var zs:Int, ys:Int, xs:Int
var matrix: [Int]
init(zs: Int, ys:Int, xs:Int) {
self.zs = zs
self.ys = ys
self.xs = xs
matrix = Array(count:zs*ys*xs, repeatedValue:0)
}
subscript(z:Int, ys:Int, xs:Int) -> Int {
get {
return matrix[ zs * ys * xs + ys ]
}
set {
matrix[ zs* ys * xs + ys ] = newValue
}
}
func zsCount() -> Int {
return self.zs
}
func colCount() -> Int {
return self.ys
}
func rowCount() -> Int {
return self.xs
}
}
var dungeon = Array3D(zs: 5, ys: 5, xs: 5)
dungeon[1,0,0] = 1
dungeon[0,4,0] = 2
dungeon[0,0,4] = 3
dungeon[0,4,4] = 4
print("You created a dungeon with \(dungeon.zsCount()) z value \(dungeon.colCount()) columns and \(dungeon.rowCount()) rows. Here is the dungeon visually:\n\n")
for z in 0..<5 {
for y in 0..<5 {
for x in 0..<5 {
print(String(dungeon[z,x,y]))
}
print("\n")
}
print("\n")
}
下标中索引的计算方式有误。要将 (z, y, x)
转换为线性坐标,您可以使用:
z * ys * xs + y * xs + x
所以下标应该固定如下:
subscript(z:Int, y:Int, x:Int) -> Int {
get {
return matrix[ z * ys * xs + y * xs + x ]
}
set {
matrix[ z * ys * xs + y * xs + x ] = newValue
}
}
注意循环内 print 语句中的索引是倒置的:
print(String(dungeon[z,x,y]))
应该是[z, y, x]
而不是
进行这些更改后,这是我在操场上获得的输出:
00003
00000
00000
00000
20004
10000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
附录
zs
、ys
和 xs
表示 3d 矩阵的大小:有 xs
列、ys
行和 zs
平面。每列的大小为xs
,每个平面的大小为xs * ys
.
对于 y = 0 和 z = 0,数组中对应于 x = 3 的元素是第 4 个元素,索引为 3(数组从零开始,以及 3d 坐标)。如果y = 1,则元素为3 + 一行的大小,即3 + 5 = 8。为了更通用
x + xs * y = 3 + 1 * 5 = 8
每个平面的大小改为 xs * ys,对应于 25。因此 z 坐标必须乘以该大小。这导致了我使用的公式:
z * ys * xs + y * xs + x
在 z = 0 的情况下,坐标 (0, 1, 3):
0 + 1 * 5 + 3 = 8
若z = 1,则坐标为(1, 1, 3),计算结果为:
1 * 5 * 5 + 1 * 5 + 3 = 33
等等。
如果这听起来很复杂,请查看代码生成的输出,它由 5 个块组成,每行 5 行,每个块包含 5 个数字。如果将其转换为单行(通过删除换行符),则可以通过从零到所需元素(从左到右)计数来获取数组索引。您可以通过向索引添加 5 跳转到下一行,通过添加 25 跳转到下一个平面。
希望能说明一点。