从两个索引列表中选择,创建新对象列表

本文关键字:列表 创建 新对象 对象 选择 两个 索引 | 更新日期: 2023-09-27 18:34:12

我有两个列表:List<Vertex> points,保存顶点(x,y)和一个列表List<Triangles> triangles,保存三角形的节点(a,b,c)。因此,a,b,c 是定义三角形的点的不切。

我现在想轻松计算这些。 例如,我想让每个三角形的所有向量从 a 点到 b 点:

list<vector> p21;
foreach triangle t p21.add(new vector(p[t.b].x-p[t.a].x, p[t.b].y-p[t.a].y));

但我相信这可以使用 linq 非常优雅地完成。

类似的东西(不使用矢量而是使用"顶点")

List<Vertex> = (from triangle in triangles select a,b  and from point in points select new { pi[b].x-pi[a], pi[b].x-pi[a] };

我该怎么做?

从两个索引列表中选择,创建新对象列表

似乎做到了。这是非常自我描述的,并且尽可能简单。

var verticesAb = triangles
    .Select(t => new Vertex(
        points[b].x - points[a].x,
        points[b].y - points[a].y));

您可以/应该添加.ToList()ToArray(),如果您一次需要一个具体的集合。您在评论中的版本似乎也可以。