Created
July 28, 2015 04:31
-
-
Save yakir12/3c5c83bf70ea50fca975 to your computer and use it in GitHub Desktop.
Random 3D points uniformly distributed on an ellipse shaped window of a sphere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
close all | |
clear all | |
clc | |
%% | |
n = 1000; | |
z = rand(n,1)*2-1; | |
t = rand(n,1)*2*pi; | |
x = sqrt(1. - z.*z).*cos(t); | |
y = sqrt(1. - z.*z).*sin(t); | |
figure('color','w') | |
scatter3(x,y,z,'ok','markerfacecolor','w') | |
hold on | |
[x,y,z] = sphere(50); | |
p = surf(x,y,z); | |
set(p,'facecolor','red','edgecolor','none','facealpha',.5); | |
daspect([1 1 1]); | |
view(3); axis tight; grid on; | |
camlight; lighting gouraud; | |
axis equal | |
axis off | |
export_fig('sphere.png') | |
%% | |
close all | |
clear all | |
clc | |
n = 1000; | |
a = .5; | |
cosa = cos(a); | |
z = rand(n,1)*(cosa - 1.) - cosa; | |
t = rand(n,1)*2*pi; | |
x = sqrt(1. - z.*z).*cos(t); | |
y = sqrt(1. - z.*z).*sin(t); | |
l = 1.2; | |
nn = 100; | |
[d,t] = meshgrid(linspace(0,l,nn),linspace(0,2*pi,nn)); | |
r = repmat(linspace(0,tan(a)*l,nn),nn,1); | |
xx = r.*cos(t); | |
yy = r.*sin(t); | |
zz = -d; | |
figure('color','w') | |
p = surf(xx,yy,zz); | |
set(p,'facecolor','green','edgecolor','none','facealpha',.25); | |
hold on | |
plot3(xx(:,end),yy(:,end),zz(:,end),'k','linewidth',3) | |
scatter3(x,y,z,'ok','markerfacecolor','w') | |
[x,y,z] = sphere(50); | |
p = surf(x,y,z); | |
set(p,'facecolor','red','edgecolor','none','facealpha',.5); | |
daspect([1 1 1]); | |
view(3); axis tight; grid on; | |
view(0,15) | |
camlight; lighting gouraud; | |
axis equal | |
axis off | |
export_fig('circle1.png') | |
view(0,90) | |
export_fig('circle2.png') | |
%% | |
close all | |
clear all | |
clc | |
nn = 100; | |
r = .8; | |
dis = 2; | |
Z = -1.3; | |
t = linspace(0,2*pi,nn); | |
x = r*cos(t); | |
y = dis+r*sin(t); | |
z = Z*ones(1,nn); | |
figure('color','w') | |
plot3(x,y,z,'k','linewidth',3) | |
hold on | |
p = surf([zeros(1,nn);x],[zeros(1,nn);y],[zeros(1,nn);z]); | |
set(p,'facecolor','green','edgecolor','none','facealpha',.25); | |
inx = 0; | |
while inx < 200 | |
z = rand()*2-1; | |
t = rand()*2*pi; | |
xyz = [sqrt(1. - z.*z).*cos(t);sqrt(1. - z.*z).*sin(t);z]; | |
xyz = xyz/norm(xyz); | |
d = Z/xyz(3); | |
p2 = d*xyz; | |
if inpolygon(p2(1),p2(2),x,y) && d > 0 | |
inx = inx+1; | |
% plot3([0,p2(1)],[0,p2(2)],[0,p2(3)]) | |
scatter3(xyz(1),xyz(2),xyz(3),'ok','markerfacecolor','w') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment