ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

DistanceTo

Gets the distance between two 2D points where a point is given using the standard .NET Point class in System.Drawing namespace

Source

static class PointExtensionMethods
{
    public static double DistanceTo(this Point point, Point otherPoint)
    {
        int dx = point.X - otherPoint.X;
        int dy = point.Y - otherPoint.Y;
        return Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
    }
}

Example

Point point1 = new Point(0,0);
Point point2 = new Point(5, 5);
double distance = point1.DistanceTo(point2);

Author: Jan Oonk

Submitted on: 5 mrt 2020

Language: csharp

Type: System.Drawing.Point

Views: 2923