polygon 與 polyhedron

February 27, 2022

使用 polygon

``````module pie(radius, angle_degrees) {
points = [
for(a = [angle_degrees[0]:angle_degrees[1]])
];
polygon([[0, 0], each points]);
}

pie(10, [45, 135]);
``````

`polygon` 還可以指定 `paths` 參數，用來指定多個頂點索引路徑，第一組頂點索引路徑會作為主要形狀，第二組以後的頂點索引路徑，都會從主要形狀中進行減集。例如，來建立一個 `ring` 模組：

``````module ring(radius, thickness) {
outer = [
for(a = [0:359])
];

inner = inner_r / radius * outer;

outer_path = [for(i = [0:359]) i];
inner_path = [for(i = [360:719]) i];

polygon(
points = concat(outer, inner),
paths = [outer_path, inner_path]
);
}

ring(10, 2);
``````

polyhedron 多面體

``````module tetrahedron(radius) {
t = (1 + sqrt(5)) / 2;
[1, 1, 1], 	[-1, -1, 1], [-1, 1, -1], [1, -1, -1]
];
tetrahedron_faces = [
[0, 1, 2], [2, 3, 0], [0, 3, 1], [1, 3, 2]
];
polyhedron(
points = tetrahedron_points,
faces = tetrahedron_faces
);
}

tetrahedron(10);
``````

``````CubePoints = [
[  0,  0,  0 ],  //0
[ 10,  0,  0 ],  //1
[ 10,  7,  0 ],  //2
[  0,  7,  0 ],  //3
[  0,  0,  5 ],  //4
[ 10,  0,  5 ],  //5
[ 10,  7,  5 ],  //6
[  0,  7,  5 ]]; //7

CubeFaces = [
[0,1,2,3],  // bottom
[4,5,1,0],  // front
[7,6,5,4],  // top
[5,6,2,1],  // right
[6,7,3,2],  // back
[7,4,0,3]]; // left

polyhedron( CubePoints, CubeFaces );
``````

``````CubePoints = [
[  0,  0,  0 ],  //0
[ 10,  0,  0 ],  //1
[ 10,  7,  0 ],  //2
[  0,  7,  0 ],  //3
[  0,  0,  5 ],  //4
[ 10,  0,  5 ],  //5
[ 10,  7,  7 ],  //6
[  0,  7,  5 ]]; //7

CubeFaces = [
[0,1,2,3],  // bottom
[4,5,1,0],  // front
[7,6,5,4],  // top
[5,6,2,1],  // right
[6,7,3,2],  // back
[7,4,0,3]]; // left

polyhedron( CubePoints, CubeFaces );
``````

``````PolySet has nonplanar faces. Attempting alternate construction
``````

實作函式圖形

``````function f(x, y) = (y ^ 2) / 4 - (x ^ 2) / 4;

min_value = -3;
max_value = 3;
resolution = 0.5;
thickness = 1;

for(x = [min_value:resolution:max_value]) {
for(y = [min_value:resolution:max_value]) {
translate([x, y, f(x, y)])
linear_extrude(thickness, center = true)
square(resolution, center = true);
}
}
``````

`resolution` 決定了 x 與 y 的步進值，值越小函式圖案就越細緻，然而，預覽或渲染的時間就會越久，模型的檔案容量也會越大，`resolution = 0.05` 的圖案，預覽就得花上一段時間，而且感覺還是不夠平滑：

``````WARNING: Normalized tree is growing past 200000 elements. Aborting normalization.
``````

``````function f(x, y) = (y ^ 2) / 4 - (x ^ 2) / 4;

min_value = -3;
max_value = 3;
resolution = 0.5;
thickness = 1;

points = [
for(y = [min_value:resolution:max_value])
[
for(x = [min_value:resolution:max_value])
[x, y, f(x, y)]
]
];

faces = [
[0, 1, 2],
[3, 4, 5],
[0, 5, 4, 1],
[0, 2, 3, 5],
[1, 4, 3, 2]
];
z_offset = [0, 0, -thickness];

for(yi = [0:len(points) - 2]) {
for(xi = [0:len(points[yi]) - 2]) {
tri1_top = [
points[yi][xi],
points[yi + 1][xi + 1],
points[yi][xi + 1]
];
tri1_bottom = [
tri1_top[2] + z_offset,
tri1_top[1] + z_offset,
tri1_top[0] + z_offset
];

tri2_top = [
points[yi][xi],
points[yi + 1][xi],
points[yi + 1][xi + 1]
];
tri2_bottom = [
tri2_top[2] + z_offset,
tri2_top[1] + z_offset,
tri2_top[0] + z_offset
];

polyhedron(
points = concat(tri1_top, tri1_bottom),
faces = faces
);

polyhedron(
points = concat(tri2_top, tri2_bottom),
faces = faces
);
}
}
``````