旋转 3 维数组

我正在使用它们来制作一个功能性的魔方立方体,但我无法将我的头绕在我将如何旋转立方体以查看其他方面的问题上。


我创建了一个包含 27 个立方体的数组,每个立方体有 6 个边,它们的外侧面都写有颜色,我想知道是否有一种更简单的方法可以让我旋转立方体中的所有值而不是全部旋转每个立方体的面并手动将它们更改为所需的面。


这是创建多维数据集的代码,如果您能想到一种方法,请告诉我,提前谢谢。


func constructcube() (newcube [3][3][3][6]string) {

    for d := 0; d < 3; d++ {

        for h := 0; h < 3; h++ {

            for w := 0; w < 3; w++ {

                for f := 0; f < 6; f++ {

                    newcube[d][h][w][f] = " "

                    switch f {

                    case 0:

                        if d == 2 {

                            newcube[d][h][w][f] = "m"

                        }

                    case 1:

                        if w == 0 {

                            newcube[d][h][w][f] = "y"

                        }

                    case 2:

                        if d == 0 {

                            newcube[d][h][w][f] = "r"

                        }

                    case 3:

                        if w == 2 {

                            newcube[d][h][w][f] = "c"

                        }

                    case 4:

                        if h == 0 {

                            newcube[d][h][w][f] = "b"

                        }

                    case 5:

                        if h == 2 {

                            newcube[d][h][w][f] = "g"

                        }

                    }

                }

            }

        }

    }

    return newcube

}


德玛西亚99
浏览 208回答 2
2回答

动漫人物

为了开始表示旋转,我们必须确定一种比我们在屏幕上看到的更简单的有效方式。在计算图形时,机器并不总是渲染场景中的所有内容,而只会渲染视口中的内容。你的问题和其他许多人一样,必须从现实中抽象出来。在围棋术语中,我们将展平立方体并使用切片。切片救援使用切片,我们可以引用数组的块/部分并玩弄它们,移动,切割,相交等。我将使用一个 3x3 的立方体,其中数字“是颜色”,如果你可以通过每组 3 可视化一条虚线数字,你也可以想象把它折叠成一个真正的立方体。// cube represents a 3x3 3d cube// the view is 2d thovar cube = [][]int{&nbsp; &nbsp; {1,1,1,2,2,2,3,3,3,4,4,4},&nbsp; &nbsp; {1,1,1,2,2,2,3,3,3,4,4,4},&nbsp; &nbsp; {1,1,1,2,2,2,3,3,3,4,4,4},&nbsp; &nbsp; {2,2,2},&nbsp; &nbsp; {2,2,2},&nbsp; &nbsp; {2,2,2},&nbsp; &nbsp; {3,3,3},&nbsp; &nbsp; {3,3,3},&nbsp; &nbsp; {3,3,3},&nbsp; &nbsp; {4,4,4},&nbsp; &nbsp; {4,4,4},&nbsp; &nbsp; {4,4,4},}玩立方体意味着完全移动一列或一行,但在视口中显示的只是立方体的前面——我们将在后面的步骤中处理它。向左移动一行,移动该行的每个元素,我们使用切片// moves the cube row to the leftfunc left(r int)&nbsp; {&nbsp; &nbsp; f := cube[r][:3] // slice the first 3 elements of the row&nbsp; &nbsp; cube[r] = append(cube[r][3:], f...) // append those to the back, while removing the first 3 and creating a new slice}这种模式在与立方体的所有交互中重复// moves the cube row to the rightfunc right(r int)&nbsp; {&nbsp; &nbsp; b := cube[r][len(cube[r])-3:]&nbsp; &nbsp; cube[r] = append(b, cube[r][:len(cube[r])-3]...)}向右移动,我们从后面获取元素并将它们放在前面......向前(向上)和向后(向下)移动我们将检索整个列并应用相同的方法// forward moves the cube's (c)column forward(up)func forward(c int)&nbsp; {&nbsp; &nbsp; r := flatten(c) // retrieve the whole column&nbsp; &nbsp; a := r[:3]&nbsp; &nbsp; r = append(r[3:], a...)&nbsp; &nbsp; move(c, r) // apply the new position}// flatten percolates the (c)olumn position and return a flatten slice of its entirefunc flatten(c int) []int {&nbsp; &nbsp; var r []int&nbsp; &nbsp; for _, v := range cube {&nbsp; &nbsp; &nbsp; &nbsp; r = append(r, v[c])&nbsp; &nbsp; }&nbsp; &nbsp; return r}// move moves the cube (c)olumn to (p)ositionfunc move(c int, p []int)&nbsp; {&nbsp; &nbsp; for i := range cube {&nbsp; &nbsp; &nbsp; &nbsp; cube[i][c] = p[i]&nbsp; &nbsp; }}所有这些都很酷且没有嵌套迭代,但这些都不起作用。原因是我们正在改变元素的位置,为了实现我们想要的,我们必须复制、重新创建(重新定位)或进行引用。我使用了没有指针的示例,试图使它们更简洁;您将在下面找到完整的功能示例。请记住,对于此类问题,可能存在许多不同的方法,可能是一种适当且高效的算法。这只是一个示例,说明如何利用 go slices 功能来简化数组操作并避免嵌套迭代等场景。package mainimport "fmt"// cube represents a 3x3 3d cube// the view is 2d thovar cube = []*[]int{&nbsp; &nbsp; {1,1,1,2,2,2,3,3,3,4,4,4},&nbsp; &nbsp; {1,1,1,2,2,2,3,3,3,4,4,4},&nbsp; &nbsp; {1,1,1,2,2,2,3,3,3,4,4,4},&nbsp; &nbsp; {2,2,2},&nbsp; &nbsp; {2,2,2},&nbsp; &nbsp; {2,2,2},&nbsp; &nbsp; {3,3,3},&nbsp; &nbsp; {3,3,3},&nbsp; &nbsp; {3,3,3},&nbsp; &nbsp; {4,4,4},&nbsp; &nbsp; {4,4,4},&nbsp; &nbsp; {4,4,4},}func main() {&nbsp; &nbsp; display()&nbsp; &nbsp; forward(1)&nbsp; &nbsp; left(1)&nbsp; &nbsp; display()&nbsp; &nbsp; backward(1)&nbsp; &nbsp; display()}// moves the cube's (c)olumn backwards(down)func backward(c int)&nbsp; {&nbsp; &nbsp; r := flatten(c)&nbsp; &nbsp; a := r[len(r)-3:]&nbsp; &nbsp; r = append(a, r[:len(r)-3]...)&nbsp; &nbsp; move(c, r)}// forward moves the cube's (c)column forward(up)func forward(c int)&nbsp; {&nbsp; &nbsp; r := flatten(c)&nbsp; &nbsp; a := r[:3]&nbsp; &nbsp; r = append(r[3:], a...)&nbsp; &nbsp; move(c, r)}// moves the cube row to the leftfunc left(r int)&nbsp; {&nbsp; &nbsp; f := (*cube[r])[:3]&nbsp; &nbsp; *cube[r] = append((*cube[r])[3:], f...)}// moves the cube row to the rightfunc right(r int)&nbsp; {&nbsp; &nbsp; b := (*cube[r])[len(*cube[r])-3:]&nbsp; &nbsp; *cube[r] = append(b, (*cube[r])[:len(*cube[r])-3]...)}// display the front view of the cubefunc display()&nbsp; {&nbsp; &nbsp; for y := 0; y < 3; y++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("[")&nbsp; &nbsp; &nbsp; &nbsp; for x := 0; x < 3; x++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d", (*cube[y])[x])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if x < 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("\t")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("]")&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println()}// flatten percolates the (c)olumn position and return a flatten slice of its entirefunc flatten(c int) []int {&nbsp; &nbsp; var r []int&nbsp; &nbsp; for _, v := range cube {&nbsp; &nbsp; &nbsp; &nbsp; r = append(r, (*v)[c])&nbsp; &nbsp; }&nbsp; &nbsp; return r}// move moves the cube (c)olumn to (p)ositionfunc move(c int, p []int)&nbsp; {&nbsp; &nbsp; for i := range cube {&nbsp; &nbsp; &nbsp; &nbsp; (*cube[i])[c] = p[i]&nbsp; &nbsp; }}免责声明该脚本依赖于cube具有 3x3 正方形面的变量的存在。没有进行索引检查,警告out of bounds恐慌。https://play.golang.com/p/Z7azOi0omP3

哆啦的时光机

我最终弄清楚如何使用称为“旋转矩阵”的东西来做到这一点,您可以在图中获取每个点的坐标并将其从 {i, j, k} 更改为 {i, k, -j} 并旋转3维对象。我不得不使用一些测试输入并将其与实际输出对齐,并且由于在这种情况下值不能为负,我发现您只需从立方体侧的上限中减去它们,即 2 .
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go