确定两个点附近 [英] Determine If Two Points Are Near

查看:155
本文介绍了确定两个点附近的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点:

bool AreNear(Point Old, Point Current)
{
    int x1 = Convert.ToInt32(Old.X);
    int x2 = Convert.ToInt32(Current.X);
    int y1 = Convert.ToInt32(Old.Y);
    int y2 = Convert.ToInt32(Current.Y);
    if (x1 == x2) {
        if (y1 == y2) {
            return true;
        }
    }
    return false;
}

我想在函数返回true,如果当前点为25像素老一点的半径。谁能告诉我该怎么做?

I want to return true in the function if the current point is in 25 pixels radius of the old point. Can anyone tell me how to do that?

推荐答案

您可以使用毕达哥拉斯公式<​​/一>来计算两个点之间的距离。在C#:<​​/ P>

You can use the Pythagorean formula to calculate the distance between two points. In C#:

var d = Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2)) 

为什么这项工作?看看下面的图,并记住 A ^ 2 + B ^ 2 = C ^ 2 保存为直角三角形:

Why does this work? Have a look at the following diagram and remember that a^2 + b^2 = c^2 holds for right triangles:

这篇关于确定两个点附近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆