tags:

views:

124

answers:

2

Hi..I am trying to see if a given pair of (lat,long) will fall within a circular region. The centre of the circle is known (lat,long values) as well as the radius. The approcah I have used is:

  • Calculate the distance between the centre and the given lat/long using Haversines formula.

Formula:

a = ( sin(delta_lat/2) )^2 + cos (vp_Current.v_Latitude) *
    cos(vp_CentreOfCircle.v_Latitude) *  ( sin(delta_long/2) )^2;
c = 2 * atan2( sqrt(a), sqrt(1-a) );
d = R * c;

where: R = 6371 Km., delta_lat = lat2 - lat1, delta_long = long2 - long1

  • Then check if this distance is less than the radius to see if it is within the circle.

I have written the code in C but when I enter the following data the output says that the point is outside rather than within the circle (the point is within as I checked on google maps).

Centre(lat/long) = (19.228177, 72.685547)
Given point = (18.959999, 72.819999) 
Radius = 30 miles (about 49 Km but entered as 50 in the program).

The weird thing is if I enter the radius as 5000, the output says inside but not even for 500. I don't know where the problem is..would be really grateful if anyone could share some pointers...thanks.

A: 

To start, I'd check to see what your distance between the 2 points (center & given) ends up being. Then I'd make sure you're using all the correct units (meters v. miles, radians v. degrees).

If you don't find a mistake there, I'd start looking at your Haversine implementation vs. one of the posted ones on wikipedia.

Hope that helps.

Bryan
Hi..thanks for the replies...I spotted the problem about 2 minutes after posting this...w.r.t LarsH's reply..yes, the formula is correct. This is the same as asin(sqrt(a)) as it is given in various refernces...w.r.t Bryan's reply..Bingo...hit the nail right on the head. I didn't convert properly at one place. I spotted this right after I posted this..and that after getting frustrated after a few hours...anyway...thanks a lot for the replies..am checking and rechecking again..will post if any other glitch comes to light..cheers!!!
Sure, glad you found it. To stop more people from going over it, you should accept the answer though.
Bryan
+2  A: 

(I am nowhere an expert in all this, but it was fun to look around for education)

Since the argument for the Harvesin function is in radians, should the latitude and longitude be converted from degrees to radians ?

Matthieu
Yep, sounds like Matthieu has found the problem.
LarsH
yes..the lat and log need to be converted to radians...cheers!!!
Then you should accept the answer.
R..