Added all remaining gists.
This commit is contained in:
1
samplings.m/README.md
Normal file
1
samplings.m/README.md
Normal file
@@ -0,0 +1 @@
|
||||
Based on explanations from scrathapixel.com, stackoverflow.com and Wolfram Mathworld.
|
46
samplings.m/samplings.m
Normal file
46
samplings.m/samplings.m
Normal 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
|
Reference in New Issue
Block a user