Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This should work for any points on earth. If you want to change it to a different size sphere just change the kEarchRadiusKms to whatever radius you want for your sphere.</p> <p>This method is used to calculate the distance between to lat and lon points.</p> <p>I got this distance formula from here: <a href="http://www.codeproject.com/csharp/distancebetweenlocations.asp" rel="nofollow noreferrer">http://www.codeproject.com/csharp/distancebetweenlocations.asp</a></p> <pre><code>public static double Calc(double Lat1, double Long1, double Lat2, double Long2) { double dDistance = Double.MinValue; double dLat1InRad = Lat1 * (Math.PI / 180.0); double dLong1InRad = Long1 * (Math.PI / 180.0); double dLat2InRad = Lat2 * (Math.PI / 180.0); double dLong2InRad = Long2 * (Math.PI / 180.0); double dLongitude = dLong2InRad - dLong1InRad; double dLatitude = dLat2InRad - dLat1InRad; // Intermediate result a. double a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) + Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) * Math.Pow(Math.Sin(dLongitude / 2.0), 2.0); // Intermediate result c (great circle distance in Radians). double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a)); // Distance. // const Double kEarthRadiusMiles = 3956.0; const Double kEarthRadiusKms = 6376.5; dDistance = kEarthRadiusKms * c; return dDistance; } </code></pre> <p>If the distance between any vertex of the rectangle is less than the distance of the radius of the circle then the circle and rectangle overlap. If the distance between the center of the circle and all of the vertices is greater than the radius of the circle and all of those distances are shorter than the width and height of the rectangle then the circle should be inside of the rectangle.</p> <p>Feel free to correct my code if you can find a problem with it as I'm sure there some condition that I have not thought of.</p> <p>Also I'm not sure if this works for a rectangle that spans the ends of the hemispheres as the distance equation might break down.</p> <pre><code>public string Test(double cLat, double cLon, double cRadius, double rlat1, double rlon1, double rlat2, double rlon2, double rlat3, double rlon3, double rlat4, double rlon4) { double d1 = Calc(cLat, cLon, rlat1, rlon1); double d2 = Calc(cLat, cLon, rlat2, rlon2); double d3 = Calc(cLat, cLon, rlat3, rlon3); double d4 = Calc(cLat, cLon, rlat4, rlon4); if (d1 &lt;= cRadius || d2 &lt;= cRadius || d3 &lt;= cRadius || d4 &lt;= cRadius) { return "Circle and Rectangle intersect..."; } double width = Calc(rlat1, rlon1, rlat2, rlon2); double height = Calc(rlat1, rlon1, rlat4, rlon4); if (d1 &gt;= cRadius &amp;&amp; d2 &gt;= cRadius &amp;&amp; d3 &gt;= cRadius &amp;&amp; d4 &gt;= cRadius &amp;&amp; width &gt;= d1 &amp;&amp; width &gt;= d2 &amp;&amp; width &gt;= d3 &amp;&amp; width &gt;= d4 &amp;&amp; height &gt;= d1 &amp;&amp; height &gt;= d2 &amp;&amp; height &gt;= d3 &amp;&amp; height &gt;= d4) { return "Circle is Inside of Rectangle!"; } return "NO!"; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload