# 2D 海龜繪圖

March 11, 2022

## t2d 函式

``````use <polyline_join.scad>

angle = 144;
length = 10;
width = 0.5;

// 每次要海龜重複執行的指令串
commands = [["turn", angle], ["forward", length]];

// 建立一隻海龜，預設位於原點，頭朝著 x 軸
t0 = t2d();

t1 = t2d(t0, commands);
polyline_join([t2d(t0, "point"), t2d(t1, "point")])
circle(width);

t2 = t2d(t1, commands);
polyline_join([t2d(t1, "point"), t2d(t2, "point")])
circle(width);

t3 = t2d(t2, commands);
polyline_join([t2d(t2, "point"), t2d(t3, "point")])
circle(width);

t4 = t2d(t3, commands);
polyline_join([t2d(t3, "point"), t2d(t4, "point")])
circle(width);

t5 = t2d(t4, commands);
polyline_join([t2d(t4, "point"), t2d(t5, "point")])
circle(width);
``````

`t2d` 是個多功能函式，可以用來建立海龜，你可以使用 list 來組織要海龜重複執行的指令串，由於 OpenSCAD 不能改變 list 的狀態，`t2d` 執行後都會傳回建立一隻新海龜，包含了執行指令串後的狀態，可以透過指定 `"point"` 來取得海龜目前的座標位置，就上例而言，每次旋轉 144 度，前進 10 mm，重複 5 次，可以畫出五角星形：

``````use <polyline_join.scad>

angle = 170;
length = 100;
n = 40;
width = 0.5;
commands = [["turn", angle], ["forward", length]];

module repeat(t, commands, n, width) {
if(n > 0) {
new_t = t2d(t, commands);
polyline_join([t2d(t, "point"), t2d(new_t, "point")])
circle(width);
repeat(new_t, commands, n - 1, width);
}
}

repeat(t2d(), commands, n, width);
``````

## footprints2 函式

``````use <polyline_join.scad>

angle = 144;
length = 10;
width = 0.5;

path = footprints2([
["turn", angle],
["forward", length],
["turn", angle],
["forward", length],
["turn", angle],
["forward", length],
["turn", angle],
["forward", length],
["turn", angle],
["forward", length]
]);

polyline_join(path)
circle(width);
``````

``````use <polyline_join.scad>

angle = 170;
length = 100;
n = 40;
width = 0.5;

commands = [["turn", angle], ["forward", length]];
path = footprints2([
for(i = [0:n]) each commands
]);

polyline_join(path)
circle(width);
``````