【问题标题】:Equidistant Points in a Matrix矩阵中的等距点
【发布时间】:2020-07-01 07:36:02
【问题描述】:

我有一个浮点值矩阵。相对于给定的原点(x 和 y 索引,点“0”),我想获得等距点的索引,从最近的点(“1”)开始,直到一个特定的数字(在这个动画示例):

距离是点与点 0 之间的斜距。例如,点“4”的距离为 sqrt(2^2+1^2) = sqrt(5) = 2.24。

有没有人知道相应的算法可以有效地获取这些索引?

【问题讨论】:

  • 请在你发现这个问题的地方添加一个链接并输出,你的程序应该产生。
  • 我对距离公式不太清楚。标为 6 的单元格,为什么标为 6 而不是标为 5?
  • 感谢 cmets。距离只是点之间的地理距离。这意味着点 1 到点 0 的距离为 1,点 2 到点 0 的距离为 sqrt(2)=1.41,依此类推。我会相应地更新问题。

标签: algorithm matrix


【解决方案1】:

换个说法,您想通过增加到中心的欧几里得距离来枚举点。

这里有两个关于https://math.stackexchange.com 的答案how-to-enumerate-2d-integer-coordinates-ordered-by-euclidean-distancealgorithm-for-enumerating-grid-points-by-distance-from-given-point

基本上:

  • 使用对称性仅考虑带有0 <= x <= y 的点;
  • 请注意,对于给定的x,点数将随着y 的增加而增加;
  • 使用优先级队列为每条垂直线保留下一个候选者。

使用您生成的最后一个索引n,时间复杂度将为O(n log n),空间复杂度为O(sqrt(n))

注意:为避免浮点计算,请考虑平方距离,这不会改变点的顺序。

这里有一些实现这个想法的python代码:

import heapq

def yield_all_quadrant(x, y): 
    s = set([(x, y), (-x, y), (x, -y), (-x, -y),
             (y, x), (-y, x), (y, -x), (-y, -x)]) 
    for u, v in sorted(s): 
        yield u, v

def indices(X, Y):
    q = [(0, 0, 0)]
    d_current = 0
    index = 0
    while True:
        d, x, y = heapq.heappop(q)
        if d > d_current:
            index += 1
            d_current = d
        for u, v in yield_all_quadrant(x, y):
            yield (X + u,Y + v), index
        if not y:
            heapq.heappush(q, (d + 2*x + 1, (x+1), 0))
        if y < x:
            heapq.heappush(q, (d + 2*y + 1, x, y+1))

例如用于填充网格的小函数

import itertools 
def fill_grid(size, center): 
    grid = [[0]*size for _ in range(size)] 
    def clip(e): 
        coord, index = e 
        return all(0 <= c < size for c in coord) 
    for (x,y), i in itertools.islice(filter(clip, indices(*center)), 0, size**2): 
        grid[x][y] = i 
    return grid 

结果

print('\n'.join(' '.join('%2d'%i for i in gi) for gi in fill_grid(20, (8,8))))

54 48 43 39 35 33 31 30 29 30 31 33 35 39 43 48 54 59 67 74
48 42 38 34 30 27 26 24 23 24 26 27 30 34 38 42 48 55 62 69
43 38 32 28 25 22 20 19 18 19 20 22 25 28 32 38 43 50 56 64
39 34 28 24 21 17 15 14 13 14 15 17 21 24 28 34 39 46 53 60
35 30 25 21 16 13 12 10  9 10 12 13 16 21 25 30 35 41 49 57
33 27 22 17 13 11  8  7  6  7  8 11 13 17 22 27 33 40 47 55
31 26 20 15 12  8  5  4  3  4  5  8 12 15 20 26 31 38 45 53
30 24 19 14 10  7  4  2  1  2  4  7 10 14 19 24 30 37 44 52
29 23 18 13  9  6  3  1  0  1  3  6  9 13 18 23 29 36 43 51
30 24 19 14 10  7  4  2  1  2  4  7 10 14 19 24 30 37 44 52
31 26 20 15 12  8  5  4  3  4  5  8 12 15 20 26 31 38 45 53
33 27 22 17 13 11  8  7  6  7  8 11 13 17 22 27 33 40 47 55
35 30 25 21 16 13 12 10  9 10 12 13 16 21 25 30 35 41 49 57
39 34 28 24 21 17 15 14 13 14 15 17 21 24 28 34 39 46 53 60
43 38 32 28 25 22 20 19 18 19 20 22 25 28 32 38 43 50 56 64
48 42 38 34 30 27 26 24 23 24 26 27 30 34 38 42 48 55 62 69
54 48 43 39 35 33 31 30 29 30 31 33 35 39 43 48 54 59 67 74
59 55 50 46 41 40 38 37 36 37 38 40 41 46 50 55 59 66 73 80
67 62 56 53 49 47 45 44 43 44 45 47 49 53 56 62 67 73 79 85
74 69 64 60 57 55 53 52 51 52 53 55 57 60 64 69 74 80 85 93

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多