mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-11 17:24:20 +02:00
symmetric
This commit is contained in:
@@ -1,53 +1,21 @@
|
||||
use <hull_polyline2d.scad>;
|
||||
use <shape_square.scad>;
|
||||
use <hollow_out.scad>;
|
||||
use <experimental/voronoi_square.scad>;
|
||||
use <bend_extrude.scad>;
|
||||
use <experimental/voronoi2d_cells.scad>;
|
||||
use <arc.scad>;
|
||||
|
||||
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]]];
|
||||
|
||||
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;
|
||||
r = size[0] / (2 * PI);
|
||||
linear_extrude(size[1])
|
||||
arc(radius = r - thickness, angle = [0, 360], width = thickness / 2);
|
||||
|
||||
linear_extrude(thickness)
|
||||
circle(r, $fn = 48);
|
||||
|
||||
translate([0, 0, xy[1]])
|
||||
linear_extrude(thickness)
|
||||
hollow_out(shell_thickness = thickness)
|
||||
circle(r, $fn = 48);
|
||||
}
|
||||
circle(r);
|
@@ -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(
|
||||
|
Reference in New Issue
Block a user