使用函数计算c#中数组之间的欧几里得距离 [英] calculate the Euclidean distance between an array in c# with function
本文介绍了使用函数计算c#中数组之间的欧几里得距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算用户输入的点之间的欧几里得距离,如您所见:
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++)
{
}
}
c#中是否有任何功能可以做到这一点?
is there any function in c# that can do this ?
我需要在数组中所有点之间具有每个距离值
I need to have each distance value between all points in my array
推荐答案
尝试关注
public void calculate(double[,] points)
{
var distanceArray = new double[points.Length, points.Length];
for (int i = 0; i < points.Length; i++)
for (int j = 0; j < points.Length; j++)
distanceArray[i, j] = Distance(points[i, 0], points[i, 1], points[j, 0], points[j, 1]);
}
public static double Distance(double x1, double y1, double x2, double y2)
=> Math.Sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
这篇关于使用函数计算c#中数组之间的欧几里得距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文