mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-24 23:25:51 +02:00
add voronoi_cells
This commit is contained in:
25
src/experimental/_impl/_voronoi_cells_impl.scad
Normal file
25
src/experimental/_impl/_voronoi_cells_impl.scad
Normal 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
|
||||||
|
]
|
||||||
|
];
|
16
src/experimental/voronoi_cells.scad
Normal file
16
src/experimental/voronoi_cells.scad
Normal 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)
|
||||||
|
];
|
Reference in New Issue
Block a user