Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.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
Python生成所有方向组合,包括三维对角线_Python - Fatal编程技术网

Python生成所有方向组合,包括三维对角线

Python生成所有方向组合,包括三维对角线,python,Python,我想从三维网格中的一个点生成所有方向,但我不能完全理解下一点。对于记录,它都存储在一个列表中,所以我需要一些数学来计算下一个点的位置 我只需要3次计算就可以计算出26个左右不同的方向中的任何一个(向上、向上向左、向上向左向前、向上向左向后、向上向右、向上向右向前等等),所以我决定使用X、Y、Z,然后将它们分为向上/向下向左/向右等,然后得到正确的数字进行加法或减法。然而,生成这个列表来进行数学运算似乎有点困难 direction_combinations = 'X Y Z XY XZ YZ XY

我想从三维网格中的一个点生成所有方向,但我不能完全理解下一点。对于记录,它都存储在一个列表中,所以我需要一些数学来计算下一个点的位置

我只需要3次计算就可以计算出26个左右不同的方向中的任何一个(向上、向上向左、向上向左向前、向上向左向后、向上向右、向上向右向前等等),所以我决定使用X、Y、Z,然后将它们分为向上/向下向左/向右等,然后得到正确的数字进行加法或减法。然而,生成这个列表来进行数学运算似乎有点困难

direction_combinations = 'X Y Z XY XZ YZ XYZ'.split()
direction_group = {}
direction_group['X'] = 'LR'
direction_group['Y'] = 'UD'
direction_group['Z'] = 'FB'
因此,基本上,使用下面的代码,这是我希望它做的事情,但显然没有硬编码。我可以用一种简陋的方式来做,但我想这里缺少一些非常简单的东西

#Earlier part of the code to get this bit working
#I've also calculated the edges but it's not needed until after I've got this bit working
grid_size = 4
direction_maths = {}
direction_maths['U'] = pow(grid_size, 2)
direction_maths['R'] = 1
direction_maths['F'] = grid_size
direction_maths['D'] = -direction_maths['U']
direction_maths['L'] = -direction_maths['R']
direction_maths['B'] = -direction_maths['F']



#Bit to get working
starting_point = 25
current_direction = 'Y'

possible_directions = [direction_group[i] for i in list(current_direction)]
for y in list(possible_directions[0]):
    print starting_point + direction_maths[y]
# 41 and 9 are adjacent on the Y axis


current_direction = 'XYZ'

possible_directions = [direction_group[i] for i in list(current_direction)]
for x in list(possible_directions[0]):
    for y in list(possible_directions[1]):
        for z in list(possible_directions[2]):
            print starting_point + direction_maths[x] + direction_maths[y] + direction_maths[z]
# 44, 36, 12, 4, 46, 38, 14 and 6 are all adjacent on the corner diagonals
下面是关于使用列表索引时网格外观的一般概念(以4x4x4为例):

编辑:使用与我最初发布的答案混合的答案(如果可能的话,希望避免转换为3D点或从3D点转换),这就是我计算完整行数的结果:)


不要把不必要的想法复杂化。不要用1个数字描述3维中的点-3坐标表示3个数字

应该是这样的:

numb = 37
cube_size = 4

# convert to x - y - z
start = [0, 0, 0]

start[2] = numb / cube_size ** 2
numb = numb % cube_size ** 2
start[1] = numb / cube_size
start[0] = numb % cube_size


for x in [-1, 0, 1]:
    current_x = start[0] + x
    for y in [-1, 0, 1]:
        current_y = start[1] + y
        for z in [-1, 0, 1]:
            current_z = start[2] + z

            #reconvert
            convert = current_x + current_y * cube_size + current_z * cube_size ** 2
            print("x: " + str(current_x) + " y: " + str(current_y) + " z: " + str(current_z) + " => " + str(convert))

只需生成您的x/y/z坐标,然后运行所有可能的操作,将-1/0/1添加到这些坐标,并重新转换为网格中的数字。

不要使计算变得复杂。不要用1个数字描述3维中的点-3坐标表示3个数字

应该是这样的:

numb = 37
cube_size = 4

# convert to x - y - z
start = [0, 0, 0]

start[2] = numb / cube_size ** 2
numb = numb % cube_size ** 2
start[1] = numb / cube_size
start[0] = numb % cube_size


for x in [-1, 0, 1]:
    current_x = start[0] + x
    for y in [-1, 0, 1]:
        current_y = start[1] + y
        for z in [-1, 0, 1]:
            current_z = start[2] + z

            #reconvert
            convert = current_x + current_y * cube_size + current_z * cube_size ** 2
            print("x: " + str(current_x) + " y: " + str(current_y) + " z: " + str(current_z) + " => " + str(convert))

只需生成x/y/z坐标,然后运行所有可能的操作,将-1/0/1添加到这些坐标,并重新转换为网格中的数字。

这里的操作与@AnnoSielder所做的基本相同,但使用itertools来减少代码量

from itertools import product

# Get a list of all 26 possible ways to move from a given coordinate in a 3 coordinate system.
base_deltas = filter(lambda point: not all(axis ==0 for axis in point), list(product([-1, 0, 1], repeat=3)))
# Define your max axis length or your grid size
grid_size = 4

# Simple function that applys the deltas to the given coordinate and returns you the list.
def apply_deltas(deltas, coordinate):
    return [
        (coordinate[0]+x, coordinate[1]+y, coordinate[2]+z)
        for x, y, z in deltas
    ]

# This will determine whether the point is out of bounds for the given grid
is_out_of_bounds = lambda point: all(0 <= axis < grid_size for axis in point)

# Define your point, in this case it's block #27 in your example
coordinate = [3, 2, 1]

# Apply the deltas, then filter using the is_out_of_bounds lambda
directions = filter(is_out_of_bounds, apply_deltas(base_deltas, coordinate))

# directions is now the list of 17 coordinates that you could move to.
来自itertools导入产品的

#从3坐标系中的给定坐标获取26种可能的移动方式的列表。
基本增量=过滤器(lambda点:并非全部(点中的轴==0),列表(产品([-1,0,1],重复=3)))
#定义最大轴长度或栅格大小
网格大小=4
#一个简单的函数,用于将增量应用于给定坐标并返回列表。
def应用增量(增量,坐标):
返回[
(坐标[0]+x,坐标[1]+y,坐标[2]+z)
对于三角洲中的x,y,z
]
#这将确定该点是否超出给定网格的边界

is_out_of_bounds=lambda point:all(0这里的内容基本上与@AnnoSielder相同,但是使用itertools来减少代码量

from itertools import product

# Get a list of all 26 possible ways to move from a given coordinate in a 3 coordinate system.
base_deltas = filter(lambda point: not all(axis ==0 for axis in point), list(product([-1, 0, 1], repeat=3)))
# Define your max axis length or your grid size
grid_size = 4

# Simple function that applys the deltas to the given coordinate and returns you the list.
def apply_deltas(deltas, coordinate):
    return [
        (coordinate[0]+x, coordinate[1]+y, coordinate[2]+z)
        for x, y, z in deltas
    ]

# This will determine whether the point is out of bounds for the given grid
is_out_of_bounds = lambda point: all(0 <= axis < grid_size for axis in point)

# Define your point, in this case it's block #27 in your example
coordinate = [3, 2, 1]

# Apply the deltas, then filter using the is_out_of_bounds lambda
directions = filter(is_out_of_bounds, apply_deltas(base_deltas, coordinate))

# directions is now the list of 17 coordinates that you could move to.
来自itertools导入产品的

#从3坐标系中的给定坐标获取26种可能的移动方式的列表。
基本增量=过滤器(lambda点:并非全部(点中的轴==0),列表(产品([-1,0,1],重复=3)))
#定义最大轴长度或栅格大小
网格大小=4
#一个简单的函数,用于将增量应用于给定坐标并返回列表。
def应用增量(增量,坐标):
返回[
(坐标[0]+x,坐标[1]+y,坐标[2]+z)
对于三角洲中的x,y,z
]
#这将确定该点是否超出给定网格的边界


is_out_of_bounds=lambda point:all(0)您的代码无法运行,很难理解您要执行的操作。“三维网格中某个点的所有方向。”首先要澄清的是——你是如何得到“27个左右不同的方向”的?用预览网格更新了问题,我的意思是如果你拿37个,不同方向的第一个点是16、17、18、20、21、22、24、25、26、32、33、34、36、38、40、41、42、48、49、50、52、53、54、56、57和58:我有数学题如果你把X,Y,Z当作独立的数字,它们可以是-1,0,或1。只需生成它们的每一个组合,并抛出平凡的[0,0,0]方向。你的代码无法运行,很难理解你在做什么。“从三维栅格中的一点开始的所有方向。"首先要澄清的是——你是如何得到“27个左右不同的方向”的?用预览网格更新了问题,我的意思是如果你拿37个,不同方向的第一个点是16、17、18、20、21、22、24、25、26、32、33、34、36、38、40、41、42、48、49、50、52、53、54、56、57和58:我有数学题要计算如何从37中得到每一个,而不是使用该数学的代码如果将X、Y和Z视为独立的数字,它们可以是-1、0或1。只需生成它们的每一个组合,并抛出平凡的[0,0,0]方向:
很简单,x轴有一个“in-in”。非常感谢你的想法,我有点把你的想法和我的想法融合在一起,并且在没有将其转换为3D的情况下工作(不想走捷径哈哈):
对于x轴[-1,0,1]:
很简单,x轴也有一个“in-in”。非常感谢你的想法,我把你的想法和我的想法融合在一起,在没有转换成3D的情况下就成功了(不想走捷径哈哈):)非常感谢,我真的不了解lambda,所以看到它在一些简单的东西上工作是很有用的,这种方式看起来非常干净:)lambdas可能很酷,但它们也会给你带来麻烦。如果你正在做一些复杂的/时髦的事情,把它变成一个函数,然后用它来代替。这是使用lambda的好方法。感谢您展示了使用Python所需的行数是多么少:)。非常感谢,我并不真正了解lambda,所以看到它在一些简单的事情上工作是很有用的,这种方式看起来非常干净:)lambda很酷,但它们也会给您带来麻烦。如果你正在做一些复杂的/时髦的事情,就把它变成一个函数,然后使用这个指令