我正在移植这个 SimplexNoise从 C++ 到 C# 的模块化,并且遇到了程序员从数组中检索值的方式的问题。他有如下内容:
单工噪声.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++ 的基础知识,但据我所知,这是尝试用一维 int 数组分配整数变量,这没有任何意义。
有什么想法吗?
最佳答案
这是因为根据您链接的来源,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);
关于c# - 将 C++ 数组移植到 C# 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9552561/