将 LINQ C# 转换为 VB.NET



我正在处理这个怪物:

public static IEnumerable<Warp> GetWarps(IEnumerable<Point> sourcePoints, IEnumerable<Point> destPoints, IEnumerable<Triangle> destTriangles)
{
// build lists of source and destination landmark points
var sourceList = sourcePoints.ToList();
var destList = destPoints.ToList();
// find all three triangle points in the list of destination landmark points
var indices = from t in destTriangles
let p1 = destPoints.First(p => Math.Abs(p.X - t.P1.X) < 1 && Math.Abs(p.Y - t.P1.Y) < 1)
let p2 = destPoints.First(p => Math.Abs(p.X - t.P2.X) < 1 && Math.Abs(p.Y - t.P2.Y) < 1)
let p3 = destPoints.First(p => Math.Abs(p.X - t.P3.X) < 1 && Math.Abs(p.Y - t.P3.Y) < 1)
select new {
X1 = destList.IndexOf(p1),
X2 = destList.IndexOf(p2),
X3 = destList.IndexOf(p3)
};
// return enumeration of warps from source to destination triangles
return from x in indices
select new Warp(
new Triangle(sourceList[x.X1], sourceList[x.X2], sourceList[x.X3]),
new Triangle(destList[x.X1], destList[x.X2], destList[x.X3]));
}

我已经尝试了很多,但我没有处理语法和陷阱的经验。

我自己的方法都失败了,我不知道我是否走在正确的方向上:

Function GetWarps(ByVal sourcePoints As IEnumerable(Of Point), ByVal destPoints As IEnumerable(Of Point), ByVal destTriangles As IEnumerable(Of Triangle)) As IEnumerable(Of Warp)
Dim sourceList = sourcePoints.ToList()
Dim destList = destPoints.ToList()
Dim p1 As Triangle
Dim p2 As Triangle
Dim p3 As Triangle
Dim indices = From t In destTriangles 
p1 = destPoints.First(Function(p) Math.Abs(p.X - t.P1.X) < 1 AndAlso Math.Abs(p.Y - t.P1.Y) < 1) 
p2 = destPoints.First(Function(p) Math.Abs(p.X - t.P2.X) < 1 AndAlso Math.Abs(p.Y - t.P2.Y) < 1) 
p3 = destPoints.First(Function(p) Math.Abs(p.X - t.P3.X) < 1 AndAlso Math.Abs(p.Y - t.P3.Y) < 1) Select
(
.X1 = destList.IndexOf(p1), Key,
.X2 = destList.IndexOf(p2), Key,
.X3 = destList.IndexOf(p3), Key,
)
Return From x In indices Select New Warp(New Triangle(sourceList(x.X1), sourceList(x.X2), sourceList(x.X3)), New Triangle(destList(x.X1), destList(x.X2), destList(x.X3)))
End Function

谁能告诉我如何正确地做到这一点?

谢谢!!

我明白了!! :-(

Public Function GetWarps(ByVal sourcePoints As IEnumerable(Of Point), ByVal destPoints As IEnumerable(Of Point), ByVal destTriangles As IEnumerable(Of Triangle)) As IEnumerable(Of Warp)
' build lists of source and destination landmark points
Dim sourceList = sourcePoints.ToList()
Dim destList = destPoints.ToList()
' find all three triangle points in the list of destination landmark points
Dim indices = From t In destTriangles _
Let p1 = destPoints.First(Function(p) Math.Abs(p.X - t.P1.X) < 1 AndAlso Math.Abs(p.Y - t.P1.Y) < 1) Let p2 = destPoints.First(Function(p) Math.Abs(p.X - t.P2.X) < 1 AndAlso Math.Abs(p.Y - t.P2.Y) < 1) Let p3 = destPoints.First(Function(p) Math.Abs(p.X - t.P3.X) < 1 AndAlso Math.Abs(p.Y - t.P3.Y) < 1) _
Select New With {Key .X1 = destList.IndexOf(p1), Key .X2 = destList.IndexOf(p2), Key .X3 = destList.IndexOf(p3)}
' return enumeration of warps from source to destination triangles
Return From x In indices _
Select New Warp(New Triangle(sourceList(x.X1), sourceList(x.X2), sourceList(x.X3)), New Triangle(destList(x.X1), destList(x.X2), destList(x.X3)))
End Function

试试这个

Public Shared Function GetWarps(ByVal sourcePoints As IEnumerable(Of Point), ByVal destPoints As IEnumerable(Of Point), ByVal destTriangles As IEnumerable(Of Triangle)) As IEnumerable(Of Warp)
Dim sourceList = sourcePoints.ToList()
Dim destList = destPoints.ToList()
Dim indices = From t In destTriangles Let p1 = destPoints.First(Function(p) Math.Abs(p.X - t.P1.X) < 1 AndAlso Math.Abs(p.Y - t.P1.Y) < 1) Let p2 = destPoints.First(Function(p) Math.Abs(p.X - t.P2.X) < 1 AndAlso Math.Abs(p.Y - t.P2.Y) < 1) Let p3 = destPoints.First(Function(p) Math.Abs(p.X - t.P3.X) < 1 AndAlso Math.Abs(p.Y - t.P3.Y) < 1) Select New With {Key
.X1 = destList.IndexOf(p1), Key
.X2 = destList.IndexOf(p2), Key
.X3 = destList.IndexOf(p3)
}
Return From x In indices Select New Warp(New Triangle(sourceList(x.X1), sourceList(x.X2), sourceList(x.X3)), New Triangle(destList(x.X1), destList(x.X2), destList(x.X3)))
End Function

最新更新