Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate the Euclidean distance between an array in c# with function

Tags:

c#

distance

I want to calculate a euclidean distance between points that the user enter,so as you can see here :

static void Main(string[] args)
{
    int numtest = int.Parse(Console.ReadLine());
    int[,] points=new int[10,2];
    for (int i = 0; i < numtest; i++)
    {
        Console.WriteLine("point " +(i+1).ToString()+" x: ");
        points[i, 0] = int.Parse(Console.ReadLine());
        Console.WriteLine("point " + (i + 1).ToString() + " y: ");
        points[i, 1] = int.Parse(Console.ReadLine());
    }
}

public float[] calculate(int[,] points)
{
    for (int i = 0; i <points.Length ; i++)
    {

    }
}

enter image description here

is there any function in c# that can do this ?

I need to have each distance value between all points in my array

like image 314
Ehsan Akbar Avatar asked Dec 05 '25 23:12

Ehsan Akbar


1 Answers

Here is how to implement the distance calculation between two given points, to get you started:

int x0 = 0;
int y0 = 0;

int x1 = 100;
int y1 = 100;

int dX = x1 - x0;
int dY = y1 - y0;
double distance = Math.Sqrt(dX * dX + dY * dY);
like image 60
SimpleVar Avatar answered Dec 07 '25 13:12

SimpleVar