plot spheres in 3D with one color (no gradient)

조회 수: 62 (최근 30일)
Phong Pham
Phong Pham 2016년 4월 5일
답변: Mike Garrity 2016년 4월 5일
Hi everyone
I want to plot spheres in 3 D with one color (no gradient). I used surf function as shown below, where a is radius, XT,YT,ZT are the center position in x,y,z and N is number of spheres.
for k=1:1:1%size(XT,1)
for j=1:N
% Generate a sphere consisting of 20by 20 faces
[x,y,z]=sphere;
% use surf function to plot
hSurface=surf(a*x+XT(k,j),a*y+YT(k,j),a*z+ZT(k,j)); hold on
set(hSurface,'FaceColor',[0 0 1], 'FaceAlpha',0.5,'EdgeAlpha', 0);
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
daspect([1 1 1]);
W(k)=getframe(gcf);
end
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
end
movie2avi(W,'35_percent.avi');
Without using 'EdgeAlpha', the spheres look right in 3D but it is visible the point to point edges due to surf function. However, if I include 'EdgeAlpha' to make invisible point to point edges, but the spheres are now in Circle (2D).
How do I plot spheres in 3D and in one color without seeing the point-point edges of plotted surface.
Thanks

채택된 답변

Mike Garrity
Mike Garrity 2016년 4월 5일
A solid FaceColor alone isn't going to give you any feeling of the 3D shape of the spheres. If you take the edges away, you'll need to use something else to show the shape.
One option is lighting:
N = 15;
a = 1/8;
XT = randn(1,N)/6;
YT = randn(1,N)/6;
ZT = randn(1,N)/6;
for j=1:N
% Generate a sphere consisting of 20by 20 faces
[x,y,z]=sphere;
% use surf function to plot
hSurface=surf(a*x+XT(j),a*y+YT(j),a*z+ZT(j));
hold on
set(hSurface,'FaceColor',[0 0 1], ...
'FaceAlpha',0.5,'FaceLighting','gouraud','EdgeColor','none')
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
daspect([1 1 1]);
end
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
camlight
Another option would be to use FaceColor to shade them:
N = 15;
a = 1/8;
XT = randn(1,N)/6;
YT = randn(1,N)/6;
ZT = randn(1,N)/6;
[x,y,z]=sphere;
l = [.25 -.433 .5];
c = max(0,x*l(1) + y*l(2) + z*l(3));
for j=1:N
% use surf function to plot
hSurface=surf(a*x+XT(j),a*y+YT(j),a*z+ZT(j),c);
hold on
set(hSurface,'FaceColor','interp','FaceAlpha',0.5,'EdgeColor','none')
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
daspect([1 1 1]);
end
hold off
xlabel('X')
ylabel('Y')
zlabel('Z')
colormap hot

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by