# 黃金螺線

Golden spiral generator 是用來作為日後設計基礎的一個作品，它會產生一或數條黃金螺線，配合 `linear_extrude` 模組，在拉高的過程中調整 `scale``twist` 等參數，就能產生以下的圖案：

# Fibonacci 數列

``````function fibonacci(nth) =
nth == 0 || nth == 1 ? nth : (
fibonacci(nth - 1) + fibonacci(nth - 2)
);
``````

# 黃金矩形

``````function fibonacci(nth) =
nth == 0 || nth == 1 ? nth : (
fibonacci(nth - 1) + fibonacci(nth - 2)
);

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);
}

module golden_rectangle(from, to, thickness) {
if(from <= to) {
f1 = fibonacci(from);
f2 = fibonacci(from + 1);

polyline([[0, 0], [f1, 0], [f1, f1], [0, f1], [0, 0]], thickness);

offset = f2 - f1;

translate([0, -offset, 0]) rotate(90)
golden_rectangle(from + 1, to, thickness);
}
}

golden_rectangle(1, 6, 0.1);
``````

# 黃金螺線

``````function fibonacci(nth) =
nth == 0 || nth == 1 ? nth : (
fibonacci(nth - 1) + fibonacci(nth - 2)
);

module sector(radius, angles, fn = 24) {
r = radius / cos(180 / fn);
step = -360 / fn;

points = concat([[0, 0]],
[for(a = [angles[0] : step : angles[1] - 360])
[r * cos(a), r * sin(a)]
],
[[r * cos(angles[1]), r * sin(angles[1])]]
);

difference() {
polygon(points);
}
}

module arc(radius, angles, width = 1, fn = 24) {
difference() {
}
}

module golden_spiral(from, to, thickness) {
if(from <= to) {
f1 = fibonacci(from);
f2 = fibonacci(from + 1);

arc(f1, [0, 90], thickness, 48);

offset = f2 - f1;

translate([0, -offset, 0]) rotate(90)
golden_spiral(from + 1, to, thickness);
}
}

golden_spiral(1, 6, 1);
``````