Last active
September 18, 2015 17:15
-
-
Save actongorton/585036fcc515cf70018c to your computer and use it in GitHub Desktop.
Calculate direction, bearing, radians and distance between two location points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static double DegreeBearing( | |
double lat1, double lon1, | |
double lat2, double lon2) | |
{ | |
const double R = 6371; //earth’s radius (mean radius = 6,371km) | |
var dLon = ToRad(lon2-lon1); | |
var dPhi = Math.Log( | |
Math.Tan(ToRad(lat2)/2+Math.PI/4)/Math.Tan(ToRad(lat1)/2+Math.PI/4)); | |
if (Math.Abs(dLon) > Math.PI) | |
dLon = dLon > 0 ? -(2*Math.PI-dLon) : (2*Math.PI+dLon); | |
return ToBearing(Math.Atan2(dLon, dPhi)); | |
} | |
public static double ToRad(double degrees) | |
{ | |
return degrees * (Math.PI / 180); | |
} | |
public static double ToDegrees(double radians) | |
{ | |
return radians * 180 / Math.PI; | |
} | |
public static double ToBearing(double radians) | |
{ | |
// convert radians to degrees (as bearing: 0...360) | |
return (ToDegrees(radians) +360) % 360; | |
} | |
// verify against the website example | |
DegreeBearing(50.36389,-4.15694,42.35111,-71.04083); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find and return the distance between two points in meters | |
public double GetDistance(double lat1, double long1, double lat2, double long2) { | |
double _eQuatorialEarthRadius = 6378.1370D; | |
double _d2r = (Math.PI / 180D); | |
double dlong = (long2 - long1) * _d2r; | |
double dlat = (lat2 - lat1) * _d2r; | |
double a = Math.Pow(Math.Sin(dlat / 2D), 2D) + Math.Cos(lat1 * _d2r) * Math.Cos(lat2 * _d2r) * Math.Pow(Math.Sin(dlong / 2D), 2D); | |
double c = 2D * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1D - a)); | |
double d = _eQuatorialEarthRadius * c; | |
return d * 1000; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// flip the mirrored webcam image | |
Vector3 theScale = transform.localScale; | |
theScale.x *= -1; | |
transform.localScale = theScale; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment