FROM:
http://www.rit.edu/~pnveme/pigf/ThreeDGraphics/thrd_index.html
不同形状的矩形
The rectangle is a graphic object that is very useful to represent simple animation through simple graphics. A moving rectangle and text is shown in the following code. Simple animation like these are only restricted by your imagination
% 3D Graphics - Graphic Object - rectangle
% Dr. P.Venkataraman
format compact
set(gcf,'Menubar','none','Name','Different Types of Rectangles', ...
'NumberTitle','off','Position',[10,600,400,300]);
axis([0 8 0 6]);
t = 0:0.1:1;
for i = 1:10
if i > 1
delete(h1);
end
left = 2*t(i);
bot = exp(0.5*t(i));
wid = 3*sin(t(i))+ 1;
ht = 3*(t(i)*t(i)+0.1);
col = rand(1,3);
ecolor = rand(1,3);
h = rectangle('Position',[left bot wid ht], ...
'FaceColor',col, ...
'EdgeColor',ecolor, ...
'LineWidth',1, ...
'Curvature',[1*rand(1,1) 1*rand(1,1)]);
h1 = text((left+wid+0.1),(bot +ht +0.1),'Top right edge');
pause(0.5);
end
MATLAB provides a command for generating a spherical surface through the sphere function. To be able to control the appearance it is necessary to create the sphere as a surface. The spheres are unit spheres. In the following three spheres with different properties are drawn using three different axes.
% 3D Graphics: Sphere
% Dr. P.Venkataraman
format compact
set(gcf,'Menubar','none','Name','Spheres', ...
'NumberTitle','off','Position',[10,350,400,300], ...
'Color',[0.2 0.3 0.4]);
% first sphere
h(1) = axes('Position',[0 0 1 1]);
[Xs Ys Zs]=sphere(30); % create data for sphere surface
hs1 = surf(Xs, Ys, Zs); % create sphere
set(hs1,'EdgeColor','none', ...
'FaceColor','red', ...
'FaceAlpha','interp');
alpha('color');
alphamap('rampdown');
camlight(45,45);
lighting phong
hidden off
axis square
% second
h(2) = axes('Position',[0.1 0.1 0.5 0.5]);
[Xs Ys Zs]=sphere(20);
hs2 = surf(Xs, Ys, Zs);
set(hs2,'EdgeColor',[0.5 0.5 0.5], ...
'FaceColor','interp', ...
'FaceAlpha','interp');
alpha('color');
alphamap('rampdown');
camlight right;
lighting phong
hidden off
axis equal
% third
h(3)= axes('Position',[0.6 0.6 0.3 0.3]);
[Xs Ys Zs]=sphere(30);
hs3 = surf(Xs, Ys, Zs);
set(hs3,'EdgeColor','none', ...
'FaceColor','y', ...
'FaceLighting','phong', ...
'AmbientStrength',0.3, ...
'DiffuseStrength',0.8, ...
'SpecularStrength',0.9, ...
'SpecularExponent',25, ...
'BackFaceLighting','lit');
camlight left;
hidden off
set(h,'Visible','off')
axis square
3D Graphics : Cylinders
MATLAB provides a command for generating a cylindrical surface through the cylinder function. To be able to control the appearance it is necessary to create the cylinder as a surface. The spheres are unit cylinders. In the following three cylinders with different properties are drawn using three different axes. One of the cylinders is rotated
% 3D Graphics: Cylider
% Dr. P.Venkataraman
format compact
set(gcf,'Menubar','none','Name','Cylinders', ...
'NumberTitle','off','Position',[10,350,400,300], ...
'Color',[0.1 0.5 0.3]);
% first cylinder
h(1) = axes('Position',[0 0 1 1]);
[Xs Ys Zs]=cylinder(30);
hs1 = surf(Xs, Ys, Zs);
set(hs1,'EdgeColor','none', ...
'FaceColor','red', ...
'FaceAlpha','interp');
alpha('color');
alphamap('rampdown');
camlight(45,45);
lighting phong
hidden off
axis square
% second
h(2) = axes('Position',[0.1 0.1 0.5 0.5]);
[Xs Ys Zs]=cylinder([1 0.5 1],20);
hs2 = surf(Xs, Ys, Zs);
set(hs2,'EdgeColor',[0.5 0.5 0.5], ...
'FaceColor','interp', ...
'FaceAlpha','interp');
alpha('color');
alphamap('rampdown');
camlight right;
lighting phong
hidden off
axis equal
rotate(hs2,[1 0 0],45); % cylinder is rotated
% third
h(3)= axes('Position',[0.6 0.6 0.3 0.3]);
[Xs Ys Zs]=cylinder([0.5 0.6 0.6 0.8 1.0 0.3],30);
hs3 = surf(Xs, Ys, Zs);
set(hs3,'EdgeColor','none', ...
'FaceColor','y', ...
'FaceLighting','phong', ...
'AmbientStrength',0.3, ...
'DiffuseStrength',0.8, ...
'SpecularStrength',0.9, ...
'SpecularExponent',25, ...
'BackFaceLighting','lit');
camlight left;
hidden off
set(h,'Visible','off')
axis square
3D Graphics : CubeMATLAB generates a cube using the patch function. The patch function requires a Vertex matrix and a Face matrix. The Vertex matrix contains the points in 3D space that are to be patched into faces which will yield the cube. . The Face matrix contains rows of list of vertices that will define a face. For a cube there are 6 faces that need to be listed. A cylinder and sphere from previous exercise is included.
% 3D Graphics: Cube
% Dr. P.Venkataraman
format compact
set(gcf,'Menubar','none','Name','Cube', ...
'NumberTitle','off','Position',[10,350,300,200], ...
'Color',[0.3 0.1 0.3]);
% the cube
h(1) = axes('Position',[0.2 0.2 0.6 0.6]);
vert = [1 1 1; 1 2 1; 2 2 1; 2 1 1 ; ...
1 1 2;1 2 2; 2 2 2;2 1 2];
fac = [1 2 3 4; ...
2 6 7 3; ...
4 3 7 8; ...
1 5 8 4; ...
1 2 6 5; ...
5 6 7 8];
patch('Faces',fac,'Vertices',vert,'FaceColor','r'); % patch function
light('Position',[1 3 2]);
light('Position',[-3 -1 3]);
material shiny;
alpha('color');
alphamap('rampdown');
camlight(45,45);
lighting phong
view(30,30);
% cylinder
h(2) = axes('Position',[0.1 0.1 0.5 0.5]);
[Xs Ys Zs]=cylinder([1 0.5 1],20);
hs2 = surf(Xs, Ys, Zs);
set(hs2,'EdgeColor',[0.5 0.5 0.5], ...
'FaceColor','interp', ...
'FaceAlpha','interp');
alpha('color');
alphamap('rampdown');
alpha(0.3);
camlight right;
lighting phong
hidden off
axis equal
rotate(hs2,[1 0 0],45); % cylinder is rotated
% sphere
h(3)= axes('Position',[0.17 0.17 0.4 0.4]);
[Xs Ys Zs]=sphere(30);
hs3 = surf(Xs, Ys, Zs);
set(hs3,'EdgeColor','none', ...
'FaceColor','y', ...
'FaceLighting','phong', ...
'AmbientStrength',0.3, ...
'DiffuseStrength',0.8, ...
'SpecularStrength',0.9, ...
'SpecularExponent',25, ...
'BackFaceLighting','lit');
camlight left;
hidden off
set(h,'Visible','off')
axis square
3D Graphics : SphereMATLAB allows handling images by associating an image with a matrix (or matrices). In the following a truecolor (RGB -8 bit) image is read into MATLAB to serve as a background. The cube, cylinder and sphere are then drawn on this background. (to run the following you need an image)
% 3D Graphics: Cube
% Dr. P.Venkataraman
format compact
set(gcf,'Menubar','none','Name','Image', ...
'NumberTitle','off','Position',[10,350,300,200]);
% the image - jpeg file
h(4) = axes('Position',[0 0 1 1]); %-stretch to fill figure
A = imread('photomech_1.JPG','jpeg'); % read image file
hi1 =image(A); % draw image on figure -
% cube
h(1) = axes('Position',[0.5 0.5 0.3 0.3]);
vert = [1 1 1; 1 2 1; 2 2 1; 2 1 1 ; ...
1 1 2;1 2 2; 2 2 2;2 1 2];
fac = [1 2 3 4; ...
2 6 7 3; ...
4 3 7 8; ...
1 5 8 4; ...
1 2 6 5; ...
5 6 7 8];
patch('Faces',fac,'Vertices',vert,'FaceColor','r'); % draw cube
light('Position',[1 3 2]);
light('Position',[-3 -1 3]);
alpha(0.1) % set very trasparent
view(30,30); % rotate view
% cylinder
h(2) = axes('Position',[0.1 0.1 0.5 0.5]);
[Xs Ys Zs]=cylinder([1 0.5 1],20);
hs2 = surf(Xs, Ys, Zs);
set(hs2,'EdgeColor',[0.5 0.5 0.5], ...
'FaceColor','b', ...
'FaceAlpha','interp');
alpha('color');
alphamap('rampdown');
alpha(0.3);
camlight right;
lighting phong
hidden off
axis equal
rotate(hs2,[1 0 0],45); % cylinder is rotated
% sphere
h(3)= axes('Position',[0.17 0.17 0.4 0.4]);
[Xs Ys Zs]=sphere(30);
hs3 = surf(Xs, Ys, Zs);
set(hs3,'EdgeColor','none', ...
'FaceColor','y', ...
'FaceLighting','phong', ...
'AmbientStrength',0.3, ...
'DiffuseStrength',0.8, ...
'SpecularStrength',0.9, ...
'SpecularExponent',25, ...
'BackFaceLighting','lit');
camlight left;
hidden off
set(h,'Visible','off')
axis square
This is used to draw a curve in 3D space. This is very useful for drawing the motion or the trajectory of a particle in space. In the example the particle motion is plotted (animation). Its distance from the horizontal reference is marked at each point and the velocity vectors at those points is drawn
% 3D Graphics - line plot
% Dr. P.Venkataraman
% The path traced by a particle in 3D space
format compact
set(gcf,'Menubar','none','Name','3D Line Plot - Path and Velocity', ...
'NumberTitle','off','Position',[10,350,400,300]);
t = 0:0.1:3;
x = 2*sin(2*t);
y = 3*cos(2*t);
z = t;
h1 = plot3(x,y,z, ...
'LineWidth',2, ...
'Color','k');
% animation -----
axis([-3 3 -5 6 0 4]);
hold on
for i=1:length(t)
if i> 1
delete(h2); % deletes the marker
delete(h3); % delete the line
end
% creats a filled patch of yellow color
h2 = plot3(x(i),y(i),z(i),'bo','MarkerFaceColor','r'); % creates a marker
h3 = line([x(i) x(i)],[y(i) y(i)],[0 z(i)],'Color','r');
pause(0.1) % pauses the plot or you can't see the animation
end
delete(h3);
%-------------------------------------
% title in bold face italic in black color
title('/bf/it3D Line Plot and Velocity Vector','Color','k', ...
'VerticalAlignment','bottom')
ylabel('y', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
xlabel('x', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
zlabel('z', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
quiver3(x,y,z,4*cos(4*t),-6*sin(t),1,2,'b')
hold off
textstr(1)={'x = 2Sin(t)'};
textstr(2)={'y = 3Cos(t)'};
textstr(3)={'z = t'};
text(1.5,4,2,textstr,'FontWeight','b')
grid
3D Graphics: Visualization OptionsMATLAB has evolved into a powerful visualization tool. Some of the features are shown in the following exercise. It is designed such that the title represents the actual command. Each action invokes the pause command so that you have to press any key to move forward. Please move the intial figure window to a portion that is not overlapping the command window
MATLAB Code
% 3D Graphics - A tour of MATLAB's Powerful
% Visualiztion tools
% Dr. P.Venkataraman
% Please make sure the figure window and the Command Window
% do not overlap and hit any key to continue
% the tile list the significant command used
% -----plot data
x = -4:0.1:4;
y = -4:0.1:4;
[X Y] = meshgrid(x,y);
R = sqrt(X.*X + Y.*Y);
Z = sin(2*R)./(R + 0.001);
%---------------------------
set(gcf,'Menubar','none','Name','3D Visualization Features', ...
'NumberTitle','off','Position',[10 350 450 350], ...
'Color',[1 1 1]);% evalstra is a cell array
% eval function will evaluate whatever is in the stringevalstra(1) = {'h = surf(x,y,Z);'};
evalstra(2)={'colormap hot;'};
evalstra(3)= {'shading interp;';}
evalstra(4) = {'set(h,''EdgeColor'',''w'');'};
evalstra(5) = {'light(''Position'',[-2,2,20]);'};
evalstra(6) = {'lighting phong;'};
evalstra(7) = {'set(h,''EdgeColor'',''b'');'};
evalstra(8) = {'shading interp;'};
evalstra(9) = {'material([0.4,0.6,0.5,30]);'};
evalstra(10) = {'set(h,''FaceColor'',[0.7 0.7 0],''BackFaceLighting'',''lit'');'};
evalstra(11) = {'view([30,25]);'};
evalstra(12) = {'daspect([2 2 1]);'};
evalstra(13) = {'camlight left;'};
evalstra(14) = {'camorbit(30,0),drawnow,camlight left'};for i = 1:length(evalstra)
evalstr = char(evalstra(i));
eval(evalstr);
evalstr1 = strrep(evalstr,';','');
title(evalstr1,'Color','k', ...
'VerticalAlignment','bottom');
fprintf('strike any key to continue - title contains the command/n');
pause
endtitle('label axes','Color','k', ...
'VerticalAlignment','bottom');
ylabel('y', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
xlabel('x', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
zlabel('z', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
fprintf('strike any key to continue - title contains the command/n');
pausetitle('Change lightt characteristics','Color','k', ...
'VerticalAlignment','bottom');set(findobj(gca,'type','surface'),...
'FaceLighting','phong',...
'AmbientStrength',.3,'DiffuseStrength',.5,...
'SpecularStrength',.9,'SpecularExponent',25,...
'BackFaceLighting','unlit')
3D Graphics: QuiverIn this example the 3D quiver plot is used to draw the surface normal - an important piece of information in fluid mechanics. It is used in conjunction with the surfnorm function - that which creates the surface normal. In the following the surface is also shown. A zoomed image is also shown to see the normal
此主题相关图片如下:
% 3D graphics - Using Quiver to draw normal to surface
% Dr. P.Venkataramanformat compact
set(gcf,'Menubar','none','Name','Surface normal', ...
'NumberTitle','off','Position',[10,350,350,300], ...
'Color',[1 1 1]);
%*************plot information
x = -4:0.2:4;
y = -4:0.2:4;
[X Y] = meshgrid(x,y);
R = sqrt(X.*X + Y.*Y);
Z = sin(2*R)./(R + 0.001);
%********************************************
[U,V,W] = surfnorm(X,Y,Z); % create surface normal
h1 = quiver3(X,Y,Z,U,V,W);
hold on;
h = surf(X,Y,Z);
shading interp
hold off
grid
ylabel('y', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
xlabel('x', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
zlabel('z', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
title('/bf/itSurface Normals','Color','k', ...
'VerticalAlignment','top');3D Graphics: Function of Three Variables
One of the strengths of the MATLAB graphic engine is its ability and easy way of displaying function of three variables. In mechanical engineering, specially in fluid mechanics the velocity, pressure, temperature, density are functions of the three space coordinates for a three dimensional problem. Since we have only three dimensions to display the information, we can only display function values in certain slices of the volume.
In the following example, the same hat function is used in 3D. There will be a significant value for the property at the origin decreasing outward in a wavy manner. This can be the model for the density of the earth.
Two plots are shown with different slices. The second reflects different transparency properties. The code included will produce both plots. The transparency properties are adjusted by controlling the alphamap values% 3D graphics - Function of three variables
% Dr. P.Venkataraman
format compact
set(gcf,'Menubar','none','Name','Three Variables', ...
'NumberTitle','off','Position',[10,350,700,300], ...
'Color',[1 1 1]);
x = -4:0.1:4;
y = -4:0.1:4;
z = -4:0.1:4;
[X Y Z] = meshgrid(x,y,z);
R = sqrt(X.*X + Y.*Y + Z.*Z);
p = sin(2*R)./(R + 0.001);
subplot('Position',[0.1 0.1 0.35 0.7]);
h = slice(X,Y,Z,p,[-4 0 3],[0],[]);
set(h,'EdgeColor','none');
ylabel('y', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
xlabel('x', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
zlabel('z', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
title('/bf/it3 Variables - Silces of Volume','Color','k', ...
'VerticalAlignment','bottom');
subplot('Position',[0.5 0.1 0.35 0.7]);
h = slice(X,Y,Z,p,[-2 1],[],[0]);
set(h,'EdgeColor','none','FaceColor','interp',...
'FaceAlpha','interp')
%Set the alpha data equal to the color data,
% increase each value in the alphamap by .3 to achieve the
%desired degree of transparency.
alpha('color')
alphamap('increase',.3)
ylabel('y', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
xlabel('x', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
zlabel('z', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
title('/bf/it3 Variables - Manipulating alphamap','Color','k', ...
'VerticalAlignment','bottom');
axis([-4 4 -4 4 -4 4]);
colorbar
% colormap(hsv) % the figure looks better with this colormap
3D Graphics: Function of Three Variables - Curved SlicesIn the previous illustration the slices were on planes which is what is usually encountered. How about a curved slice -i.e. a plot the values of the function of three variables on a surface you specify ? Wow!!.
Below is an illustration of the 3D "Hat" function plotted on the 2D "Hat" surface which is shifted along +x to remove some symmetry. Note the function being plotted and the surface have different variable limits
% 3D graphics - Curved Slices - 3 Variables
% Dr. P.Venkataraman
format compact
set(gcf,'Menubar','none','Name','Three Variables - Curved Slices', ...
'NumberTitle','off','Position',[10,350,400,300], ...
'Color',[1 1 1]);
% this is the function of three variables
% note the variable limits
x = -2:0.1:2;
y = -2:0.1:2;
z = -2:0.1:2;
[X Y Z] = meshgrid(x,y,z);
R = sqrt(X.*X + Y.*Y + Z.*Z);
p = sin(2*R)./(R + 0.001);
% this is the surface on which the function will be displayed
% shifted hat function
x1 = -4:0.2:4;
y1= -4:0.2:4;
[X1 Y1] = meshgrid(x1,y1);
R1 = sqrt((X1-1).*(X1-1)+ Y1.*Y1);
Z1 = sin(2*R1)./(R1 + 0.001);
% display the slice
h1 = slice(X,Y,Z,p,X1,Y1,Z1);
set(h1,'FaceColor','interp','EdgeColor',[0.8 0.8 0.8], ...
'EdgeAlpha',0.5);
axis([-4 4 -4 4 -4 4]);
ylabel('y', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
xlabel('x', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
zlabel('z', ...
'FontWeight','b','Color','b', ...
'VerticalAlignment','bottom'); % boldface blue
title('/bf/itCurved Slices - 3 Variables','Color','k', ...
'VerticalAlignment','bottom');