Calculating the distance between 2 points

后端 未结 7 2297
青春惊慌失措
青春惊慌失措 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:32

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

    // 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:

    • Add reference to Microsoft documentation
    • Result of Point.Subtract is System.Windows.Vector and it has also property LengthSquared to save one sqrt calculation if you just need to compare distance.
    • Adding reference to WindowsBase assembly may be needed in your project
    • You can also use operators

    Example with LengthSquared and operators

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

提交回复
热议问题