• 0

[Math][C#] Check if coord is inside Bounding Radius.


Question

I'm doing some stuff with GPS. I want to build a bounding radius around my GPS cord. (So a 20 meter diameter around the GPS Coord.

I can do this, and store each point of the 360 degree line of the bounding radius, but i want to check if a coord is inside this radius, and i can't work out how todo this.

13 answers to this question

Recommended Posts

  • 0

C = point that is the center of the area

P = point we want to check for being inside the area

R = radius of the area

P is inside the area if

||P-C|| <= R

In layman's terms, trace a vector from the point to the center by subtracting the two points. If the length of that vector is equal or inferior to the radius of the area, then the point is inside the circle.

  • 0

I had to do a similar thing in my own code a while ago. I'm guessing the trouble you might be having is working out distances in spherical coordinates? The examples posted above are for standard x/y coordinate systems. I tended to use the haversine formula found here: http://en.wikipedia.org/wiki/Great-circle_distance (second large-ish equation down). That gives you the angular distance between the two points. Multiply that by the radius of the Earth and you have the "physical" distance in meters. Hope that helps, assuming I get what the problem you are having is.

  • 0
  On 17/09/2011 at 22:09, Dr_Asik said:

C = point that is the center of the area

P = point we want to check for being inside the area

R = radius of the area

P is inside the area if

||P-C|| <= R

In layman's terms, trace a vector from the point to the center by subtracting the two points. If the length of that vector is equal or inferior to the radius of the area, then the point is inside the circle.

I'm using lat/long cords, how would I do that, using that formula?

  • 0
  On 17/09/2011 at 23:28, Xerax said:

I'm using lat/long cords, how would I do that, using that formula?

That's fairly complicated because geographical coordinates don't give you a (x, y) point on a flat surface. Since the earth is spherical, the equations are much more involved.

See http://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates

and http://www.math.montana.edu/frankw/ccp/cases/Global-Positioning/spherical-coordinates/learn.htm

  • 0
  On 17/09/2011 at 23:28, Xerax said:

I'm using lat/long cords, how would I do that, using that formula?

First, from lat/long (degrees) to UTM (meters) use the Redfearn formula, and then you can use the Dr Asik method solid knight method (easy and short).

For the distance, if you need precision, you need to calculate the geodesic distance with the Vincenty formula.

  • 0
  On 18/09/2011 at 05:37, tuto said:

First, from lat/long (degrees) to UTM (meters) use the Redfearn formula, and then you can use the Dr Asik method.

The first method I gave works for a flat surface and is a good approximation for small distances. In the general case though, you don't want to simply draw a simple vector between the two points, because the earth being spherical, that vector would actually through the earth rather than around its surface. So to code for the general case you have to go through some additional steps as I linked to in my last post.
  • 0

I can't edit so (a more simple idea without meters conversions):

1) with the lat/long coords of the point and the lat/long coord of the center of circle, calculate the geodesic distance with the Vincenty formula.

2) if the geodesic distance < radius, then the point is inside the circle (Solid Knight).

Here are a geodesic distance calculator to test:

http://www.ga.gov.au/geodesy/datums/vincenty_inverse.jsp

  • 0
  On 18/09/2011 at 07:38, GreenMartian said:

He did mention a 20m radius. In the grand scheme of things, i guess it could be considered 'flat enough' :p

You would be assuming rectangular coordinates, which would be very skewed at the poles.

  • 0
  On 18/09/2011 at 07:58, rfirth said:

You would be assuming rectangular coordinates, which would be very skewed at the poles.

If I needed a GPS app to be used at the poles, I wouldn't grab one from some random source, where the authors could be getting advice from some random forum on the net. :laugh:

I kid! @OP: Try to not just plug numbers into formulas, learn the how & why they work the way they do. The posters here have been kind enough to point you to the right direction (pun intended).

  • 0

Thanks for all your feedback, I did as GreenMartin said and looked into how Dr_Asik's original forumula worked, and i coded this;

// Get distance between center of 
                        double dist = Classes.Maths.gpsCordDistance(lapStartBoundingBox[0], lapStartBoundingBox[1], geoPos.Coordinate.Latitude, geoPos.Coordinate.Longitude);

                        // If the distance between point-&gt;center of bounding radius is less than the radius, we are in for the #winning
                        if (dist &lt;= lapStartBoundingBox[2])
                        {

                        }

/// Code for CordDistance
        public static double gpsCordDistance(double pointLat, double pointLon, double locLat, double locLon)
        {
            double R = 6371; // Earth Radius
            double dLat = degreesToRadians(pointLat - locLat);
            double dLon = degreesToRadians(pointLon - locLon);
            double lat1 = degreesToRadians(locLat);
            double lat2 = degreesToRadians(pointLat);

            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                    Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double d = R * c;

            return d;
        }

Returns distance in Kilometres. :) Thanks for all your help.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.