# 最簡單的方式

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

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.01``` 的圖案，在我的電腦上要花上一分多鐘才能計算出預覽圖案：

# 認識 `polyhedron`

``````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
``````

``````points = [
[0, 0, 1], [1, 0, 2],
[1, 1, 4], [0, 1, 1],
[0, 0, 2], [1, 0, 3],
[1, 1, 5], [0, 1, 2]
];

points2 = [
[1, 0, 2], [2, 0, 2],
[2, 1, 0], [1, 1, 4],
[1, 0, 3], [2, 0, 3],
[2, 1, 1], [1, 1, 5],
];

faces = [
[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(points, faces);
polyhedron(points2, faces);
``````

# 自行切割三角形

``````points = [
[[0, 0, 1], [1, 0, 2], [2, 0, 2], [3, 0, 3]],
[[0, 1, 1], [1, 1, 4], [2, 1, 0], [3, 1, 3]],
[[0, 2, 1], [1, 2, 3], [2, 2, 1], [3, 2, 3]],
[[0, 3, 1], [1, 3, 3], [2, 3, 1], [3, 3, 3]]
];

thickness = 1;

faces = [
[0, 1, 2],
[3, 4, 5],
[0, 1, 4, 3],
[1, 2, 5, 4],
[2, 0, 3, 5]
];
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][xi + 1],
points[yi + 1][xi + 1]
];
tri1_bottom = [
tri1_top[0] + z_offset,
tri1_top[1] + z_offset,
tri1_top[2] + z_offset
];

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

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

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

``````WARNING: Object may not be a valid 2-manifold and may need repair!
``````

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

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, 1, 4, 3],
[1, 2, 5, 4],
[2, 0, 3, 5]
];
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][xi + 1],
points[yi + 1][xi + 1]
];
tri1_bottom = [
tri1_top[0] + z_offset,
tri1_top[1] + z_offset,
tri1_top[2] + z_offset
];

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

// 將兩個三角形包起來，避免誤差產生破面問題
hull() polyhedron(
points = concat(tri1_top, tri1_bottom),
faces = faces
);

hull() polyhedron(
points = concat(tri2_top, tri2_bottom),
faces = faces
);

}
}
``````