2

I am converting a Python script to C# and I'm in need in some help. I just don't have any experience in Python really. These types of arrays are totally new to me.

I'm having trouble with the second to last line, var posVec = dSorted[0][1];, as well as the last line: return posVec;.

What is the actual variable type of var posVec?

Also I'm trying to return posVec, which should be a Vector3d but I'm getting this error:

Cannot implicitly convert type 'double' to 'Rhino.Geometry.Vector3d'

What am I doing wrong? Thank you!

Python:

posVec = dSorted[0][1]
return posVec

The full Python method:

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

The full C# method as I've rewritten so far:

    // 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;
    }
Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
greyBow
  • 1,298
  • 3
  • 28
  • 62
  • 1
    "_What is the actual variable type of var posVec?_" Are you asking about your C# code or your Python code? With regard to the C# code, you can quickly figure out by yourself, as Visual Studio's IntelliSense will tell you its type if you just hover with the mouse over the variable name. With regard to error message: As it says, you cannot implictly convert a floating point value (double) to a Vector3d. A 3D vector would require three floating point values to initialize (x,y,z), and not just a single one, right? –  Nov 01 '18 at 19:02
  • I guess part of my problem is that I don't understand at all what `dSorted[0][1];` is doing. Could you help me understand? – greyBow Nov 02 '18 at 03:00

1 Answers1

0

I am late to the party, but anyway if it serves someone in the future...

You are creating an array of Vector3d(dSorted) from your values of your dictionary(d), but you try to cast it to var posVec which should be a Vector3d using dSorted[0][1]. This is the notation for a jagged array, but you declared dSorted to be an array of Vector3d.

So, to access one of its items just use dSorted[0].

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
Felipe Gutierrez
  • 675
  • 6
  • 17