将C++数组移植到C#数组

本文关键字:数组 C++ | 更新日期: 2023-09-27 18:26:50

我正在将这个SimplexNoise方法从C++移植到C#,并且在程序员从数组中检索值的方式上遇到了问题。他有这样的东西:

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

在我的C#端口中:

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

对于我得到的那一行,无法从int[]转换为int。这是合乎逻辑的,但这在C++版本中怎么没有任何错误呢?我只知道C++的基本知识,但据我所知,这是一种尝试,用1D int数组分配一个整数变量,这没有任何意义。

有什么想法吗?

将C++数组移植到C#数组

这是因为根据链接的源,dot()需要一个数组作为其第一个参数:

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

const int* g的意思是"指向整数的指针"或"整数数组"。考虑到用法,签名所暗示的是"一个整数数组"。因此,您需要更改C#dot():的签名

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

试试这个:

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

我建议您也阅读MSDN上关于将C++移植到C#的文章(尽管它可能有点过时):http://msdn.microsoft.com/en-us/magazine/cc301520.aspx