find the distance between two points in csharp

To find the distance between two points in C#, you can use the distance formula, which is based on the Pythagorean theorem. Here's an example code snippet:

main.cs
using System;

namespace DistanceCalculation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define the two points
            Point point1 = new Point(0, 0);
            Point point2 = new Point(3, 4);

            // Calculate the distance
            double distance = Math.Sqrt(Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2));

            Console.WriteLine("The distance between the two points is: " + distance);
            Console.ReadLine();
        }
    }

    class Point
    {
        public int X { get; set; }
        public int Y { get; set; }

        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }
    }
}
725 chars
33 lines

In this example, we define two points (0, 0) and (3, 4). We then use the distance formula to calculate the distance between the two points, which in this case is 5. We do this by subtracting the x and y values of one point from the x and y values of the other point, squaring each difference, adding the two squares together, and then taking the square root of the result.

gistlibby LogSnag