From:
http://www.rit.edu/~pnveme/pigf/threedgraphics/thrd_index.html
Rectangle of different shapes
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
The related pictures of this topic are as follows:
% 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', Color, ... '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '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.
The related pictures of this topic are as follows: Matlab Code
% 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 ', ...' ', ...' 'DiffusestRength', 0.8, ... 'specularstregth', 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 ROTED This topic is as follows:
% 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 Offset (H, 'Visible', 'Off') AXIS Square
3D Graphics: Cube
MATLAB 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. a cylinder and sphere from previous excercise is incro.
The related pictures of this topic are as follows:
% 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]); 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); 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 ', ...' ', ...' 'DiffusestRength', 0.8, ... 'specularstregth', 0.9, ... 'specularExponent', 25, ... 'backfacelighting', ' LIT '); camlight left; hidden OFF
Set (H, 'Visible', 'Off') Axis Square
3D Graphics: Sphere
MATLAB 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)
The related pictures of this topic are as follows: Matlab Code
% 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 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 '); AlphaMap (' Rambdown '); 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 ', ...' ', ...' 'DiffusestRength', 0.8, ... 'specularstregth', 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 This topic is as follows:
% 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 yelow color h2 = plot3 (x (i), y (i), z (i), 'bo', 'markerfacecolor', 'R' );% Creates a marker h3 = line ([x (i) x (i)], [Y) 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 ', ...' 'Vertical_Mene,' bottom ' ) Ylabel ('y', ... 'fontweight', 'b', 'color', 'b', ... 'verticalterialignment', 'bottom');% boldface blue xlabel ('x', ... 'Fontweight', 'b', 'color', 'b', ... '' '' ');% boldface blue zlabel (' Z ', ...' Fontweight ',' B ',' Color ',' b ', ...' Verticala Lignment ',' bottom ');% boldface bluequiver3 (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 Options
MATLAB 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 flight window to a portion That In not overlapping the commit windowmatlab code
% 3D Graphics - A Tour of Matlab's Powerful
% Visualization Tools
% Dr. p.venkataraman
% Please make Sure the first window and the commit 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 Whatver is in The String
Evalstra (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; '}; evArstra (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 ', ...' 'VerticalAlign ",' bottom ') FPRINTF ('strike any key to continue - title contacts the command / n'); Pause End
Title ('Label Axes',' Color ',' K ', ...' '); YLabel (' Y ', ...' Fontweight ',' B ',' Color ',' B ', ...' '' '' '');% Boldface Blue XLabel ('x', ... 'Fontweight', 'B', 'Color', 'B', ... 'VerticalALIGNMENT', ' Bottom ');% boldface blue zlabel (' Z ', ...' fontweight ',' b ',' color ',' b ', ...' 'verticalalignment', 'bottom');% boldface bluefprintf ('strike Any Key to Continue - Title Contains The Command / N '); PAUSE
Title ('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: quiver
In 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
This topic related pictures are as follows:% 3D Graphics - Using Quiver to Draw Normal To Surface% Dr. P. Thenkataraman
Format 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; [XY] = 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', ... '' '' '' '' '' '');% Boldface Blue XLabel ('X', ... 'Fontweight', 'b', 'color', 'b', ... '' '' ');% boldface blue zlabel (' Z ', ...' Fontweight ',' B ',' Color ',' BOTTOM ');% boldface blue title (' / bf / itsurface normals', 'color', 'k', ... 'verticalterialignment', '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 .
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 picture this topic are as follows:
% 3D Graphics - Function of Three Variables
% Dr. p.venkataraman
Format Compact Set (GCF, 'Menubar', 'None', 'Name', 'Three Variables', ... 'Numertitle', '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', ... '' Vertical_Ment ',' Bottom ');% Boldface Blue XLabel (' x ', ...' FontWeight ',' B ',' Color ',' B ', ...' 'VerticalAlignment', 'Bottom');% Boldface Blue Zlabel ('Z', ... 'Fontweight', 'B', 'Color' , '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', ... '' Vertical_Ment ',' Bottom ');% Boldface Blue XLabel (' x ', ...' FontWeight ',' B ',' Color ',' B ', ...' 'VerticalAlignment', 'Bottom');% Boldface Blue Zlabel ('Z', ... 'Fontweight', 'B', 'Color' , 'b', ... '' ');% 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 Slices
In The Previous Illustration The Slices WERE ON Planes Which is what is usually encountered. How About a curved slice -i.e. a plot the value 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
The related pictures of this topic are as follows:
% 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 Dispeyed% 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', '' '' '' '' '' '');% Boldface Blue Title ('/ BF / ITCURVED SLICES - 3 Variables ",' Color ',' k ', ...' 'VerticalAlignment', 'bottom');