[英]Converting Python array to C# and returning values
我正在將Python腳本轉換為C#,並且需要一些幫助。 我只是真的沒有任何Python經驗。 這些類型的數組對我來說是全新的。
我在倒數第二行上遇到了麻煩, var posVec = dSorted[0][1];
,以及最后一行: return posVec;
。
var posVec
的實際變量類型是var posVec
?
我也試圖返回posVec
,它應該是一個Vector3d,但是我得到這個錯誤:
無法將類型'double'隱式轉換為'Rhino.Geometry.Vector3d'
我究竟做錯了什么? 謝謝!
蟒蛇:
posVec = dSorted[0][1]
return posVec
完整的Python方法:
def getDirection(self):
#find a new vector that is at a 90 degree angle
#define dictionary
d = {}
#create an list of possible 90 degree vectors
arrPts = [(1,0,0), (-1,0,0), (0,1,0), (0,-1,0)]
vec = self.vec
vec = rs.VectorUnitize(vec)
#find the distance between the self vec
#position and one of the 4 90 degree vectors
#create a dictionary that matches the distance with the 90 degree vector
for i in range(4):
dist = rs.Distance(vec, arrPts[i])
d[dist] = arrPts[i]
#sort the dictionary. This function converts it to an array
#sort by the distances, "value"/item 0
dSorted = sorted(d.items(), key=lambda value: value[0])
#select the second item in the array which is one of the 90 degree vectors
posVec = dSorted[0][1]
return posVec
到目前為止,我已經重寫了完整的C#方法:
// find a new vector that is at a 90 degree angle
public Vector3d GetDirection()
{
// define dictionary
Dictionary<double, Vector3d> d = new Dictionary<double, Vector3d>();
Vector3d[] arrPts = new Vector3d[] {
new Vector3d(1, 0, 0),
new Vector3d(-1, 0, 0),
new Vector3d(0, 1, 0),
new Vector3d(0, -1, 0),
new Vector3d(0, 0, 1),
new Vector3d(0, 0, -1) };
_vec = Vec;
_vec.Unitize();
// find the distance between the self vec position and one of the 6 90 degree vectors
// create a dictionary that matches the distance with the 90 degree vector
for (int i = 0; i < arrPts.Length; i++)
{
double dist = Math.Sqrt(
((_vec.X - arrPts[i].X) * (_vec.X - arrPts[i].X)) +
((_vec.Y - arrPts[i].Y) * (_vec.Y - arrPts[i].Y)) +
((_vec.Z - arrPts[i].Z) * (_vec.Z - arrPts[i].Z)));
d.Add(dist, arrPts[i]);
}
Vector3d[] dSorted = d.Values.ToArray();
var posVec = dSorted[0][1];
return posVec;
}
我參加晚會很晚,但是無論如何如果將來有一個人參加...
您正在根據您的dictionary(d)
值創建一個Vector3d(dSorted)
數組,但是您嘗試使用dSorted[0][1]
將其Vector3d
為var posVec
,它應該是Vector3d
。 這是一個鋸齒狀排列的符號,但是你宣布dSorted
是數組Vector3d
。
因此,只需使用dSorted[0]
即可訪問其項之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.