我正在处理这个怪物:
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