• 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

    • Maybe the site will get out of dabbing into Identity Politics.
    • ASUS TUF Gaming A16 (2024): A capable gaming laptop with AI for $300 less by Paul Hill If you are in the market for a mid-range gaming laptop packed with AI features, check out the ASUS TUF Gaming A16 2024 laptop, which is now discounted by 19% to just $1,299. The list price of this laptop is $1,599.99, so you’re saving $300 by taking advantage of this deal. Inside are the AMD Ryzen AI 9 HX 370 processor, which supports Windows AI features such as Recall, and the Nvidia GeForce RTX 4060 GPU, which offers solid gaming performance at 1080p. What you get: Features and performance for gamers The ASUS TUF A16 includes a 16-inch display with a 16:10 aspect ratio. Its refresh rate is 165Hz to minimize lag, it supports Nvidia G-Sync, and it has a 2.5K resolution. Inside the device, we have the AMD Ryzen AI 9 HX 370 processor, Nvidia GeForce RTX 4060 GPU, 16GB of LPDDR5X RAM, and a 1TB PCIe Gen 4.0 SSD. A nice thing ASUS has taken into consideration with this laptop is durability. It is MIL-STD-810H certified, meaning it has passed durability tests. While the laptop is discounted, it’s not cheap, so knowing it’s durable is a plus. In terms of cooling, it features Arc Flow Fans, four exhaust vents, five head pipes, and an anti-dust filter. Combined, these will keep the laptop running cool, providing better gaming performance. As for sound, the A16 features Dolby Atmos, Hi-Res Audio, and two-way AI noise cancellation. All of this will make your gaming sessions more immersive, and the noise cancellation will ensure you can hear your teammate properly. The laptop runs Windows 11 and includes 3 months of PC Game Pass, so you can play games right away, and save some money given the rising cost of games. Who should buy this laptop? Buyers can expect solid performance from this laptop for its price point. With the fast SSD, AI-capable CPU, and decent graphics card, you can expect to get most school or work tasks done easily, and it should capably handle games up to 1080p. The cooling features are nice because they will prevent any performance degradation, and the MIL-STD-810H rating offers peace of mind related to durability. If you are seeking the cutting edge of gaming performance, this laptop may not be for you. Additionally, if you aren’t much of a gamer or a creator, this laptop could be overkill, and you’d get away with buying something cheaper. If you want to pick it up, check out the buying link below. ASUS TUF Gaming A16: $1,299 (Amazon US) / MSRP $1,599.99 This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • After Elon has been high as a kite IN the oval office on video, I don't think anyone can mention Hunter and it mean anything anymore. As far as I'm concerned, Elon wiped the Hunter laptop issue away. And Marge Traitor Green just used that laptop to show congress Hunter naked so that she could look at him more! That was horrible.
    • AI vibrators do the same thing now..... err wait....
  • Recent Achievements

    • Reacting Well
      Cole Multipass earned a badge
      Reacting Well
    • Reacting Well
      JLP earned a badge
      Reacting Well
    • Week One Done
      Rhydderch earned a badge
      Week One Done
    • Experienced
      dismuter went up a rank
      Experienced
    • One Month Later
      mevinyavin earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      695
    2. 2
      ATLien_0
      275
    3. 3
      Michael Scrip
      218
    4. 4
      +FloatingFatMan
      188
    5. 5
      Steven P.
      146
  • Tell a friend

    Love Neowin? Tell a friend!