我正试图通过使用其成员函数.sort((对通用列表进行排序,该函数使用自定义IComparer。然而,我的比较器使用外部数据来返回结果-被排序的对象代表三维点,在比较过程中,我将它们从世界坐标转换为屏幕坐标,并比较转换后的坐标-因此我得到了错误:
无法排序,因为IComparer.Compare((方法返回的结果不一致。
如何在不出现此错误的情况下对列表进行排序?
我的比较器代码:
public static int CompareSlices(Slice a, Slice b) {
var cam = Game.map.Camera();
int theta = (int)cam.rotation % Game.gameWidth;
int orientation = 1;
if (theta > 90 && theta < 270)
orientation = -1;
Vector2 aWorldCoords = a.CombinedT3().XY();
aWorldCoords.Y = aWorldCoords.Y + orientation * (a.offset.pos.Z);
Vector2 bWorldCoords = a.CombinedT3().XY();
bWorldCoords.Y = bWorldCoords.Y + orientation * (a.offset.pos.Z);
Vector2 aCameraCoords = Raylib.GetWorldToScreen2D(aWorldCoords, cam);
Vector2 bCameraCoords = Raylib.GetWorldToScreen2D(bWorldCoords, cam);
return (int)(aCameraCoords.Y + a.CombinedT3().pos.Z - bCameraCoords.Y + b.CombinedT3().pos.Z);
}
您的代码中可能存在导致问题的拼写错误:
Vector2 aWorldCoords = a.CombinedT3().XY();
aWorldCoords.Y = aWorldCoords.Y + orientation*(a.offset.pos.Z);
Vector2 bWorldCoords = a.CombinedT3().XY();
bWorldCoords.Y = bWorldCoords.Y + orientation*(a.offset.pos.Z);
即CCD_ 1和CCD_。