Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Calculating distance between 2 2D points

Author
Message
FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 3rd Nov 2003 15:09
OK, here's the problem. I have a grid of 18x18 squares of equal size. I am trying to write a function that takes the coordinates of the current square (xCurrent, yCurrent) and the coordinates of the target square (xTarget, yTarget) and calculates the distance (in squares) between these two points.

Why am I doing this? It's in order to calculate if the distance exceeds the playes allowed maximum movement. The player can move in any of eight directions (up, down, left, right and the four diagonals).

So far, I am able to calculate the distance between the two points if they are on the same horizontal or vertical plane (as the distance will either be xTarget-xCurrent or yTarget-yCurrent respectively). I have written code that calculates the distance between any two points but the problem is that it assumes the player is moving in an 'L-shape' to the target square (therefore the distance would be the sum of the difference between xTarget and xCurrent and yTarget and yCurrent). This is all well and good but it has severe faults as it always calculates the distance as greater than is possible if one were to use a combination of diagonal, horizontal and vertical lines.

Here is the function as it stands now:



If anyone could help me out I would be very grateful,

Thanks,

Beavis
23
Years of Service
User Offline
Joined: 15th May 2003
Location: Czech Republic
Posted: 3rd Nov 2003 15:42
Here is solution :
if you need total distance in integer numbers then remove # in totaldistance#



--------
Beavis
http://www.progress-studio.com
FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 3rd Nov 2003 16:30
Thanks Beavis,

You wouldn't believe I have two A-levels in Maths would you?

spooky
23
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 3rd Nov 2003 17:14
I am not sure that is what FieldDoc wants. That simply gives you distance 'as the crow flys'. i.e. the length of the hypotanese of a right angle triangle. You are after how many moves it takes a player to move from one square to a target square moving like a 'king' in chess. Sure there is an easy solution somewhere...

Boo!
Beavis
23
Years of Service
User Offline
Joined: 15th May 2003
Location: Czech Republic
Posted: 3rd Nov 2003 17:25
If you set that player have for example 8 moves, then if you use this function you can move in squares in circle around player. I think that its right function.

I recommend use pathfinding function, because if you have blocked parts in map, then this function is useless.

--------
Beavis
http://www.progress-studio.com
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Nov 2003 19:10
@Spooky, you are talking about the 'manhattan' distance, which is simply the x distance added to the y distance.

distance = abs(XTarget - XSource) + abs(YTarget - YSource)
spooky
23
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 3rd Nov 2003 19:24
No, never heard that phrase but a good name for it though!

manhattan: (1st example=7 moves, 2nd=5 moves)



Fieldoc: (both examples take 4 moves)



So I think FieldDoc wants 'how many moves to get from X to Y moving one square at a time moving in any of the 8 directions, left, right, up, down or the 4 diagnals)'

@IanM - rather like your pathfinding snippet but a lot easier as no obstructions to worry about.

Boo!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Nov 2003 19:43
If you use the diagonals, then you just take the higher distance

FieldDoc
23
Years of Service
User Offline
Joined: 30th May 2003
Location: London, UK
Posted: 3rd Nov 2003 20:35
@Spooky:
That's what i'm after mate exactly. I have just noticed that Beavis' solution seems to give erroneous answers at times (sometimes 1 or 2 out) but i haven't worked out exactly when it is erroneous.

I am not dealing with obstacles in the way at the moment (although I will need to fairly soon )

spooky
23
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 3rd Nov 2003 22:32
Just got back to post my answer and found IanM already posted correct answer! Although he forgot to put abs around first two lines.



Boo!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Nov 2003 22:39
Doh!

Login to post a reply

Server time is: 2026-07-26 16:04:13
Your offset time is: 2026-07-26 16:04:13