求求大佬帮我调一下我的柏林噪声
有效必关!
代码如下:
#include<bits/stdc++.h>
using namespace std;
const double pi = 14159265358979;
const double size = 50;
long long _hash(int x){//普通的哈希函数
x = (x << 2) ^ 0xb9113fce;
x *= 0xd86a32ca;
x = (x << 2) ^ 0xd0d8958b;
x *= 0x1d7539e1;
x = (x << 2) ^ 0xab63ec6c;
x *= 0x95b08412;
x = (x << 2) ^ 0xe7d7ae4e;
x *= 0xd22ee12b;
return x;
}
long long hash_(int x){
x *= 0xd22ee12b;
x = (x << 2) ^ 0xe7d7ae4e;
x *= 0x95b08412;
x = (x << 2) ^ 0xab63ec6c;
x *= 0x1d7539e1;
x = (x << 2) ^ 0xd0d8958b;
x *= 0xd86a32ca;
x = (x << 2) ^ 0xb9113fce;
return x;
}
long long _hash_(int x){
x *= 0xba8956ac;
x = (x << 2) ^ 0x295ff4a6;
x *= 0xbf351d6c;
x = (x << 2) ^ 0xbb1ea812;
x *= 0x3e119203;
x = (x << 2) ^ 0x2c3bd591;
x *= 0x114b0c94;
x = (x << 2) ^ 0x5b126d49;
return x;
}
double fade(double x){
return 6.0 * pow(x,5) - 15.0 * pow(x,4) + 10.0 * pow(x,3);
}
double perlin(int x,int y,int seed){
int chunk_x = floor(1.0 * x / size);
int chunk_y = floor(1.0 * y / size);
int ul = _hash_(seed + _hash(chunk_x) + hash_(chunk_y + 1)) % 360;//根据点位置计算晶格顶点的位置,并根据此计算其梯度向量
int ur = _hash_(seed + _hash(chunk_x + 1) + hash_(chunk_y + 1)) % 360;//同上
int dl = _hash_(seed + _hash(chunk_x) + hash_(chunk_y)) % 360;//同上
int dr = _hash_(seed + _hash(chunk_x + 1) + hash_(chunk_y)) % 360;//同上
double xu = 1.0 * (x - chunk_x * size) / size;
double yu = 1.0 * (y - chunk_y * size) / size;
double vul = xu * cos(ul * (pi / 180)) + (1 - yu) * sin(ul * (pi / 180));//计算其偏移向量和梯度向量的点积
double vur = (1 - xu) * cos(ur * (pi / 180)) + (1 - yu) * sin(ur * (pi / 180));//同上
double vdl = xu * cos(dl * (pi / 180)) + yu * sin(dl * (pi / 180));//同上
double vdr = (1 - xu) * cos(dr * (pi / 180)) + yu * sin(dr * (pi / 180));//同上
double vu = vul + fade(xu) * (vur - vul);//利用fade函数进行平滑插值
double vd = vdl + fade(xu) * (vdr - vdl);//同上
double v = vd + fade(yu) * (vu - vd);//同上
return v;
}
int main(){
int seed;
cin>>seed;
for(int i = -500;i < 500;i++){
for(int j = -500;j < 500;j++){
double x = perlin(j,i,seed);
// cout<<x<<" ";
if(0.8 >= x && x > 0.4){//可视化
cout<<"\033[31m██\033[0m";//红色
}else
if(0.4 >= x && x > 0.2){
cout<<"\033[33m██\033[0m";//黄色
}else
if(0.2 >= x && x > 0){
cout<<"\033[32m██\033[0m";//绿色
}else
if(0 >= x && x > -0.2){
cout<<"\033[36m██\033[0m";//青色
}else
if(-0.2 >= x && x > -0.4){
cout<<"\033[34m██\033[0m";//蓝色
}else
if(-0.4 >= x && x > -0.6){
cout<<"\033[35m██\033[0m";//紫色
}else
if(-0.6 >= x && x > -0.8){
cout<<"\033[37m██\033[0m";//白色
}else{
cout<<" ";//黑色
}
}
cout<<endl;
}
}
现在的问题就是它的晶格边缘的等高线会出现一个明显的拐角,就像这个一样:
另外附上我学习柏林噪声的视频链接