From 8fbb00c7db031798ee493b056f20789d22d13dd0 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 22 Feb 2020 15:45:18 +0800 Subject: [PATCH] add voronoi_cells --- .../_impl/_voronoi_cells_impl.scad | 25 +++++++++++++++++++ src/experimental/voronoi_cells.scad | 16 ++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/experimental/_impl/_voronoi_cells_impl.scad create mode 100644 src/experimental/voronoi_cells.scad diff --git a/src/experimental/_impl/_voronoi_cells_impl.scad b/src/experimental/_impl/_voronoi_cells_impl.scad new file mode 100644 index 00000000..20f6254d --- /dev/null +++ b/src/experimental/_impl/_voronoi_cells_impl.scad @@ -0,0 +1,25 @@ + +use ; + +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]); + +function _cells_lt_before_intersection(shape, size, points, pt) = + let(half_region_size = 0.5 * size) + [ + for(p = points) + if(pt != p) + let( + v = p - pt, + offset = (pt + p) / 2 - v / norm(v) * half_region_size, + a = atan2(v[1], v[0]) + ) + [ + for(sp = shape) + rotate_p(sp, a) + offset + ] + ]; \ No newline at end of file diff --git a/src/experimental/voronoi_cells.scad b/src/experimental/voronoi_cells.scad new file mode 100644 index 00000000..5a726c43 --- /dev/null +++ b/src/experimental/voronoi_cells.scad @@ -0,0 +1,16 @@ +use ; +use ; + +function voronoi_cells(points, region_shape) = + let( + size = _default_region_size(points), + shape = is_undef(region_shape) ? shape_square(size) : region_shape, + regions_lt = [ + for(p = points) + _cells_lt_before_intersection(shape, size, points, p) + ] + ) + [ + for(regions = regions_lt) + convex_intersection_for(regions) + ]; \ No newline at end of file