【问题标题】:Coordinates of the edges of a honeycomb grid in pythonpython中蜂窝网格边缘的坐标
【发布时间】:2022-01-14 13:11:54
【问题描述】:

我整天坐着,试图解决这个问题。 首先,我会告诉你,我想得到什么样的模式:

如您所见,我正在尝试获取整个矩形中所有六边形的角的坐标,分别是红色圆圈(我猜,您可以看到,我尝试在没有标记所有它们的情况下去哪里) . 灰线到灰线的距离等于 1,六边形的直径为 3。 我已经尝试使用以下代码至少获得红色圆圈的图案,而不将它们限制为矩形:

x_start = 0
y_start = 0
width = 9
height = 9

#here I catch all the neighbours of one position I guess

def positions(x_start, y_start, width, height):
    positions_list = []
    
    for x in range(x_start, width + 1):
        for y in range(y_start, height + 1):
            positions_list +=[(x_start - 1, y_start),
                              (x_start - 1/2, y_start - 1),
                              (x_start + 1/2, y_start - 1),
                              (x_start + 1, y_start),
                              (x_start + 1/2, y_start + 1),
                              (x_start - 1/2, y_start + 1)]
            x_start += 1
            y_start += 1
    return positions_list
    print(positions_list)

positions(x_start, y_start, width, height)

但是如何更改代码以获取矩形中的所有坐标?最好像列表一样

[(0,0), (0.5, 1), (0, 1), ...]

或者,我尝试了这个:

import numpy

def positions(x_start, y_start, width, height):
    position_list = []
    
    for x in numpy.arange(x_start, width + 0.1, 0.5):
        for y1 in range(0, height+1, 2):
            position_list += [(x_start, y_start)                         ]
        for y2 in range(1, height+1, 2): 
            position_list += [(x_start, y_start)
    return positions_list
    print(positions_list)

positions(x_start, y_start, width, height)

但我没有得到任何输出。 我希望我的问题是可以理解的。我很绝望。

诚挚的问候:)

【问题讨论】:

  • 你不能简单地删除所有至少有一个负数的元组吗?就像这个列表理解 without_negative = [i for i in position(x_start, y_start, width, height) if i[0] >= 0 and i[1] >= 0]
  • @T1Berger 是的,我认为这是可能的,并且当对 i[0]
  • 只需对中间点执行相同的 for 循环并组合然后列表

标签: python grid coordinates android-3.0-honeycomb


【解决方案1】:

别人为我找到了一种更优雅的方法:

def positions(w,h):
    poss = []
    for y in range(0,h+1):
        if y%2 == 0:
            poss = [(x,y) for x in range(0,w+1)] + poss
        else:
            x = 0.5
            while x<=w:
                poss = [(x,y)] + poss
                x = x+1
    return poss


print(positions(5,5))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2017-10-14
    相关资源
    最近更新 更多