python中蜂窝网格边缘的坐标 [英] Coordinates of the edges of a honeycomb grid in python

查看:18
本文介绍了python中蜂窝网格边缘的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was sitting the whole day, trying to solve this problem. First, I will show you, what kind of pattern I am trying to get:

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

As you can see, I am tryig to get the coordinates of the corners of all the hexagons in the whole rectangle, respectively the red circles (I guess, you can see, where I tried going without me marking all of them). The distance from grey line to grey line equals one and a hexagon as a diameter of 3. I already tried with the following code to get at least the pattern of the red circles without limiting them to the rectangle:

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)

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

But how do I change my code in order to get all coordinates in the rectangle? Preferably as a list like

[(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)

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

But I did got no output. I hope my question is understandable. I am quite desperate.

诚挚的问候:)

推荐答案

别人给我找到了一个更优雅的方法:

Someone else found a way for me to do it more elegantly:

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))

这篇关于python中蜂窝网格边缘的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆