1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-23 23:03:23 +02:00

add voronoi_cells

This commit is contained in:
Justin Lin
2020-02-22 15:45:18 +08:00
parent f0d77c242f
commit 8fbb00c7db
2 changed files with 41 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
use <rotate_p.scad>;
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
]
];

View File

@@ -0,0 +1,16 @@
use <experimental/_impl/_voronoi_cells_impl.scad>;
use <experimental/convex_intersection_for.scad>;
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)
];