1

I have a problem. How can I test in C++ if I have latitude and longitude of a point(a car with GPS) and I need to find if the point is inside the area of a circle. I have the center of the circle in latitude and longitude and the radius is in km.

For example:

I'm in the point :

52.51523649714909 13.18586926568004

And the circle center position is:

48.147763 11.563454 radius : 50km

I have this values from the project I need to resolve.

2 Answers2

1

Have a look at the Haversine Formula, here:
Calculate distance between two latitude-longitude points? (Haversine formula)

Community
  • 1
  • 1
novice
  • 555
  • 3
  • 16
  • The link is fine, but what precedes it is of no use to the OP. – dshin Jan 19 '16 at 00:34
  • Why? he still has to know if the point lies within the area right, plus what preceded will help him think about what needs to be done in general, formulas aside – novice Jan 19 '16 at 00:37
  • He needs the distance from the point to the circle center. Converting lat/log values to (x,y) pairs doesn't really help him compute that distance, especially if you are close to the poles. – dshin Jan 19 '16 at 00:39
  • well it is of some help if you are at the equator :) ,plus as i said i dont believe in blindly giving out formulas, some logic (trivial though it might be) goes a long way in helping people understand – novice Jan 19 '16 at 00:42
  • Yes, this can be a good approximation depending on where you are on the globe. On the other hand, it is a mistake to assume your users are like you, and applying the Euclidean-approximation-that-works-near-me to your user is essentially a form of that mistake. Promoting that mistake is not a good idea. Coming up with a Eucliean approximation that works well regardless of your global position is much more complex than directly using Haversine, so there's no good reason to do it. – dshin Jan 19 '16 at 00:49
  • ok, Let me edit my answer to address your concerns – novice Jan 19 '16 at 00:51
  • Remove the downvote? – novice Jan 19 '16 at 01:16
  • Again, I like the last sentence, but I don't understand the purpose of everything that comes before it. – dshin Jan 19 '16 at 01:27
  • ok. Just the last sentence it is. – novice Jan 19 '16 at 01:32
0

Take a look at the Great Circle Distance (https://en.wikipedia.org/wiki/Great-circle_distance) and the Haversine formula, which is one way to calculate this distance (https://en.wikipedia.org/wiki/Haversine_formula)

Once you have decided which formula you will use to calculate the distance between your two points, simply plug in your latitude and longitude and you will know your distance. If that is <= 50, then you are within the circle.

M

swinefish
  • 559
  • 6
  • 21