【问题标题】:Simple square grid lookup简单的方格查找
【发布时间】:2021-01-24 00:26:38
【问题描述】:

我在尝试在 python 中提出一个简单的方形网格查找时画了一个空白,希望有人可以在这里给我一些指示。假设我有一个 5x5 网格,从中心 (3,3) 开始,我希望查找增加半径输出外围坐标,如下所示:

 Radius     Output   
   0        [[3,3]]   
   1        [[2,2], [2,3], [2,4], [3,2], [3,4], [4,2], [4,3], [4,4]]   
   2        [[1,1], [1,2], [1,3], [1,4], [1,5], [2,1], [2,5], [3,1], 
             [3,5], [4,1], [4,5], [1,1], [1,2], [1,3], [1,4], [1,5]]

任何指针将不胜感激!

更新:我目前的代码是:

center_coordinate = [3, 3]
completed_locations = [center_coordinate]
radius = 0
max_radius = 3
while radius != max_radius:
    x_grid = [x for x in range(center_coordinate[0] - radius, center_coordinate[0] + radius + 1)]
    y_grid = [y for y in range(center_coordinate[0] - radius, center_coordinate[0] + radius + 1)]
    locations = []
    for x in x_grid:
        for y in y_grid:
            if [x, y] not in completed_locations:
                locations.append([x, y])
                completed_locations.append([x, y])
    radius += 1
    print(radius, locations)

虽然这确实可以完成工作,但我正在寻找一种解决方案,它不需要我在迭代时交叉检查每个位置。我要处理的实际网格大小是 750x750,这个特定的模块将定期被召唤。

【问题讨论】:

  • 你什么都想不出来?比如:1) 计算中心坐标 x0, y0 2) 获取坐标 x0+/-1, y0+/-1, 3) 获取所有单元格 x0+/-2, y0+/-2...?

标签: python-3.x


【解决方案1】:

感谢@think-maths 的指点。我已经修改了我的代码,并感谢对性能的显着提升。我已将这两个代码都包含在此处,以供任何做类似事情的人参考(例如遍历游戏的平铺地图;))。

def adapted_lookup(center_coordinate, max_radius=50):
    # ADAPTED FROM think-maths
    center_coordinate = [375, 375]
    locations_by_radius = []
    radius = 0
    while radius != max_radius:
        x_grid = [x for x in range(center_coordinate[0] - radius, center_coordinate[0] + radius + 1)]
        y_grid = [y for y in range(center_coordinate[0] - radius, center_coordinate[0] + radius + 1)]
        locations = []
        if len(x_grid) > 1:
            for x in x_grid:
                locations.append([x, y_grid[0]])
                if x == x_grid[0] or x == x_grid[-1]:
                    for y in range(y_grid[1], y_grid[-1]):
                        locations.append([x, y])
                locations.append([x, y_grid[-1]])
        else:
            locations.append([x_grid[0], y_grid[0]])
        locations_by_radius.append([radius, locations])
        radius += 1


def original_lookup(center_coordinate, max_radius=50):
    # ORIGINAL CODE
    locations_by_radius = []
    checked = []
    radius = 0
    while radius != max_radius:
        x_grid = [x for x in range(center_coordinate[0] - radius, center_coordinate[0] + radius + 1)]
        y_grid = [y for y in range(center_coordinate[0] - radius, center_coordinate[0] + radius + 1)]
        locations = []
        for x in x_grid:
            for y in y_grid:
                if [x, y] not in checked:
                    locations.append([x, y])
                    checked.append([x, y])
        locations_by_radius.append([radius, locations])
        radius += 1

在 750x750 网格中半径为 40 的 timeit 性能:

Original Lookup Time: 14.5097
Adapted Lookup Time: 0.0020

【讨论】:

    【解决方案2】:

    对于这种遍历网格形成圆圈的问题,Midpoint circle algorithm 是可能的

    例如,为了演示您可以根据需要使用类似的东西来实现您的要求

    def midPointCircleDraw(x_centre, y_centre, r): 
        x = r 
        y = 0
        
        print("(", x + x_centre, ", ",  
                   y + y_centre, ")",  
                   sep = "", end = "")  
        
        
        if (r > 0) : 
          
            print("(", x + x_centre, ", ", 
                      -y + y_centre, ")",  
                      sep = "", end = "")  
            print("(", y + x_centre, ", ",  
                       x + y_centre, ")", 
                       sep = "", end = "")  
            print("(", -y + x_centre, ", ",  
                        x + y_centre, ")", sep = "")  
          
         
        P = 1 - r  
      
        while x > y: 
          
            y += 1
              
           
            if P <= 0:  
                P = P + 2 * y + 1
                  
             
            else:          
                x -= 1
                P = P + 2 * y - 2 * x + 1
              
            if (x < y): 
                break
               
            print("(", x + x_centre, ", ", y + y_centre, 
                                ")", sep = "", end = "")  
            print("(", -x + x_centre, ", ", y + y_centre,  
                                 ")", sep = "", end = "")  
            print("(", x + x_centre, ", ", -y + y_centre, 
                                 ")", sep = "", end = "")  
            print("(", -x + x_centre, ", ", -y + y_centre, 
                                            ")", sep = "")  
              
             
            if x != y: 
              
                print("(", y + x_centre, ", ", x + y_centre,  
                                    ")", sep = "", end = "")  
                print("(", -y + x_centre, ", ", x + y_centre, 
                                     ")", sep = "", end = "")  
                print("(", y + x_centre, ", ", -x + y_centre, 
                                     ")", sep = "", end = "")  
                print("(", -y + x_centre, ", ", -x + y_centre,  
                                                ")", sep = "")
    

    【讨论】:

    • 这很有用!不完全是我正在寻找的东西,但给了我一个解决问题的想法。我将对其进行测试并返回结果:)
    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    相关资源
    最近更新 更多