Check if point is inside a circle

前端 未结 1 523
无人及你
无人及你 2021-01-06 03:03

I have a point expressed in lat/long

Position louvreMuseum = new Position( 48.861622, 2.337474 );

and I have a radius value expressed in me

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 03:29

    Function to calculate the distance between two coordinates (converted to C# from this answer):

    double GetDistance(double lat1, double lon1, double lat2, double lon2) 
    {
        var R = 6371; // Radius of the earth in km
        var dLat = ToRadians(lat2-lat1);
        var dLon = ToRadians(lon2-lon1); 
        var a = 
            Math.Sin(dLat/2) * Math.Sin(dLat/2) +
            Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) * 
            Math.Sin(dLon/2) * Math.Sin(dLon/2);
    
        var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a)); 
        var d = R * c; // Distance in km
        return d;
    }
    
    double ToRadians(double deg) 
    {
        return deg * (Math.PI/180);
    }
    

    If the distance between the two points is less than the radius, then it is within the circle.

    0 讨论(0)
提交回复
热议问题