Calculating the distance between 2 points

后端 未结 7 2301
青春惊慌失措
青春惊慌失措 2020-12-17 18:37

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

7条回答
  •  一个人的身影
    2020-12-17 19:18

    Here is my 2 cents:

    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.

提交回复
热议问题