c# - 如何使结构组件成为变量?

标签 c# list

不确定我是否在问这个问题,或者这是否可能。 我觉得最好在相关位置的代码中直接询问我的问题,因此请在下面的代码片段中查看我的评论。

我想知道如何在不为每次迭代构建新的值列表的情况下实现这一目标。我觉得这不应该是必要的。

这个循环的大图是将 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/

相关文章:

java - Java中的排序列表

python - 如何将元组列表转换为多个列表?

python - 将字符串转换为列表并将元素转换为整数

c# - 我可以简化这个案例陈述吗?

c# - Caliburn Micro WPF : Message. Attach with guard 属性禁用整个主机控制

c# - 使用 itextsharp 将页面插入现有 PDF

java - 如何用 N 个分区而不是大小为 N 的分区对列表进行分区?

python - 优化列表范围内的数字总和

c# - 使用 .Net 2 的 Nant 脚本?为什么?

c# - 在 .msi 安装程序中包装 C# 应用程序