以使循环运行通过X Y输入的列表



我是C#编码的第一天新手,需要在毕业工作中学习它,他们给我设置了一个挑战,即通过两个不同输入(X和Y(的列表进行计算,现在我只想通过列表进行计算并取两个不同的数字并应用计算。我会把代码粘贴进去,但这是我想要运行的订单。

xInput - 100    // first number to be taken
xInput - 200    //second number to be taken
xInput - 300    // Id like the code to cycle through going from first to second agian at this point!
xInput - 400

我意识到这可能非常明显,但我对编码太陌生了,不知道从哪里开始!

List<double> xInputs = new List<double>()
{
0, 2194.233, 758.7178, 2381.054, 661.3527,
};
List<double> yInputs = new List<double>()
{
4000, 3310.551, 2875.47, 2095.711, 1287.857,
};
for (int i = 0; i < xInputs.Count; i++)
{
double xTranslation = Math.Pow(xInputs[i] - xInputs[i], 2) + Math.Pow(yInputs[i] - yInputs[i], 2);
double xySqrt = Math.Sqrt(xTranslation);
Console.WriteLine(xInputs);
}

不清楚这里的问题是什么,但如果你想要这个输出

X            Y         Dist
0         4000            0
2194.233     3310.551         2300
758.7178      2875.47     1356.547
2381.054     2095.711     3048.891
661.3527     1287.857     2791.614

然后运行这个代码

static class Program
{
static void Main(string[] args)
{
double[] xInputs = new double[]
{
0, 2194.233, 758.7178, 2381.054, 661.3527,
};
double[] yInputs = new double[]
{
4000, 3310.551, 2875.47, 2095.711, 1287.857,
};
double[] xySqrt = new double[xInputs.Length];
for (int i = 0; i < xySqrt.Length; i++)
{
xySqrt[i] = Math.Sqrt(Math.Pow(xInputs[i]-xInputs[0], 2) + Math.Pow(yInputs[i]-yInputs[0], 2));
}
Console.WriteLine($"{"X",12} {"Y",12} {"Dist",12}");
for (int i = 0; i < xySqrt.Length; i++)
{
Console.WriteLine($"{xInputs[i],12:g7} {yInputs[i],12:g7} {xySqrt[i],12:g7}");
}
}
}

当然,由于您正在学习编码,您应该认识到"x"one_answers"y"值总是分部分的,因此最好使用内置类型System.Numerics.Vector2来表示这些值。作为额外的奖励,距离函数已经作为Vector2.Distance()存在。

using System.Numerics;
static class Program
{
static void Main(string[] args)
{
Vector2[] inputs = new Vector2[] 
{
new Vector2(0,           4000f),
new Vector2(2194.233f,   3310.551f),
new Vector2(758.7178f,   2875.47f), 
new Vector2(2381.054f,   2095.711f),
new Vector2(661.3527f,   1287.857f),
};
double[] xySqrt = new double[inputs.Length];
for (int i = 0; i < xySqrt.Length; i++)
{
xySqrt[i] = Vector2.Distance(inputs[i], inputs[0]);
}
Console.WriteLine($"{"Point",24} {"Dist",12}");
for (int i = 0; i < xySqrt.Length; i++)
{
Console.WriteLine($"{inputs[i],24:g7} {xySqrt[i],12:g7}");
}
}
}

在上面的代码中,与Math.Sqrt(Math.Pow(xInputs[i]-xInputs[0], 2) + Math.Pow(yInputs[i]-yInputs[0], 2));相比,Vector2.Distance(inputs[i], inputs[0]);对读者来说更清楚

听起来您想要使用输入对遍历列表。只需对代码进行几个小的更改,这就很容易了。

for (int i = 0; i < xInputs.Count - 1; i++)
{
double xTranslation = Math.Pow(xInputs[i + 1] - xInputs[i], 2) + Math.Pow(yInputs[i + 1] - yInputs[i], 2);
double xySqrt = Math.Sqrt(xTranslation);
Console.WriteLine(xySqrt);
}

注意,列表现在在i < xInputs.Count - 1之后停止,并且第一输入用i + 1进行索引。

我还将您的WriteLine更改为输出结果。

产生:

2299.9996482369297
1499.9997886639987
1799.9996788642604
1900.0001701414897

您可能想考虑的另一种方法是:

List<(double x, double y)> inputs = new List<(double x, double y)>()
{
(0, 4000), (2194.233, 3310.551), (758.7178, 2875.47),
(2381.054, 2095.711), (661.3527, 1287.857), 
};
IEnumerable<double> distances =
from i in Enumerable.Range(0, inputs.Count - 1)
let x0 = inputs[i].x
let x1 = inputs[i + 1].x
let y0 = inputs[i].y
let y1 = inputs[i + 1].y
select Math.Sqrt(Math.Pow(x1 - x0, 2.0) + Math.Pow(y1 - y0, 2.0));
foreach (double distance in distances)
{
Console.WriteLine(distance);
}

甚至这个我认为读起来很清楚:

IEnumerable<double> distances =
from ps in inputs.Buffer(2, 1)
where ps.Count == 2
let dx = ps[1].x - ps[0].x
let dy = ps[1].y - ps[0].y
select Math.Sqrt(Math.Pow(dx, 2.0) + Math.Pow(dy, 2.0));

注意:它使用Microsoft的Interactive Framework(NuGet"System.Interactive"(来获取Buffer运算符。

最新更新