How can I create sphere in this code?
이전 댓글 표시
X=(0:0.02:1);
Y=(0:0.02:1);
[x,y] = meshgrid(X,Y);
%z=rand(size(x))*1000;
%z=1e6*ones(size(x));
z=zeros(size(x));
x0=mean(X);
y0=mean(Y);
b_center1 = [0.2,0.8];
b_center2 = [0.6,0.2];
b_center3 = [0.4,0.55];
r1=0.1*max(X);
r2=0.1*max(X);
r3=0.1*max(X);
for i=1:length(X)
for j=1:length(Y)
dist = pdist([[X(i),Y(j)];b_center1]);
if (dist < r1)
z(i,j)=1;
end
dist = pdist([[X(i),Y(j)];b_center2]);
if (dist < r2)
z(i,j)=1;
end
dist = pdist([[X(i),Y(j)];b_center3]);
if (dist < r3)
z(i,j)=1;
end
end
end
surf(x,y,z)
답변 (1개)
Ayush Gupta
2020년 9월 10일
The above code is not able to generate spheres primarily because in the workflow any point that is less than r1 is given a true value by giving it 1 whereas for a sphere the points should be all equidistant from a center. To plot a sphere with center at (a,b,c) and radius r, refer to the following code:
[x,y,z] = sphere;
x = x*r;
y = y*r;
z = z*r;
figure
surf(x+a,y+b,z+c)
카테고리
도움말 센터 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!