C# 计算两点之间的距离

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11555355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:16:59  来源:igfitidea点击:

Calculating the distance between 2 points

c#

提问by user1307376

I have two points (x1,y1) and (x2,y2). I want to know whether the points are within 5 meters of one another.

我有两个点 (x1,y1) 和 (x2,y2)。我想知道这些点是否彼此相距在 5 米以内。

采纳答案by Felice Pollano

measure the square distance from one point to the other:

测量从一个点到另一个点的平方距离:

((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) < d*d

where d is the distance, (x1,y1) are the coordinates of the 'base point' and (x2,y2) the coordinates of the point you want to check.

其中 d 是距离,(x1,y1) 是“基点”的坐标,(x2,y2) 是要检查的点的坐标。

or if you prefer:

或者,如果您更喜欢:

(Math.Pow(x1-x2,2)+Math.Pow(y1-y2,2)) < (d*d);

Noticed that the preferred one does not call Pow at all for speed reasons, and the second one, probably slower, as well does not call Math.Sqrt, always for performance reasons. Maybe such optimization are premature in your case, but they are useful if that code has to be executed a lot of times.

注意到首选的一个根本不调用 Pow 出于速度原因,而第二个可能更慢,也不调用Math.Sqrt,总是出于性能原因。在您的情况下,这种优化可能还为时过早,但如果必须多次执行该代码,它们就会很有用。

Of course you are talking in meters and I supposed point coordinates are expressed in meters too.

当然,您是在以米为单位说话,我认为点坐标也以米为单位表示。

回答by Roee Gavirel

Given points (X1,Y1) and (X2,Y2) then:

给定点 (X1,Y1) 和 (X2,Y2) 然后:

dX = X1 - X2;
dY = Y1 - Y2;

if (dX*dX + dY*dY > (5*5))
{
    //your code
}

回答by Felice Pollano

the algorithm : ((x1 - x2) ^ 2 + (y1 - y2) ^ 2) < 25

算法:((x1 - x2) ^ 2 + (y1 - y2) ^ 2) < 25

回答by DarkPh03n1X

Here is my 2 cents:

这是我的 2 美分:

double dX = x1 - x2;
double dY = y1 - y2;
double multi = dX * dX + dY * dY;
double rad = Math.Round(Math.Sqrt(multi), 3, MidpointRounding.AwayFromZero);

x1, y1 is the first coordinate and x2, y2 the second. The last line is the square root with it rounded to 3 decimal places.

x1, y1 是第一个坐标,x2, y2 是第二个坐标。最后一行是平方根,四舍五入到小数点后三位。

回答by j123b567

If you are using System.Windows.Pointdata type to represent a point, you can use

如果您使用System.Windows.Point数据类型来表示一个点,您可以使用

// assuming p1 and p2 data types
Point p1, p2;
// distanc can be calculated as follows
double distance = Point.Subtract(p2, p1).Length;


Update 2017-01-08:

2017-01-08 更新:

  • Add reference to Microsoft documentation
  • Result of Point.Subtractis System.Windows.Vectorand it has also property LengthSquaredto save one sqrtcalculation if you just need to compare distance.
  • Adding reference to WindowsBaseassembly may be needed in your project
  • You can also use operators
  • 添加对 Microsoft 文档的引用
  • 结果Point.SubtractSystem.Windows.Vector并且如果您只需要比较距离,它还具有LengthSquared保存一项sqrt计算的属性。
  • WindowsBase您的项目中可能需要添加对程序集的引用
  • 您还可以使用运算符

Example with LengthSquaredand operators

使用LengthSquared和运算符的示例

// assuming p1 and p2 data types
Point p1, p2;
// distanc can be calculated as follows
double distanceSquared = (p2 - p1).LengthSquared;

回答by Hyman Fairfield

Something like this in c# would probably do the job. Just make sure you are passing consistent units (If one point is in meters, make sure the second is also in meters)

在 c# 中这样的东西可能会完成这项工作。只要确保您通过的是一致的单位(如果一个点以米为单位,请确保第二个也是以米为单位)

private static double GetDistance(double x1, double y1, double x2, double y2)
{
   return Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
}

Called like so:

像这样调用:

double distance = GetDistance(x1, y1, x2, y2)
if(distance <= 5)
{
   //Do stuff
}

回答by destro

You can use the below formula to find the distance between the 2 points:

您可以使用以下公式来计算 2 点之间的距离:

distance*distance = ((x2 ? x1)*(x2 - x1)) + ((y2 ? y1)*(y2 - y1))