그리드의 한 필드에서 다른 필드에서 다른 필드에서 다른 필드의 단계는 직각으로?

StackOverflow https://stackoverflow.com/questions/5490812

문제

어떻게 "단계"의 양을 어떻게 계산합니까? 그 필드에서 다른 필드가 다른 필드에서 다른 필드에서 직각으로 움직입니까?

나는 발전하고있는 게임을위한 A * Pathfinding 시스템을 구현하고,이 간단한 수학적 조작은 내 방식대로있다.

나는 아마도 3 학년을 재배치해야합니다.하하.

도움이 되었습니까?

해결책

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:

abs(x1-x2) + abs(y1-y2)

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top