0

所以就像标题说的那样,我需要帮助尝试将点从二维平面映射到数轴,这样每个点都与一个唯一的正整数相关联。换句话说,我需要一个函数 f:ZxZ->Z+ 并且我需要 f 是单射的。此外,我需要在合理的时间内运行。

所以我认为这样做的方式基本上只是计算点,从 (1,1) 开始并向外螺旋。

下面我写了一些 python 代码来做这件事(i,j)

def plot_to_int(i,j):

a=max(i,j) #we want to find which "square" we are in
b=(a-1)^2 #we can start the count from the last square
J=abs(j)
I=abs(i)
if i>0 and j>0: #the first quadrant 
    #we start counting anticlockwise
    if I>J: 
        b+=J
    #we start from the edge and count up along j
    else:
        b+=J+(J-i)
    #when we turn the corner, we add to the count, increasing as i decreases
elif i<0 and j>0: #the second quadrant
    b+=2a-1 #the total count from the first quadrant
    if J>I:
        b+=I
    else:
        b+=I+(I-J)
elif i<0 and j<0: #the third quadrant
    b+=(2a-1)2 #the count from the first two quadrants
    if I>J:
        b+=J
    else:
        b+=J+(J-I)
else:
    b+=(2a-1)3
    if J>I:
        b+=I
    else:
        b+=I+(I-J)

return b

我很确定这可行,但正如您所见,它是一个相当庞大的功能。我正在想办法简化这种“螺旋计数”逻辑。或者,如果有另一种更易于编码的计数方法也可以使用。

4

1 回答 1

0

这是一个半生不熟的想法:

  1. 对于每个点,计算f = x + (y-y_min)/(y_max-y_min)
  2. d找到任何给定f_n和之间的最小增量f_{n+1}。将所有f值乘以,1/d使所有f值至少相隔 1。
  3. floor()所有f值中的一个。

这有点像在 x 轴上的投影,但它试图分散值以保持唯一性。

更新:

如果您不知道所有数据并且将来需要输入新数据,也许有一种方法可以在步骤 1 中硬编码任意大小的常量,y_max并根据边界为步骤 2硬编码y_min任意增量d您期望的数据值。或者一种根据浮点运算的限制计算这些值的方法。

于 2021-02-22T03:54:27.970 回答