# 四條貝茲曲線

``````include <line3d.scad>;

ctrl_pts = [
[[0, 0, 20],  [60, 0, -35],   [90, 0, 60],    [200, 0, 5]],
[[0, 50, 30], [100, 60, -25], [120, 50, 120], [200, 50, 5]],
[[0, 100, 0], [60, 120, 35],  [90, 100, 60],  [200, 100, 45]],
[[0, 150, 0], [60, 150, -35], [90, 180, 60],  [200, 150, 45]]
];

thickness = 2;
t_step = 0.05;

for(i = [0:len(ctrl_pts) - 1]) {
bezier_pts = bezier_curve(t_step, ctrl_pts[i]);
polyline3d(bezier_pts, thickness);
}
``````

# 從線到面

``````include <bezier_curve.scad>;

ctrl_pts = [
[[0, 0, 20],  [60, 0, -35],   [90, 0, 60],    [200, 0, 5]],
[[0, 50, 30], [100, 60, -25], [120, 50, 120], [200, 50, 5]],
[[0, 100, 0], [60, 120, 35],  [90, 100, 60],  [200, 100, 45]],
[[0, 150, 0], [60, 150, -35], [90, 180, 60],  [200, 150, 45]]
];
thickness = 2;
t_step = 0.05;

g_pts = [for(i = [0:len(ctrl_pts) - 1])
bezier_curve(t_step, ctrl_pts[i])
];

function_grapher(g_pts, thickness);
``````

X 軸方向看來很平滑，感覺很接近了，不過，在 Y 軸方向上，因為還是只有四個控制點，因而看來仍是直線而不像是曲線。

# 用貝茲來做貝茲

``````include <line3d.scad>;

ctrl_pts = [
[[0, 0, 20],  [60, 0, -35],   [90, 0, 60],    [200, 0, 5]],
[[0, 50, 30], [100, 60, -25], [120, 50, 120], [200, 50, 5]],
[[0, 100, 0], [60, 120, 35],  [90, 100, 60],  [200, 100, 45]],
[[0, 150, 0], [60, 150, -35], [90, 180, 60],  [200, 150, 45]]
];

thickness = 2;
t_step = 0.05;

// 第一次畫四條貝茲曲線
g_pts = [for(i = [0:len(ctrl_pts) - 1])
bezier_curve(t_step, ctrl_pts[i])
];
for(i = [0:len(g_pts) - 1]) {
polyline3d(g_pts[i], thickness);
}

// 四條貝茲曲線上各自對應的四個點
crtl_j = 5;
color("red") union() {
polyline3d([for(i = [0:len(g_pts) - 1]) g_pts[i][crtl_j]], thickness);
for(i = [0:3]) {
translate(g_pts[i][5]) sphere(r = thickness * 2);
}
}

// 用四條貝茲曲線上各自對應的四個點再畫貝茲曲線
for(j = [0:len(g_pts[0]) - 1]) {
ctrl_pts2 = [for(i = [0:len(g_pts) - 1]) g_pts[i][j]];
bezier_pts = bezier_curve(t_step, ctrl_pts2);
color(j == crtl_j ? "blue" : "green") polyline3d(bezier_pts, thickness);

}
``````

``````include <bezier_curve.scad>;

ctrl_pts = [
[[0, 0, 20],  [60, 0, -35],   [90, 0, 60],    [200, 0, 5]],
[[0, 50, 30], [100, 60, -25], [120, 50, 120], [200, 50, 5]],
[[0, 100, 0], [60, 120, 35],  [90, 100, 60],  [200, 100, 45]],
[[0, 150, 0], [60, 150, -35], [90, 180, 60],  [200, 150, 45]]
];

thickness = 2;
t_step = 0.05;

bezier_pts = [for(i = [0:len(ctrl_pts) - 1])
bezier_curve(t_step, ctrl_pts[i])
];

g_pts = [for(j = [0:len(bezier_pts[0]) - 1])
bezier_curve(t_step,
[for(i = [0:len(bezier_pts) - 1]) bezier_pts[i][j]]
)
];

function_grapher(g_pts, thickness);
``````

``````include <bezier_curve.scad>;

t_step = 0.05;
thickness = 0.5;

ctrl_pts = [
[[0, 0, 20],  [60, 0, -35],   [90, 0, 60],    [200, 0, 5]],
[[0, 50, 30], [100, 60, -25], [120, 50, 120], [200, 50, 5]],
[[0, 100, 0], [60, 120, 35],  [90, 100, 60],  [200, 100, 45]],
[[0, 150, 0], [60, 150, -35], [90, 180, 60],  [200, 150, 45]]
];

g = bezier_surface(t_step, ctrl_pts);
function_grapher(g, thickness);
``````