参考:https://blog.csdn.net/yhmhappy2006/article/details/2934435
function main()
local t = {} --可视化螺旋阵列
local t1={} --转换成螺旋坐标
local y1=0
for y = -5,5 do
y1=y1+1
t[y1]={}
local x1=0
for x = -5,5 do
x1=x1+1
t[y1][x1]=spiral(x, y)
t1[spiral(x, y)]={x1,y1}
end
end
print(t) --必须使用支持打印表的函数
print(t1)
end
function spiral(x,y)
local c = max(math.abs(x), math.abs(y)) --当前坐标所在圈
local max1 = (c * 2 + 1) * (c * 2 + 1) --当前圈上最大值
if y == -c then --上边
return max1 + (x + y)
elseif (x == -c) then --左边
return max1 + (3 * x - y)
elseif (y == c) then --下
return max1 + (-x - 5 * y)
else --右边
return max1 + (-7 * x + y)
end
end
function max(n1,n2)
if n1 > n2 then
return n1
else
return n2
end
end
main()
--[[{
{111,112,113,114,115,116,117,118,119,120,121,},
{110, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,},
{109, 72, 43, 44, 45, 46, 47, 48, 49, 50, 83,},
{108, 71, 42, 21, 22, 23, 24, 25, 26, 51, 84,},
{107, 70, 41, 20, 7, 8, 9, 10, 27, 52, 85,},
{106, 69, 40, 19, 6, 1, 2, 11, 28, 53, 86,},
{105, 68, 39, 18, 5, 4, 3, 12, 29, 54, 87,},
{104, 67, 38, 17, 16, 15, 14, 13, 30, 55, 88,},
{103, 66, 37, 36, 35, 34, 33, 32, 31, 56, 89,},
{102, 65, 64, 63, 62, 61, 60, 59, 58, 57, 90,},
{101,100, 99, 98, 97, 96, 95, 94, 93, 92, 91,},}]]