Darestium
Darestium

Reputation: 653

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?

Upvotes: 1

Views: 169

Answers (2)

SameOldNick
SameOldNick

Reputation: 2437

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

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

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);

Upvotes: 2

Related Questions