将分形树函数从递归转换为迭代

问题描述

我在sml中编写了以下程序,以将分形树生成到网页中(我对此感到非常自豪?)。我使用了递归函数,因为sml中没有for循环。

val width = 1000.0;
val height = 1000.0;
val length = 350.0;
val scale = 1.0 / 1.618;
val header = "<!DOCTYPE html><html><body><svg width='1000' height='1000' version='1.1' xmlns='http://www.w3.org/2000/svg'> \n";
val footer = "</svg></body></html>";
val outfile = TextIO.openOut "fractal-tree.html";

fun tree(x,y,length,angle) =
    if length > 1.0 then (
        let
            val x2 = x + length * (Math.cos angle)
            val y2 = y + length * (Math.sin angle)
            val outString = "<line x1='" ^ Real.toString x ^ "'"
            ^ " y1='" ^ Real.toString y ^ "'"
            ^ " x2='" ^ Real.toString x2 ^ "'"
            ^ " y2='" ^ Real.toString y2 ^ "'"
            ^ " style='stroke:dodgerblue;stroke-width:2'/> \n"
        in
            TextIO.output(outfile,outString);
            tree(x2,y2,length * scale,angle + Math.pi / 2.5);
            tree(x2,angle - Math.pi / 2.5)
        end
    )else ();
    
    
TextIO.output(outfile,header);
tree(width / 2.0,height,3.0 * Math.pi / 2.0);
TextIO.output(outfile,footer);
TextIO.closeOut outfile;

现在,我需要一些需要更改的代码才能将其转换为迭代函数。

这只是次要的,但是我想知道是否有一种方法可以在控制台中显示我的树。当我尝试时,我只会得到一个实数序列,然后程序停止。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)