Determine If Two Points Are Near

前端 未结 4 1577
轮回少年
轮回少年 2020-12-20 22:30

I have the following:

bool AreNear(Point Old, Point Current)
{
    int x1 = Convert.ToInt32(Old.X);
    int x2 = Convert.ToInt32(Current.X);
    int y1 = Con         


        
4条回答
  •  Happy的楠姐
    2020-12-20 23:04

    You can use Math.Abs to get the distance:

    public static bool InDistance(Point Old, Point Current, int distance)
    {
        int diffX = Math.Abs(Old.X - Current.X);
        int diffY = Math.Abs(Old.Y - Current.Y);
        return diffX <= distance && diffY <= distance;
    }
    

    use it:

    bool arePointsInDistance = InDistance(new Point(100, 120), new Point(120, 99), 25);
    

提交回复
热议问题