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

add nz_voronoi2

This commit is contained in:
Justin Lin
2020-03-29 16:57:04 +08:00
parent 9d2b4d09ef
commit 4a26673b75
3 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
use <experimental/_impl/_nz_worley_comm.scad>;
use <util/sort.scad>;
function _neighbors(fcord, seed, dim, m, n) = [
for(y = [-1:1])
for(x = [-1:1])
let(
nx = fcord[0] + x,
ny = fcord[1] + y,
sd_base = nx + ny * dim,
sd1 = _lookup_noise_table(seed + sd_base),
sd2 = _lookup_noise_table(sd1 * 255 + sd_base),
nbr = [(nx + sd1) * m, (ny + sd2) * n]
)
nbr
];
function _nz_voronoi2(p, seed, dim, m, n) =
let(
fcord = [floor(p[0] / m), floor(p[1] / n)],
nbrs = _neighbors(fcord, seed, dim, m, n),
dists = [
for(nbr = nbrs)
if(!is_undef(nbr[1])) // Here's a workaround for a weired undef problem. bug of 2019.05?
[nbr[0], nbr[1], norm(nbr - p)]
],
sorted = sort(dists, by = "z"),
a = [sorted[0][0], sorted[0][1]],
b = [sorted[1][0], sorted[1][1]],
m = (a + b) / 2
)
(p - m) * (a - m);

View File

@@ -0,0 +1,19 @@
use <experimental/nz_voronoi2.scad>;
size = [100, 50];
dim = 5;
seed = 5;
points = [
for(y = [0:size[1] - 1])
for(x = [0:size[0] - 1])
[x, y, nz_voronoi2(size, x, y, seed, dim)]
];
max_dist = max([for(p = points) p[2]]);
for(p = points) {
c = p[2] / max_dist;
color([c, c, c])
translate([p[0], p[1]])
square(1);
}

View File

@@ -0,0 +1,11 @@
use <util/rand.scad>;
use <experimental/_impl/_nz_voronoi2_impl.scad>;
function nz_voronoi2(size, x, y, seed, dim = 2) =
let(
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256),
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim
)
_nz_voronoi2([x, y], sd, dim, m, n);