且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算两点之间的距离

更新时间:2022-01-17 00:04:49

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

measure the square distance from one point to the other:

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

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

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.

或者,如果您愿意:

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

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

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.

您当然是以米为单位说话,我认为点坐标也以米为单位.

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