# 圓的組成

``````radius = 10;
height = 20;
thickness = 1;
\$fn = 24;

render() intersection() {
difference() {
cylinder(
h = height, center = true);
cylinder(
h = height, center = true);
}

rotate([90, 0, 0]) linear_extrude(radius + thickness)
text("A", size = height, valign = "center", halign = "center");
}
``````

# 程式的實作

``````linear_extrude(thickness)
translate([font_size / 2, font_size / 2, 0])
rotate(90)
text("A", size = font_size, valign = "center", halign = "center");
``````

OpenSCAD 中，`polygon` 模組可以指定座標來建立多邊形，因此可以使用 `polygon` 模組來建立三角形；如果圓由 `fn` 個三角形組成，那麼兩個等腰的夾角就是 `a = 360 / fn`，若兩個等腰形成的頂點為原點，那麼另兩個頂點的座標就是 `[radius * cos(a / 2), radius * sin(a / 2)]``[radius * cos(a / 2), -radius * sin(a / 2)]`，因此，我們建立以下的 `one_over_fn_for_circle` 模組：

``````radius = 10;
\$fn = 24;

a = 360 / fn;
x = radius * cos(a / 2);
y = radius * sin(a / 2);
polygon(points=[[0, 0], [x, y],[x, -y]]);
}

``````

``````tx = "A";
font_size = 20;
thickness = 1;
\$fn = 24;

a = 360 / fn;
x = radius * cos(a / 2);
y = radius * sin(a / 2);
polygon(points=[[0, 0], [x, y],[x, -y]]);
}

module text_to_cylinder(tx, font_size, thickness, fn) {
r = font_size / 6.28318;
a = 360 / fn;
y = r * sin(a / 2);
for(i = [0 : fn - 1]) {
// 將交集結果移動、轉回圓的一部份
rotate(a * i) translate([0, -(2 * y * i + y), 0])

// 取交集
intersection() {
// 將三角形攤開
translate([0, 2 * y * i + y, 0])
linear_extrude(font_size)
one_over_fn_for_circle(r, fn);

// 文字立起
translate([r - thickness, 0, font_size])
rotate([0, 90, 0])
// 文字
linear_extrude(thickness)
translate([font_size / 2, font_size / 2, 0])
rotate(90)
text(tx, size = font_size, valign = "center", halign = "center");
}
}
}

text_to_cylinder(tx, font_size, thickness, \$fn);
``````

``````tx = "A";
font_size = 20;
thickness = 1;
fn = 24;

a = 360 / fn;
x = radius * cos(a / 2);
y = radius * sin(a / 2);
polygon(points=[[0, 0], [x, y],[x, -y]]);
}

module square_to_cylinder(length, width, square_thickness, fn) {
r = length / 6.28318;
a = 360 / fn;
y = r * sin(a / 2);
for(i = [0 : fn - 1]) {
// 將交集結果移動、轉回圓的一部份
rotate(a * i) translate([0, -(2 * y * i + y), 0])
// 取交集
intersection() {
// 將三角形攤開
translate([0, 2 * y * i + y, 0])
linear_extrude(width)
one_over_fn_for_circle(r, fn);
// 圖案立起
translate([r - square_thickness, 0, width])
rotate([0, 90, 0])
children(0);
}
}
}

square_to_cylinder(font_size, font_size, thickness, fn)
linear_extrude(thickness)
translate([font_size / 2, font_size / 2, 0])
rotate(90)
text(tx, size = font_size, valign = "center", halign = "center");
``````