gpt4 book ai didi

algorithm - 从位置获取螺旋索引

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:41 27 4
gpt4 key购买 nike

我正在使用 Alberto Santini 对此问题的解决方案来获取基于项目索引的螺旋网格引用

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

这不是公认的解决方案,但它是满足我需求的最佳解决方案,因为它避免了使用循环。

它运行良好,但我现在想要的是逆向操作。基于已知的 x 和 y 坐标返回位置的索引。

这是退还给定位置周围元素的前奏。

最佳答案

帕斯卡代码:

if y * y >= x * x then begin
p := 4 * y * y - y - x;
if y < x then
p := p - 2 * (y - x)
end
else begin
p := 4 * x * x - y - x;
if y < x then
p := p + 2 *(y - x)
end;

描述:左上半对角线 (0-4-16-36-64) 包含平方层数 (4 * layer^2)。外部 if 语句定义层并在左上半平面的相应行或列中找到(预)位置的结果,内部 if 语句校正镜像位置的结果。

关于algorithm - 从位置获取螺旋索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41876613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com