Added all remaining gists.

This commit is contained in:
Miguel Astor
2023-06-21 21:40:51 -04:00
parent 22ff5bfa25
commit b0ca706a25
26 changed files with 892 additions and 0 deletions

1
samplings.m/README.md Normal file
View File

@@ -0,0 +1 @@
Based on explanations from scrathapixel.com, stackoverflow.com and Wolfram Mathworld.

46
samplings.m/samplings.m Normal file
View File

@@ -0,0 +1,46 @@
function [nt, nb] = coord_system(n)
if abs(n(1)) > abs(n(2))
nt = [n(3), 0, -n(1)] / norm([n(3), 0, -n(1)]);
else
nt = [0, -n(3), n(2)] / norm([0, -n(3), n(2)]);
endif
v = cross(n, nt);
nb = v / norm(v);
endfunction
function [x, y, z] = disk_sampling(c, n, r)
rr = rand() * r;
[nt, nb] = coord_system(n);
theta = rand() * 2 * pi;
x = c(1) + (rr * cos(theta) * nt(1)) + (rr * sin(theta) * nb(1));
y = c(2) + (rr * cos(theta) * nt(2)) + (rr * sin(theta) * nb(2));
z = c(3) + (rr * cos(theta) * nt(3)) + (rr * sin(theta) * nb(3));
endfunction
function [x, y, z] = sphere_sampling(c, r)
theta = rand() * 2 * pi;
u = (rand() * 2) - 1;
sqrt1muu = sqrt(1 - (u * u));
x = (r * sqrt1muu * cos(theta)) + c(1);
y = (r * sqrt1muu * sin(theta)) + c(2);
z = (r * u) + c(3);
endfunction
function test_sampling()
x = [];
y = [];
z = [];
for i = 1:5000
%[sx, sy, sz] = disk_sampling([0, 0.75, -1], [0, -1, 0], 0.15);
[sx, sy, sz] = sphere_sampling([0, 0.75, -1], 0.15);
x(end + 1) = sx;
y(end + 1) = sy;
z(end + 1) = sz;
endfor
scatter3(x, y, z);
axis([-2, 2, -2, 2, -2, 2]);
xlabel('x');
ylabel('y');
zlabel('z');
endfunction