clc; clear all; close all; R = 3; time = 10000; origin = [0,0,0]; %%======绘制球体====== t=linspace(0,pi,25); p=linspace(0,2*pi,25); [theta,phi]=meshgrid(t,p); x=R * sin(theta).*sin(phi) + origin(1); y=R *sin(theta).*cos(phi) + origin(2); z=R *cos(theta) + origin(3); surf(x,y,z); alpha(0.9) hold on xlabel('x') ylabel('y') zlabel('z') axis equal; %%============绘制正方体================ x=([0 1 1 0 0 0;1 1 0 0 1 1;1 1 0 0 1 1;0 1 1 0 0 0]-0.5)*2 * R+origin(1); y=([0 0 1 1 0 0;0 1 1 0 0 0;0 1 1 0 1 1;0 0 1 1 1 1]-0.5)*2 * R+origin(2); z=([0 0 0 0 0 1;0 0 0 0 0 1;1 1 1 1 0 1;1 1 1 1 0 1]-0.5)*2 * R+origin(3); surf(x,y,z); alpha(0.1) %%============================ XYZ = unifrnd(-R, R, 3, time);%随机生成正方体内的随机数; D = pdist([origin;XYZ'],'euclidean'); D = D(1:size(XYZ,2)); Ctime =length(D(D<R)); for ii = 1: time if D(ii) < R plot3(XYZ(1,ii),XYZ(2,ii),XYZ(3,ii),'r*') else plot3(XYZ(1,ii),XYZ(2,ii),XYZ(3,ii),'b*') end hold on % pause(0.1) end % plot3(XYZ(1,Aindex),XYZ(2,Aindex),XYZ(3,Aindex),'b*') text(2,2,4.2,'pi') disp(length(D(D<R))/time * 6)