六边形网格两点间沿边遍历的算法求解
背景
需要在六边形网格上沿最近边追踪两点间的路径。
问题
确定可约束首次迭代后所有步骤沿边执行的算法。
代码
已知参数:
- vac -- 起始顶点的X坐标。
- var -- 起始顶点的Y坐标。
- vbc -- 终止顶点的X坐标。
- vbr -- 终止顶点的Y坐标。
- offset_ac -- 起始中心点的网格X偏移量。
- offset_ar -- 起始中心点的网格Y偏移量。
可计算得到:
- theta -- 起始点与终止点连线的角度(单位:度)。
- vangle -- 距离连线最近的顶点对应的角度。
- vc -- 相对于中心点偏移后的首个顶点X坐标。
- vr -- 相对于中心点偏移后的首个顶点Y坐标。
% Compute the direction towards the first segment (to vertex of an edge). theta := degrees( atantwo( vac, var, vbc, vbr ) ); vangle := round( theta / 60 ) * 60 * pi / 180; % Calculate the position of the first vertex, offset from the center. vc := offset_ac + cos( vangle ); vr := offset_ar + sin( vangle ); % Draw a line from the starting point to the ending point. draw (offset_ac, offset_ar) -- (vc, vr) withcolor colour_node; % Draw a circle at the ending coordinate. draw (vc, vr) withcolor colour_node withpen pencircle scaled vertex_sm;
输出
当前输出效果如下:
期望输出效果如下:
问题
采用何种算法可在起始点与终止点间沿图的边进行路径遍历?
找到首个顶点的操作较为简单。理论上,将起始点偏移至顶点后,可迭代执行现有代码,但偏移后新角度是否会因半宽、半高出现偏差?即便如此,如何确保后续迭代能达到第二张图所示的约束效果?
内容的提问来源于stack exchange,提问作者Dave Jarvis
相关产品推荐
相关产品推荐

