我正在尝试在处理 javascript 时实现分形植物(最高级别 - 6)。即使满足基本条件,我也会收到“超出最大调用堆栈大小”错误。
这是代码:第一个函数自定义绘制线根据长度、角度和原点绘制线。增量功能将角度增加 25 度。减量功能将角度减小 25 度。
var customDrawLine = function(x, y, length, angle)
{
var f={x2:'', y2:''};
f.x2 = x+(sqrt(sq(length)/(1+sq(tan (angle)))));
f.y2 = y + ((f.x2-x) * tan (angle));
line(x, y, f.x2, f.y2);
return f;
};
var incrementAngle = function(angle)
{
return (angle+25);
};
var decrementAngle = function(angle)
{
return (angle-25);
};
var fProductionRule = function(x, y, z, degrees, flag)
{
var l = {x1:'', y1:''};
if(flag === 1)
{
for (var a=0; a<2;a++)
{
l = customDrawLine(l.x1, l.y1, z, degrees);
}
}
else
{
l = customDrawLine(l.x1, l.y1, z, degrees);
}
return l;
};
var xProductionRule = function(x, y, degrees, nLevel, flag)
{
var k = {x1:'', y1:''};
var m;
k.x1 = x;
k.y1 = y;
m = degrees;
for(var z=0; z<7; z++)
{
var f = fProductionRule(k.x1, k.y1, (10-z), m, flag);
m = incrementAngle(m);
flag = 1;
{
{
xProductionRule(f.x2,f.y2, m, z);
}
m = decrementAngle(m);
xProductionRule(f.x2,f.y2, m, z);
}
m = decrementAngle(m);
f = fProductionRule(k.x1, k.y1, (10-z), m, flag);
{
m = decrementAngle(m);
f = fProductionRule(k.x1, k.y1, (10-z), m, flag);
xProductionRule(f.x2,f.y2, m, z);
}
m = incrementAngle(m);
xProductionRule(f.x2,f.y2, m, z);
}
};
var drawShape = function(x, y, degrees)
{
xProductionRule(x, y, degrees, 0, 0);
};
drawShape(10, 380, 25);