代码

 % --
function koch(Ax, Ay, Bx, By)
% 控制递归深度
Deepth = ;
% 控制图画大小
Size = ;
if ((Bx-Ax)^ + (By-Ay)^) < Deepth
plot([Ax, Bx], [Ay, By], '-r')
hold on
axis([ Size Size]);
else
Cx = Ax + (Bx-Ax)/;
Cy = Ay + (By-Ay)/;
Dx = Bx - (Bx-Ax)/;
Dy = By - (By-Ay)/; koch(Ax, Ay, Cx, Cy);
koch(Dx, Dy, Bx, By); L = sqrt((Dx-Cx)^ + (Dy-Cy)^);
alpha = atan((Dy-Cy)/(Dx-Cx));
% 数学
if (alpha>= && Dx-Cx<) || (alpha< && (Dx-Cx)<)
alpha = alpha + pi;
end Ex = Cx + cos(alpha + pi/)*L;
Ey = Cy + sin(alpha + pi/)*L; koch(Cx, Cy, Ex, Ey);
koch(Ex, Ey, Dx, Dy);
end
end % matlab的单步调试有助于理解该递归
% 可通过设置断点观察图形是如何一步一步被画出来的

koch曲线

koch曲线与koch雪花的MATLAB实现-LMLPHP

koch雪花

koch曲线与koch雪花的MATLAB实现-LMLPHP

04-26 02:22