From ced32c5615df980a2e564be7e69f36fa94f1032e Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 4 Dec 2020 15:42:21 +0800 Subject: [PATCH] support 2d/3d --- src/voxel/_impl/_vx_bezier_impl.scad | 25 +++++++++++++++++++++---- src/voxel/vx_bezier.scad | 4 ++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/voxel/_impl/_vx_bezier_impl.scad b/src/voxel/_impl/_vx_bezier_impl.scad index b62382ab..08eda6a8 100644 --- a/src/voxel/_impl/_vx_bezier_impl.scad +++ b/src/voxel/_impl/_vx_bezier_impl.scad @@ -1,4 +1,4 @@ -function _vx_bezier_pt(p1, p2, p3, p4, pts) = +function _vx_bezier_pt3(p1, p2, p3, p4, pts) = let( a1 = (p1 + p2) * 0.5, a2 = (p2 + p3) * 0.5, @@ -8,9 +8,26 @@ function _vx_bezier_pt(p1, p2, p3, p4, pts) = c = (b1 + b2) * 0.5, p = [round(c[0]), round(c[1]), round(c[2])] ) - _vx_bezier(c, b2, a3, p4, _vx_bezier(p1, a1, b1, c, concat(pts, [p]))); + _vx_bezier3(c, b2, a3, p4, _vx_bezier3(p1, a1, b1, c, concat(pts, [p]))); -function _vx_bezier(p1, p2, p3, p4, pts) = +function _vx_bezier3(p1, p2, p3, p4, pts) = (abs(p1[0] - p4[0]) < 1 && abs(p1[1] - p4[1]) < 1 && abs(p1[2] - p4[2]) < 1) ? pts : - _vx_bezier_pt(p1, p2, p3, p4, pts); \ No newline at end of file + _vx_bezier_pt3(p1, p2, p3, p4, pts); + +function _vx_bezier_pt2(p1, p2, p3, p4, pts) = + let( + a1 = (p1 + p2) * 0.5, + a2 = (p2 + p3) * 0.5, + a3 = (p3 + p4) * 0.5, + b1 = (a1 + a2) * 0.5, + b2 = (a2 + a3) * 0.5, + c = (b1 + b2) * 0.5, + p = [round(c[0]), round(c[1])] + ) + _vx_bezier2(c, b2, a3, p4, _vx_bezier2(p1, a1, b1, c, concat(pts, [p]))); + +function _vx_bezier2(p1, p2, p3, p4, pts) = + (abs(p1[0] - p4[0]) < 1 && abs(p1[1] - p4[1]) < 1) ? + pts : + _vx_bezier_pt2(p1, p2, p3, p4, pts); \ No newline at end of file diff --git a/src/voxel/vx_bezier.scad b/src/voxel/vx_bezier.scad index ddda5aff..c66c7d3d 100644 --- a/src/voxel/vx_bezier.scad +++ b/src/voxel/vx_bezier.scad @@ -5,7 +5,7 @@ use <_impl/_vx_bezier_impl.scad>; function vx_bezier(p1, p2, p3, p4) = let( is2d = len(p1) == 2, - pts = is2d ? _vx_bezier(__to3d(p1), __to3d(p2), __to3d(p3), __to3d(p4), []) : - _vx_bezier(p1, p2, p3, p4, []) + pts = is2d ? _vx_bezier2(__to3d(p1), __to3d(p2), __to3d(p3), __to3d(p4), []) : + _vx_bezier3(p1, p2, p3, p4, []) ) is2d ? [for(p = pts) __to2d(p)] : pts; \ No newline at end of file