• 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.
  • Posts

    • For anyone looking for a lightweight formatting-free text editor, I recommend Notepad3.
    • This looks really dumb, especially if it costs $100+. Noone who cares about using a flight yoke would touch that thing, people who don't care are probably fine using the analog sticks on their controller, so who is it for?
    • A) "they shouldn't be making money off of those [free videos]"?? That is literally their business model, making money off videos that users post...if you don't feel like that should be allowed, then are you saying YouTube shouldn't exist. B) Yes, the example I gave is a net-negative transaction. If YouTube makes money from others who are following their rules, it doesn't change the fact that the person using an ad-blocker is costing them money. C) YouTube has always operated at a loss...kind of invalidates your entire argument. As I always say, I don't care what you do, I will not even say you are wrong for doing it. That is purely your choice. Just be honest enough to say something like "Google is rich, I honestly don't care." Perfectly fine reason. Don't act like there is some imagined justification for why it isn't breaking the rules.
    • You can now present content from your camera feed in Google Meet by David Uzondu Google has a new feature rolling out for Google Meet that lets you directly present video from an external camera feed right into your meetings. This means if you have a document camera for showing physical papers, a dedicated external camera for a better angle, or even output from a video production tool, you can now pipe that into Meet as a presentation source. This new option supports video up to 1080p at 30FPS. This "present from camera" function offers a more integrated way to handle certain video inputs compared to some existing workarounds. For instance, it might prove less complicated than a setup with OBS Studio where you arrange your various video sources into scenes, activate the virtual camera output, and then navigate Google Meet's settings to specifically choose "OBS Virtual Camera" as your video input before you can even start presenting that customized feed. Alongside this camera presentation feature, Google's announcement also mentioned several improvements to the general screen sharing experience in Meet. Initiating any type of screen share is faster now, and video quality during screen sharing has also been sharpened, with better handling of dynamic content like scrolling text or embedded videos. To reduce interruptions, if a second presenter stops sharing their screen, any previous presentation will now automatically resume. For those wondering when they can get their hands on this, the rollout for the camera presentation feature and these screen sharing enhancements has begun for Rapid Release domains. Users on Scheduled Release domains will start seeing it from June 11, 2025. Google notes that it could take up to 15 days for these features to be visible to all eligible users. Most Google Workspace accounts, including Business Standard and Plus, various Enterprise and Education tiers, and Workspace Individual subscribers, will have access. This new presentation option joins other recent Google Workspace enhancements. For instance, Gemini in Google Drive can now summarize changes to your files, offering a quick way to get updated on what you missed in documents since you last opened them.
  • Recent Achievements

    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      382
    2. 2
      +FloatingFatMan
      177
    3. 3
      ATLien_0
      174
    4. 4
      snowy owl
      169
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!