コード例 #1
0
        public static Double DistanceBetweenKilometers(GeodeticCoordinate from, GeodeticCoordinate to)
        {
            var fromLatitudeRadians = DegreesToRadians(from.Latitude);
            var toLatitudeRadians   = DegreesToRadians(to.Latitude);

            var deltaLongitudeRadians = DegreesToRadians(to.Longitude - from.Longitude);
            var deltaLatidudeRadians  = DegreesToRadians(to.Latitude - from.Latitude);

            // haversine formula https://en.wikipedia.org/wiki/Haversine_formula

            var distance = EarthRadiusKilometers * 2 *
                           Math.Asin(Math.Sqrt(
                                         Math.Pow(Math.Sin(deltaLatidudeRadians / 2), 2) +
                                         Math.Cos(fromLatitudeRadians) *
                                         Math.Cos(toLatitudeRadians) *
                                         Math.Pow(Math.Sin(deltaLongitudeRadians / 2), 2))
                                     );

            return(distance);
        }
コード例 #2
0
        public static GeodeticCoordinate NextGeodeticCoordinate(this Random random)
        {
            // generate random coordinates on a sphere using Alternative Method 2 in
            // http://corysimon.github.io/articles/uniformdistn-on-sphere/

            while (true)
            {
                var uniformCartesianX = random.NextDoubleFromNegativeOneToOne();
                var uniformCartesianY = random.NextDoubleFromNegativeOneToOne();
                var uniformCartesianZ = random.NextDoubleFromNegativeOneToOne();

                var distanceFromOrigin = DistanceFromOrigin(uniformCartesianX, uniformCartesianY, uniformCartesianZ);
                var isCurrentPointOutsideUnitSphere = distanceFromOrigin > 1;

                if (isCurrentPointOutsideUnitSphere)
                {
                    continue;
                }

                // convert to spherical using conversions on
                // https://en.wikipedia.org/wiki/Spherical_coordinate_system

                var sphericalTheta = Math.Acos(uniformCartesianZ / distanceFromOrigin);
                var sphericalPhi   = Math.Atan2(uniformCartesianY, uniformCartesianX);

                var latitude  = RadiansToDegrees(sphericalTheta) - 90.0;
                var longitude = RadiansToDegrees(sphericalPhi);

                var result = new GeodeticCoordinate
                {
                    Latitude  = latitude,
                    Longitude = longitude
                };
                return(result);
            }
        }