所以就像标题说的那样,我需要帮助尝试将点从二维平面映射到数轴,这样每个点都与一个唯一的正整数相关联。换句话说,我需要一个函数 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
我很确定这可行,但正如您所见,它是一个相当庞大的功能。我正在想办法简化这种“螺旋计数”逻辑。或者,如果有另一种更易于编码的计数方法也可以使用。