ric*_*ick 5 language-agnostic algorithm
我正在使用Alberto Santini的解决方案来获得基于项目索引的螺旋网格参考
这不是公认的解决方案,但它最符合我的需求,因为它避免使用循环.
它运作良好,但我现在想要的是反过来.基于已知的x和y坐标返回位置的索引.
这是返回给定位置周围的项目的前兆.
Pascal代码:
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;
Run Code Online (Sandbox Code Playgroud)
描述:左上半对角线(0-4-16-36-64)包含平方层数(4*层^ 2).外部if语句定义层并在左上半平面的相应行或列中查找(预)结果,并且内部if语句更正镜像位置的结果.