登录
首页 >  Golang >  Go问答

对 3 维数组进行旋转

来源:stackoverflow

时间:2024-03-11 09:24:26 360浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《对 3 维数组进行旋转》,聊聊,我们一起来看看吧!

问题内容

我正在使用它们制作一个功能性的 rubix 立方体,但我无法理解如何旋转立方体以查看其他面。

我创建了一个包含 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
}

解决方案


要开始表示旋转,我们必须找到一种比我们在屏幕上看到的更简单的有效方法。在计算图形时,机器并不总是渲染场景中的所有内容,而仅渲染视口中的内容。你的问题和其他许多问题一样,必须从现实中抽象出来。用 go 术语来说,我们将压平立方体并使用切片。

Slices to the rescue

通过切片,我们可以引用数组的块/部分并使用它们、移动、剪切、相交等。 我将使用一个 3x3 的立方体,其中数字“是颜色”,如果您可以想象每组 3 个数字的虚线,您也可以想象将其折叠成一个真正的立方体。

// cube represents a 3x3 3d cube
// the view is 2d tho
var cube = [][]int{
    {1,1,1,2,2,2,3,3,3,4,4,4},
    {1,1,1,2,2,2,3,3,3,4,4,4},
    {1,1,1,2,2,2,3,3,3,4,4,4},
    {2,2,2},
    {2,2,2},
    {2,2,2},
    {3,3,3},
    {3,3,3},
    {3,3,3},
    {4,4,4},
    {4,4,4},
    {4,4,4},
}

使用立方体意味着完全移动一列或一行,但视口中显示的只是立方体的前面 - 我们将在后面的步骤中处理它。 向左移动一行,移动该行的每个元素,我们使用切片

// moves the cube row to the left
func left(r int)  {
    f := cube[r][:3] // slice the first 3 elements of the row
    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 right
func right(r int)  {
    b := cube[r][len(cube[r])-3:]
    cube[r] = append(b, cube[r][:len(cube[r])-3]...)
}

要向右移动,我们从后面取出元素并将它们添加到前面...... 对于向前(向上)和向后(向下)移动,我们将检索整个列并应用相同的方法

// forward moves the cube's (c)column forward(up)
func forward(c int)  {
    r := flatten(c) // retrieve the whole column
    a := r[:3]
    r = append(r[3:], a...)
    move(c, r) // apply the new position
}

// flatten percolates the (c)olumn position and return a flatten slice of its entire
func flatten(c int) []int {
    var r []int
    for _, v := range cube {
        r = append(r, v[c])
    }

    return r
}

// move moves the cube (c)olumn to (p)osition
func move(c int, p []int)  {
    for i := range cube {
        cube[i][c] = p[i]
    }
}

所有这些都很酷并且没有嵌套迭代,但是这些都不起作用。原因是我们正在更改元素的位置,为了实现我们想要的效果,我们必须复制、重新创建(重新定位)或进行引用。

我使用了没有指针的示例,试图使它们更清晰一些; 下面您将找到完整功能的示例。

请记住,对于此类问题可能存在许多不同的方法,可能是一种适当且高效的算法。这里只是一个如何利用 go slice 功能来简化数组操作并避免嵌套迭代等场景的示例。

package main

import "fmt"

// cube represents a 3x3 3d cube
// the view is 2d tho
var cube = []*[]int{
    {1,1,1,2,2,2,3,3,3,4,4,4},
    {1,1,1,2,2,2,3,3,3,4,4,4},
    {1,1,1,2,2,2,3,3,3,4,4,4},
    {2,2,2},
    {2,2,2},
    {2,2,2},
    {3,3,3},
    {3,3,3},
    {3,3,3},
    {4,4,4},
    {4,4,4},
    {4,4,4},
}

func main() {
    display()
    forward(1)
    left(1)
    display()
    backward(1)
    display()
}

// moves the cube's (c)olumn backwards(down)
func backward(c int)  {
    r := flatten(c)
    a := r[len(r)-3:]
    r = append(a, r[:len(r)-3]...)
    move(c, r)
}

// forward moves the cube's (c)column forward(up)
func forward(c int)  {
    r := flatten(c)
    a := r[:3]
    r = append(r[3:], a...)
    move(c, r)
}

// moves the cube row to the left
func left(r int)  {
    f := (*cube[r])[:3]
    *cube[r] = append((*cube[r])[3:], f...)
}

// moves the cube row to the right
func right(r int)  {
    b := (*cube[r])[len(*cube[r])-3:]
    *cube[r] = append(b, (*cube[r])[:len(*cube[r])-3]...)
}

// display the front view of the cube
func display()  {
    for y := 0; y < 3; y++ {
        fmt.Printf("[")
        for x := 0; x < 3; x++ {
            fmt.Printf("%d", (*cube[y])[x])
            if x < 2 {
                fmt.Printf("\t")
            }
        }
        fmt.Println("]")
    }
    fmt.Println()
}

// flatten percolates the (c)olumn position and return a flatten slice of its entire
func flatten(c int) []int {
    var r []int
    for _, v := range cube {
        r = append(r, (*v)[c])
    }

    return r
}

// move moves the cube (c)olumn to (p)osition
func move(c int, p []int)  {
    for i := range cube {
        (*cube[i])[c] = p[i]
    }
}

免责声明

该脚本依赖于面为 3x3 正方形的 cube 变量的存在。没有进行索引检查,警告 out ofbounds 恐慌。

https://play.golang.com/p/Z7azOi0omP3

本篇关于《对 3 维数组进行旋转》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>