From 21d5638e942e6e49d7b6392076e9d63ce5217de0 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Tue, 5 Apr 2022 11:40:16 +0800 Subject: [PATCH] add rands_sphere, rands_disk --- test/util/rands_disk.scad | 25 +++++++++++++++++++++++++ test/util/rands_sphere.scad | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 test/util/rands_disk.scad create mode 100644 test/util/rands_sphere.scad diff --git a/test/util/rands_disk.scad b/test/util/rands_disk.scad new file mode 100644 index 00000000..2e014cda --- /dev/null +++ b/test/util/rands_disk.scad @@ -0,0 +1,25 @@ +function rands_disk(radius, value_count, seed = undef) = + let( + seed_undef = is_undef(seed), + theta = seed_undef ? rands(0, 360, value_count * 2) : rands(0, 360, value_count * 2, seed), + k = seed_undef ? rands(0, 1, value_count * 2) : rands(0, 1, value_count * 2, seed) + + ) + [ + for(i = [0:value_count - 1]) + let(r = sqrt(k[i]) * radius) + [cos(theta[i]), sin(theta[i])] * r + ]; + +/* +number = 10000; +radius = 2; + +points = rand_pts_circle(radius, number); + +for(p = points) { + translate(p) + circle(.01); +} + +%circle(radius, $fn = 96);*/ \ No newline at end of file diff --git a/test/util/rands_sphere.scad b/test/util/rands_sphere.scad new file mode 100644 index 00000000..11cbb19e --- /dev/null +++ b/test/util/rands_sphere.scad @@ -0,0 +1,37 @@ +use ; + +function rands_sphere(radius, value_count, seed = undef) = + let(r_nums = is_undef(seed) ? rands(0, 1, value_count * 2) : rands(0, 1, value_count * 2, seed)) + [ + for(i = [0:value_count - 1]) + let( + theta = degrees(2 * PI * r_nums[i]), + phi = acos(r_nums[i + value_count] * 2 - 1), + sin_phi = sin(phi), + x = sin_phi * cos(theta), + y = sin_phi * sin(theta), + z = cos(phi) + ) + [x, y, z] + ] * radius; + +/* + +use ; +use ; + +number = 20; +radius = 2; + +points = rand_pts_sphere(radius, number); + +polyhedron_hull(points); + +for(p = points) { + translate(p) + sphere(.05); +} + +%sphere(radius, $fn = 48); + +*/ \ No newline at end of file