1334250330_8408.jpg
    参考:https://blog.csdn.net/yhmhappy2006/article/details/2934435

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