检查点是否在圆圈内

5

本文介绍了检查点是否在圆圈内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一点用纬度/经度表示

I have a point expressed in lat/long

Position louvreMuseum = new Position( 48.861622, 2.337474 );

我有一个以米表示的半径值.我需要检查另一个点(也以 lat/long 表示)是否在圆圈内.

and I have a radius value expressed in meters. I need to check if another point, also expressed in lat/long, is inside the circle.

如果我在平坦的表面上,我可以简单地使用公式

If I were on a flat surface I can simply use the formula

(x - center_x)^2 + (y - center_y)^2 <= radius^2

正如这些 SO answer 中深入解释的那样.

as deeply explained in these SO answer.

但是根据纬度/经度的用法,由于行星的球形性质,我不能使用该公式.

However as per the latitude/longitude usage I can not use that formula because of the spherical nature of the planet.

如何计算任何给定点到中心的距离以与半径进行比较?

How can I calculate a distance from any given point to the center to be compared with the radius?

推荐答案

计算两个坐标之间距离的函数(从这里转换为C# 回答):

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.

这篇关于检查点是否在圆圈内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14