从网格中的一个字段到另一个字段的正交步数?
如何计算网格中从一个字段到另一个字段正交移动的“步数”?
我正在为我正在开发的游戏实现一个 A* 寻路系统,但这个简单的数学运算阻碍了我。
我可能应该重新上三年级。哈哈。
How do I calculate the amount of "steps" there is from one field to another in a grid, moving orthogonally?
I am implementing an A* pathfinding system for a game that I am developing, and this simple mathematical operation is in my way.
I should probably re-attend third grade. Haha.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,我认为你只需将必要的 x,y 运动相加即可。给定两个点
(x1,y1)
和(x2,y2)
,那么距离(假设“正交移动”意味着仅水平和/或垂直移动)则为:例如,从位置(1,1)移动到(3,4)意味着向右移动2个空格,向上移动3个空格,总共5个。
abs(1-3)+abs(1-4 ) = 2 + 3 = 5
If I understand correctly, I think you just add up the x,y movements necessary. Given two points
(x1,y1)
and(x2,y2)
, then the distance (assuming "moving orthogonally" means moving only horizontally and/or vertically) then it is:For example, moving from position (1,1) to (3,4) means moving 2 spaces to the right and 3 spaces up for a total of 5.
abs(1-3)+abs(1-4) = 2 + 3 = 5
我确实相信这是一个简单的数学问题。
当然,您知道起始 x / y 值和结束 x / y 值。要获得两者之间的距离,请执行以下操作:
dist = sqrt(dx^2 + dy^2 )
其中 dx 是点的 x 坐标之间的差
其中 dy 是点的 y 坐标之间的差。
所以,举例来说。假设坐标 A 是 A(15,20),坐标 B 是 B(35,5);
dx = 35 - 15 = 20;
dy = 20-5 = 15;
所以;
AB 之间的距离 = sqrt(20^2 + 15^2)
= 25.0 单位。
现在,对于您的最终答案,这取决于您的程序中的“步骤”有多少个单元。如果步长为 5 个单位 (25/5),则从 A 点到 B 需要 5 步。
I do believe this is a matter of simple math.
Surely, you know your starting x / y values, and your ending x / y values. To get the distance between the two, you do this:
dist = sqrt(dx^2 + dy^2 )
Where dx is the difference between the x-coordinates of the points
Where dy is the difference between y-coordinates of the points.
So, for example. Lets say co-ordinate A is A(15,20) and co-ordinate B is B(35,5);
dx = 35 - 15 = 20;
dy = 20-5 = 15;
Therefore;
dist between AB = sqrt(20^2 + 15^2)
= 25.0 units.
Now for your final answer, this depends how many units a "step" is in your program. If a step is 5 units, (25/5) than there is 5 steps needed to get from point A to B.