不确定我是否在问这个问题,或者这是否可能。 我觉得最好在相关位置的代码中直接询问我的问题,因此请在下面的代码片段中查看我的评论。
我想知道如何在不为每次迭代构建新的值列表的情况下实现这一目标。我觉得这不应该是必要的。
这个循环的大图是将 3D 点的各个维度绘制成三个新的 2D 图。希望这是有道理的。
for (int i = 0; i < 3; i++) // 3 iterations (X,Y;Z)
{
// what here? how to make the data component of Vector3D a variable
for (int k = 0; k <= Points.Count - 1; k++)
{
Vector2D TL = new Vector2D();
TL.x = ((1 / (float)FrameCount.Sum()) * k);
TL.y = Points[k].x; // on i = 0 want Points[k].x
// on i = 1 want Points[k].y
// on i = 2 want Points[k].z
TimelinePoints.Add(TL); // just collect to a flat list for now
}
}
最佳答案
一个选项是拥有一组可应用于点的提取函数。然后您可以使用 LINQ Select
接受 Func<TInput, int, TOutput>
的重载生成一系列要添加的值,并将其添加到 TimeLinePoints
那样。
// Possibly store this in a static variable somewhere
var extractors = new Func<Point, float>[] { p => p.x, p => p.y, p => p.z };
// Just do this once; store the result as a float for simplicity when dividing later.
float frameSum = FrameCount.Sum();
foreach (var extractor in extractors)
{
TimeLinePoints.AddRange(Points.Select((point, index) =>
new Vector2D(index / frameSum, extractor(point));
}
(您可能会进一步使用 SelectMany
,但这就是我要开始的地方...)
关于c# - 如何使结构组件成为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48853423/