%Fractals1_Diamond_square2_1
%随机钻石方块法――三维
clear;clc;
%(一)字符解释
m=6;
n=2^m+1;
H=1;%系数Hurst=1~0,H=0.5是纯布朗运动;大就光滑,小就粗燥;
C=1/2^H;c=C;
%
Z=zeros(n,n);z=ones(n,n);
%Y**X*****************
for K=m:-1:1
if K<m;C=C*c;end%C递减
nK=2^K; %步长
%-------[ ]
for K0=n-2^(K-1):-(2^K):2 %Y方向计算
for J0=n-2^(K-1):-(2^K):2 %X方向计算
K2=2^(K-1);
s1=(rand-1/2)*C;
Z(J0,K0)=(Z(J0-K2,K0-K2)+Z(J0+K2,K0+K2)+....
Z(J0-K2,K0+K2)+Z(J0+K2,K0-K2))/4+s1;
%[K J0 K0 K2]
end%J0
end%K0
%-------《》
for K0=n-2^(K-1):-(2^K):2 %Y方向计算
for J0=n-2^(K-1):-(2^K):2 %X方向计算
K2=2^(K-1);
s1=(rand-1/2)*C*c;
ZJ=0;if J0-K2-K2>0;ZJ=Z(K0,J0-K2-K2);end
Z(K0,J0-K2)=(Z(K0-K2,J0-K2)+Z(K0+K2,J0-K2)+ZJ+Z(K0,J0-K2+K2))/4+s1;
if J0==n-K2;
Z(K0,J0+K2)=(Z(K0-K2,J0+K2)+Z(K0+K2,J0+K2)+Z(K0,J0+K2-K2)+0)/4+s1;
end
s1=(rand-1/2)*C*c;
ZK=0;if K0-K2-K2>0;ZK=Z(K0-K2-K2,J0);end
Z(K0-K2,J0)=(Z(K0-K2,J0-K2)+Z(K0-K2,J0+K2)+ZK+Z(K0-K2+K2,J0))/4+s1;
if K0==n-K2
Z(K0+K2,J0)=(Z(K0+K2,J0-K2)+Z(K0+K2,J0+K2)+Z(K0+K2-K2,J0)+0)/4+s1;
end
%[K J0 K0 J0-K2 K0-K2]
end%J0
end%K0
end%K
%Z
%break
la=min(min(Z));z=0*z;la
figure(1)
[X,Y]=meshgrid([1:1:n]);
%mesh(X,Y,Z);%网格
surf(X,Y,Z);hold on;%海平面
%surfc(X,Y,Z);%表面+等高线
surf(X,Y,z);hold on;%表面
shading interp;%颜色插值光滑
%colormap(hsv(128));%色调
%hidden on;%消隐
%axis off
Am=[1 n 1 n];axis(Am);
评论