# 2D 版樹木曲線

``````// 必須用到之前寫的 3D 海龜繪圖程式碼

leng = 50;
leng_scale = 0.7;
angle = 20;
width = 2;
fn = 4;

t = turtle3D(
pt3D(0, 0, 0),
[pt3D(1, 0, 0), pt3D(0, 1, 0), pt3D(0, 0, 1)] // 海龜座標向量
);

module tree(t, leng, leng_scale, angle, width, fn) {
t2 = moveX(t, leng);
polyline3D([getPt(t), getPt(t2)], width, ``````fn);

t3 = moveX(turnZ(t2, angle), leng * leng_scale);
polyline3D([getPt(t2), getPt(t3)], width, ``````fn);

t4 = moveX(turnZ(t2, -angle), leng * leng_scale);
polyline3D([getPt(t2), getPt(t4)], width, ``````fn);
}

tree(t, leng, leng_scale, angle, width, fn);
``````

``````// 必須用到之前寫的 3D 海龜繪圖程式碼

leng = 50;
leng_limit = 5;
leng_scale = 0.7;
angle = 20;
width = 2;
fn = 4;

t = turtle3D(
pt3D(0, 0, 0),
[pt3D(1, 0, 0), pt3D(0, 1, 0), pt3D(0, 0, 1)] // 海龜座標向量
);

module tree(t, leng, leng_scale, leng_limit, angle, width, fn) {
if(leng > leng_limit) {
t2 = moveX(t, leng);
polyline3D([getPt(t), getPt(t2)], width, ``````fn);

t3 = moveX(turnZ(t2, angle), leng * leng_scale);
polyline3D([getPt(t2), getPt(t3)], width, ``````fn);

t4 = moveX(turnZ(t2, -angle), leng * leng_scale);
polyline3D([getPt(t2), getPt(t4)], width, ``````fn);

tree(
turnZ(t2, angle),
leng * leng_scale,
leng_scale,
leng_limit,
angle, width, fn);

tree(
turnZ(t2, -angle),
leng * leng_scale,
leng_scale,
leng_limit,
angle, width, fn);

}
}

tree(t, leng, leng_scale, leng_limit, angle, width, fn);
``````

2D 版本的遞迴樹就出現了：

``````t3 = moveX(turnZ(t2, angle), leng * leng_scale);
polyline3D([getPt(t2), getPt(t3)], width, ``````fn);

t4 = moveX(turnZ(t2, -angle), leng * leng_scale);
polyline3D([getPt(t2), getPt(t4)], width, ``````fn);
``````

``````// 必須用到之前寫的 3D 海龜繪圖程式碼

leng = 50;
leng_limit = 5;
leng_scale = 0.7;
angle = 20;
width = 2;
fn = 4;

t = turtle3D(
pt3D(0, 0, 0),
[pt3D(1, 0, 0), pt3D(0, 1, 0), pt3D(0, 0, 1)] // 海龜座標向量
);

module tree(t, leng, leng_scale, leng_limit, angle, width, fn) {
if(leng > leng_limit) {
t2 = moveX(t, leng);
polyline3D([getPt(t), getPt(t2)], width, ``````fn);

tree(
turnZ(t2, angle),
leng * leng_scale,
leng_scale,
leng_limit,
angle, width, fn);

tree(
turnZ(t2, -angle),
leng * leng_scale,
leng_scale,
leng_limit,
angle, width, fn);

}
}

tree(t, leng, leng_scale, leng_limit, angle, width, fn);
``````

# 3D 版樹木曲線

``````// 必須用到之前寫的 3D 海龜繪圖程式碼

leng = 50;
leng_limit = 5;
leng_scale = 0.7;
angle1 = 20;
angle2 = 0;
width = 2;
fn = 4;

t = turtle3D(
pt3D(0, 0, 0),
[pt3D(1, 0, 0), pt3D(0, 1, 0), pt3D(0, 0, 1)] // 海龜座標向量
);

module tree(t, leng, leng_scale, leng_limit, angle1, angle2, width, fn) {
if(leng > leng_limit) {
t2 = moveX(t, leng);
polyline3D([getPt(t), getPt(t2)], width, ``````fn);

tree(
turnZ(t2, angle1),
leng * leng_scale, leng_scale, leng_limit,
angle1, angle2,
width, fn);

tree(
turnZ(t2, angle2),
leng * leng_scale, leng_scale, leng_limit,
angle1, angle2,
width, fn);
}
}

tree(t, leng, leng_scale, leng_limit, angle1, angle2, width, fn);
``````

``````// 必須用到之前寫的 3D 海龜繪圖程式碼

leng = 50;
leng_limit = 5;
leng_scale = 0.7;
angle1 = 20;
angle2 = 127;
width = 2;
fn = 4;

t = turtle3D(
pt3D(0, 0, 0),
[pt3D(1, 0, 0), pt3D(0, 1, 0), pt3D(0, 0, 1)] // 海龜座標向量
);

module tree(t, leng, leng_scale, leng_limit, angle1, angle2, width, fn) {
if(leng > leng_limit) {
t2 = moveX(t, leng);
polyline3D([getPt(t), getPt(t2)], width, ``````fn);

tree(
turnZ(t2, angle1),
leng * leng_scale, leng_scale, leng_limit,
angle1, angle2,
width, fn);

tree(
turnX(t2, angle2),
leng * leng_scale, leng_scale, leng_limit,
angle1, angle2,
width, fn);
}
}

tree(t, leng, leng_scale, leng_limit, angle1, angle2, width, fn);
``````

``````// 必須用到之前寫的 3D 海龜繪圖程式碼

leng = 100;
leng_limit = 1;
leng_scale1 = 0.4;
leng_scale2 = 0.9;
angleZ = 60;
angleX = 135;
width = 2;
fn = 4;

t = turtle3D(
pt3D(0, 0, 0),
[pt3D(1, 0, 0), pt3D(0, 1, 0), pt3D(0, 0, 1)] // 海龜座標向量
);

module tree(t, leng, leng_scale1, leng_scale2, leng_limit,
angleZ, angleX, width, fn) {
if(leng > leng_limit) {
t2 = moveX(t, leng);
polyline3D([getPt(t), getPt(t2)], width, ``````fn);

tree(
turnZ(t2, angleZ),
leng * leng_scale1, leng_scale1, leng_scale2, leng_limit,
angleZ, angleX,
width, fn);

tree(
turnX(t2, angleX),
leng * leng_scale2, leng_scale1, leng_scale2, leng_limit,
angleZ, angleX,
width, fn);
}
}

tree(t, leng, leng_scale1, leng_scale2, leng_limit,
angleZ, angleX, width, fn);
``````