# 前進與旋轉

``````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 = 144;
width = 1;

t1 = turtle(0, 0, 0);

t2 = forward(t1, side_leng);
polyline([get_xy(t1), get_xy(t2)], width);

t3 = forward(turn(t2, angle), side_leng);
polyline([get_xy(t2), get_xy(t3)], width);

t4 = forward(turn(t3, angle), side_leng);
polyline([get_xy(t3), get_xy(t4)], width);

t5 = forward(turn(t4, angle), side_leng);
polyline([get_xy(t4), get_xy(t5)], width);

t6 = forward(turn(t5, angle), side_leng);
polyline([get_xy(t5), get_xy(t6)], width);
``````

# 來個遞迴

``````// 略...
// 以上必須用到先前實作的海龜繪圖

module turtle_spiral(t_before, times, side_leng, angle, width) {
if(times != 0) {
t_after = forward(turn(t_before, angle), side_leng);
polyline([get_xy(t_before), get_xy(t_after)], width);
turtle_spiral(t_after, times - 1, side_leng, angle, width);
}

}

side_leng = 10;
angle = 144;
width = 1;
times = 5;

turtle_spiral(turtle(0, 0, 0), times, side_leng, angle, width);
``````

``````t_after = forward(turn(t_before, angle), side_leng);
polyline([get_xy(t_before), get_xy(t_after)], width);
``````

# 加點變化

``````// 略...
// 以上必須用到先前實作的海龜繪圖

module turtle_spiral(t_before, side_leng, d_step, min_leng, angle, width) {
if(side_leng > min_leng) {
t_after = forward(turn(t_before, angle), side_leng);
polyline([get_xy(t_before), get_xy(t_after)], width);
turtle_spiral(t_after, side_leng - d_step, d_step, min_leng, angle, width);
}

}

side_leng = 50;
d_step = 1;
min_leng = 1;
angle = 90;
width = 1;

turtle_spiral(
turtle(0, 0, 0),
side_leng,
d_step,
min_leng,
angle,
width
);
``````

``````side_leng = 50;
d_step = 0.1;
min_leng = 1;
angle = 15;
width = 1;
``````

``````// 略...
// 以上必須用到先前實作的海龜繪圖

side_leng = 30;
min_leng = 0.2;
angle = 15;
width = 1;

module turtle_spiral_by_times(t_before, times, side_leng, angle, width) {
if(times != 0) {
t_after = forward(turn(t_before, angle), side_leng * angle / 180 * 3.14159);
polyline([get_xy(t_before), get_xy(t_after)], width);

turtle_spiral_by_times(t_after, times - 1, side_leng, angle, width);
} else {
turtle_spiral(t_before, side_leng / 1.618, min_leng, angle, width);
}

}

module turtle_spiral(t_before, side_leng, min_leng, angle, width) {
if(side_leng > min_leng) {
times = 90 / angle;
turtle_spiral_by_times(t_before, 90 / angle, side_leng, angle, width);
}
}

turtle_spiral(
turtle(0, 0, 0),
side_leng,
min_leng,
angle,
width
);
``````

=