我已经完全掌握了3D中Perlin Noise的艺术,现在我正在尝试将相同的实现用于2D算法.问题似乎在于选择我的渐变方向.在3D中,我在均匀分布的方向上使用16个渐变,这非常有用.在2D我想我会使用8个渐变.上,下,左,右和四个对角线方向.
这是我得到的:

噪声的一般外观总是正确的,但正方形的边缘并不完全匹配.我也尝试使用其他渐变或更少的渐变,但得到类似的结果.在另一个例子中,您可以看到边缘有时会匹配,并且该区域的结果很好 -

当我不使用渐变而只是在4个角中的每个角落随机拾取的值之间进行插值时,我得到了正确的结果,这就是让我觉得渐变部分会弄乱它的原因.
这是我的代码:
//8 different gradient directions
private Point[] grads = new Point[] {
new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(1, -1),
new Point(0, -1), new Point(-1, -1), new Point(-1, 0), new Point(-1, 1),};
//takes the dot product of a gradient and (x, y)
private float dot2D(int i, float x, float y)
{
return
grads[i].X * x + grads[i].Y * y;
}
public float Noise2D(float x, float y)
{
int
ix = …Run Code Online (Sandbox Code Playgroud) I want to modify the famous binary search algorithm to return the index of the next bigger item instead of the key being searched.
So we have 4 cases:
e.g:
data = { 1, 3, 5, 7, 9, 11 };
Run Code Online (Sandbox Code Playgroud)