# 設定位置與角度

``````function turtle(x, y, angle) = [[x, y], angle];

function get_x(turtle) = turtle[0][0];  // 取得 x
function get_y(turtle) = turtle[0][1];  // 取得 y
function get_xy(turtle) = turtle[0];    // 取得 [x, y]
function get_angle(turtle) = turtle[1]; // 取得 angle
``````

``````t = turtle(0, 0, 0);
// 設定至 [10, 10] 座標
t[0][0] = 10;
t[0][1] = 10;
``````

``````function set_point(turtle, point) = [point, get_angle(turtle)];
``````

``````t = turtle(0, 0, 0);
// 設定至 [10, 10] 座標
new_t = set_point(t, [10, 10]);
``````

``````function set_x(turtle, x) = [[x, get_y(turtle)], get_angle(turtle)]; // 設定 x
function set_y(turtle, y) = [[get_x(turtle), y], get_angle(turtle)]; // 設定 y
function set_angle(turtle, angle) = [get_xy(turtle), angle];         // 設定 angle
``````

# 由目前位置移動指定長度

``````function forward(turtle, leng) =
turtle(
get_x(turtle) + leng * cos(get_angle(turtle)),
get_y(turtle) + leng * sin(get_angle(turtle)),
get_angle(turtle)
);
``````

``````leng = 10;
width = 1;

t = turtle(0, 0, 0);
new_t = forword(t, leng);

polyline([get_xy(t), get_xy(new_t)], width);
``````

``````width = 1;

t = turtle(0, 0, 0);
// 設定至 [10, 10] 座標
new_t = set_point(t, [10, 10]);

polyline([get_xy(t), get_xy(new_t)], width);
``````

# 旋轉海龜

``````function turn(turtle, angle) = [get_xy(turtle), get_angle(turtle) + angle];
``````

``````function turtle(x, y, angle) = [[x, y], angle];

function get_x(turtle) = turtle[0][0];
function get_y(turtle) = turtle[0][1];
function get_xy(turtle) = turtle[0];
function get_angle(turtle) = turtle[1];

function set_point(turtle, point) = [point, get_angle(turtle)];

function set_x(turtle, x) = [[x, get_y(turtle)], get_angle(turtle)];
function set_y(turtle, y) = [[get_x(turtle), y], get_angle(turtle)];
function set_angle(turtle, angle) = [get_xy(turtle), angle];

function forward(turtle, leng) =
turtle(
get_x(turtle) + leng * cos(get_angle(turtle)),
get_y(turtle) + leng * sin(get_angle(turtle)),
get_angle(turtle)
);

function turn(turtle, angle) = [get_xy(turtle), get_angle(turtle) + angle];

module line(point1, point2, width = 1, cap_round = true) {
angle = 90 - atan((point2[1] - point1[1]) / (point2[0] - point1[0]));
offset_x = 0.5 * width * cos(angle);
offset_y = 0.5 * width * sin(angle);

offset1 = [-offset_x, offset_y];
offset2 = [offset_x, -offset_y];

if(cap_round) {
translate(point1) circle(d = width, \$fn = 24);
translate(point2) circle(d = width, \$fn = 24);
}

polygon(points=[
point1 + offset1, point2 + offset1,
point2 + offset2, point1 + offset2
]);
}

module polyline(points, width = 1) {
module polyline_inner(points, index) {
if(index < len(points)) {
line(points[index - 1], points[index], width);
polyline_inner(points, index + 1);
}
}

polyline_inner(points, 1);
}

side_leng = 10;
angle = 120;
width = 1;

t = turtle(0, 0, 0);

t_p1 = forward(t, side_leng);                   // 前進 side_leng
polyline([get_xy(t), get_xy(t_p1)], width);     // 畫線

t_p2 = forward(turn(t_p1, angle), side_leng);   // 轉 angle 並前進 side_leng
polyline([get_xy(t_p1), get_xy(t_p2)], width);  // 畫線

t_p3 = forward(turn(t_p2, angle), side_leng);   // 轉 angle 並前進 side_leng
polyline([get_xy(t_p2), get_xy(t_p3)], width);  // 畫線
``````