简体   繁体   中英

Porting C++ Arrays to C# Arrays

I am porting this SimplexNoise modual from C++ to C#, and have ran into issues with the way the programmer has retrevied values from arrays. He has something like as follows:

simplexnoise.h

static const int grad3[12][3] = {
    {1,1,0}, {-1,1,0}, {1,-1,0}, {-1,-1,0},
    {1,0,1}, {-1,0,1}, {1,0,-1}, {-1,0,-1},
    {0,1,1}, {0,-1,1}, {0,1,-1}, {0,-1,-1}
};

simplesxnoise.cpp

n1 = t1 * t1 * dot(grad3[gi1], x1, y1);

And in my C# port:

SimplexNoise.cs

    private static int[][] grad3 = new int[][] { new int[] {1,1,0}, new int[] {-1,1,0}, new int[] {1,-1,0}, new int[] {-1,-1,0},
                                                 new int[] {1,0,1}, new int[] {-1,0,1}, new int[] {1,0,-1}, new int[] {-1,0,-1},
                                                 new int[] {0,1,1}, new int[] {0,-1,1}, new int[] {0,1,-1}, new int[] {0,-1,-1}};

...

    n1 = t1 * t1 * dot(grad3[gi1], x1, y1);

And for that line I get, cannot convert from int[] to int. Which is logical but how does this not have any errors in the C++ version? I only know the basics of C++ but from what I know that is an attempt to assign an integer variable with a 1D int array, which just doesn't make any sence.

Any ideas?

This is because according to the source that you linked, dot() expects an array as its first argument:

float dot(const int* g, const float x, const float y);

const int* g means "a pointer to an integer", or "an integer array". Considering the usage, it is "an integer array" that the signature implies. Hence, you need to change the signature of your C# dot() :

float dot(int g[], float x, float y);

Try this:

int grad3[,] = { 
                {1,1,0}, {-1,1,0}, {1,-1,0}, {-1,-1,0},
                {1,0,1}, {-1,0,1}, {1,0,-1}, {-1,0,-1},
                {0,1,1}, {0,-1,1}, {0,1,-1}, {0,-1,-1}
               };

I suggest you also read this MSDN article (although it might be a little out of date) on porting C++ to C#: http://msdn.microsoft.com/en-us/magazine/cc301520.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM