Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating distance between 2 points on a circular map

Tags:

algorithm

2d

I'm currently trying to create a video game where characters are AI and are in a circular map with ressources on it.

I'm currently trying to calculate the shortest distance betwing 2 points on this map but my problem is that the map is circular : for example

if my map is 20*20 and i m in (0,0) the (19,0) point has a distance on 1 only. I've been looking on the internet but i didn't found answers for my problem. I have to take care of the orientation (North South west East) of my character too, in he has to turn to go to the point, the distance has to be longer.

Is there an existing formula ?

Thanks for reading !

like image 806
Loadex Avatar asked Jul 08 '12 09:07

Loadex


People also ask

How do you find the distance between two cylindrical coordinates?

Their distance then is d=√(x1−x2)2+(y1−y2)2+(z1−z2)2 .

What is the formula for distance between two points?

Distance between two points is the length of the line segment that connects the two points in a plane. The formula to find the distance between the two points is usually given by d=√((x2 – x1)² + (y2 – y1)²). This formula is used to find the distance between any two points on a coordinate plane or x-y plane.


1 Answers

For every coordinate choose the minimal distance, and then use it as usual.

int dx = min(abs(x0-x1),20-abs(x0-x1));
int dy = min(abs(y0-y1),20-abs(y0-y1));

For euclid distance :

double dist = sqrt(dx * dx + dy * dy);

For Manhattan distance:

int dist = dx + dy;
like image 138
RiaD Avatar answered Oct 21 '22 22:10

RiaD