From 53e86a14fa8f8cc7badce28c5e1a5b96fcd4a010 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 9 Apr 2020 14:31:08 +0800 Subject: [PATCH] symmetric --- examples/voronoi/voronoi_penholder.scad | 62 ++++++------------------- src/experimental/voronoi_square.scad | 32 ++++++++----- 2 files changed, 36 insertions(+), 58 deletions(-) diff --git a/examples/voronoi/voronoi_penholder.scad b/examples/voronoi/voronoi_penholder.scad index f79bacfa..26698c8e 100644 --- a/examples/voronoi/voronoi_penholder.scad +++ b/examples/voronoi/voronoi_penholder.scad @@ -1,53 +1,21 @@ -use ; -use ; -use ; +use ; use ; -use ; +use ; -xy = [100, 40]; -pt_nums = 20; +size = [300, 120]; +grid_w = 30; thickness = 2; +spacing = 3; +seed = 5; +$fn = 24; -voronoi_penholder(xy, pt_nums, thickness); +color("black") +bend_extrude(size, thickness = thickness, angle = 360) + voronoi_square(size, grid_w, seed, spacing); -module voronoi_penholder(xy, pt_nums, thickness) { - xs1 = rands(0, xy[0], pt_nums); - ys1 = rands(0, xy[1], pt_nums); - points = [for(i = [0:len(xs1) - 1]) [xs1[i], ys1[i]]]; +r = size[0] / (2 * PI); +linear_extrude(size[1]) + arc(radius = r - thickness, angle = [0, 360], width = thickness / 2); - cpts = concat( - [for(p = points) if(p[0] > xy[0] - 10) p + [-xy[0], 0]], - points, - [for(p = points) if(p[0] < 10) p + [xy[0], 0]] - ); - - function default_region_size(points) = - let( - xs = [for(p = points) p[0]], - ys = [for(p = points) abs(p[1])] - ) - max([(max(xs) - min(xs) / 2), (max(ys) - min(ys)) / 2]); - - size = default_region_size(cpts); - region_shape = shape_square(size, corner_r = size / 5); - - cells = voronoi2d_cells(cpts, region_shape); - bend_extrude(size = [xy[0], xy[1]], thickness = thickness, angle = 360, $fn = 48) - { - for(i = [0:len(cpts) - 1]) { - cell = cells[i]; - hull_polyline2d(concat(cell, [cell[0]]), width = thickness); - - } - } - - r = 0.5 * xy[0] / PI; - - linear_extrude(thickness) - circle(r, $fn = 48); - - translate([0, 0, xy[1]]) - linear_extrude(thickness) - hollow_out(shell_thickness = thickness) - circle(r, $fn = 48); -} \ No newline at end of file +linear_extrude(thickness) + circle(r); \ No newline at end of file diff --git a/src/experimental/voronoi_square.scad b/src/experimental/voronoi_square.scad index c751746c..b393f9ec 100644 --- a/src/experimental/voronoi_square.scad +++ b/src/experimental/voronoi_square.scad @@ -3,28 +3,32 @@ module voronoi_square(size, grid_w, seed, spacing = 1, r = 0, delta = 0, chamfer function _lookup_noise_table(i) = _noise_table[i % 256]; - function cell_pt(fcord, seed, x, y) = + function cell_pt(fcord, seed, x, y, gw, gh) = let( nx = fcord[0] + x, ny = fcord[1] + y, - sd_base = abs(nx + ny * grid_w), + sd_x = nx < 0 ? nx + gw : + nx >= gw ? nx % gw : nx, + sd_y = ny < 0 ? ny + gh : + ny >= gh ? ny % gh : ny, + sd_base = abs(sd_x + sd_y * grid_w), sd1 = _lookup_noise_table(seed + sd_base), sd2 = _lookup_noise_table(sd1 * 255 + sd_base) ) [(nx + sd1) * grid_w, (ny + sd2) * grid_w]; // 21-nearest-neighbor - function _neighbors(fcord, seed, grid_w) = + function _neighbors(fcord, seed, grid_w, gw, gh) = concat( [ for(y = [-1:1]) for(x = [-1:1]) - cell_pt(fcord, seed, x, y) + cell_pt(fcord, seed, x, y, gw, gh) ], - [for(x = [-1:1]) cell_pt(fcord, seed, x, -2)], - [for(x = [-1:1]) cell_pt(fcord, seed, x, 2)], - [for(y = [-1:1]) cell_pt(fcord, seed, -2, y)], - [for(y = [-1:1]) cell_pt(fcord, seed, 2, y)] + [for(x = [-1:1]) cell_pt(fcord, seed, x, -2, gw, gh)], + [for(x = [-1:1]) cell_pt(fcord, seed, x, 2, gw, gh)], + [for(y = [-1:1]) cell_pt(fcord, seed, -2, y, gw, gh)], + [for(y = [-1:1]) cell_pt(fcord, seed, 2, y, gw, gh)] ); region_size = grid_w * 3; @@ -44,13 +48,19 @@ module voronoi_square(size, grid_w, seed, spacing = 1, r = 0, delta = 0, chamfer sd = is_undef(seed) ? rands(0, 255, 1)[0] : seed; - cell_nbrs_lt = [for(cy = [0:grid_w:size[1]]) - for(cx = [0:grid_w:size[0]]) + + gw = size[0] / grid_w; + gh = size[1] / grid_w; + + cell_nbrs_lt = [for(cy = [-grid_w:grid_w:size[1]]) + for(cx = [-grid_w:grid_w:size[0]]) let( nbrs = _neighbors( [floor(cx / grid_w), floor(cy / grid_w)], sd, - grid_w + grid_w, + gw, + gh ), p = nbrs[4], points = concat(