1 / 82

AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 7 3D GRAPHICS

AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 7 3D GRAPHICS. Chapter 7 – Objective Present the implementation of a wide selection of three-dimensional plotting capabilities. Topics. Lines in 3D Surfaces. Lines in 3D - The 3D version of plot is

elvis-morin
Télécharger la présentation

AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 7 3D GRAPHICS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 7 3D GRAPHICS

  2. Chapter 7 – Objective • Present the implementation of a wide selection of three-dimensional plotting capabilities.

  3. Topics Lines in 3D Surfaces

  4. Lines in 3D - • The 3D version of plot is • plot3(u1, v1, w1, c1, u2, v2, w2, c2,…) • where • uj, vj, and wj are the x, y, and z coordinates, respectively, of a point. • They are scalars, vectors of the same length, matrices of the same order, or expressions that, when evaluated, result in one of these three quantities. • cj is a string of characters - • One character specifies the color. • One character specifies the point characteristics. • One or two characters specify the line type.

  5. To draw a set of n unconnected lines whose end points are • (x1j,y1j,z1j) and (x2j,y2j,z2j), j = 1, 2, …, n • we create six vectors • Then, plot3 is • x1 = […]; x2 = […]; • y1 = […]; y2 = […]; • z1 = […]; z2 = […]; • plot3([x1; x2], [y1; y2], [z1; z2]) • where [x1; x2], [y1; y2], and [z1; z2] are each (2n) matrices.

  6. All annotation procedures discussed for 2D drawings are applicable to the 3D curve- and surface-generating functions, except that the arguments of text become • text(x, y, z, s) • where s is a string and • zlabel • is used to label the z-axis.

  7. Example – Drawing Wire Frame Boxes • We shall create a function called BoxPlot3 to draw the four edges of each of the six surfaces of a box and then use the function to draw several boxes.

  8. The location and orientation of the box are determined by the coordinates of its two diagonally opposed corners: P(xo,yo,zo) and P(xo+Lx, yo+Ly, zo+Lz). The script is

  9. function BoxPlot3(x0, y0, z0, Lx, Ly, Lz) x = [x0, x0, x0, x0, x0+Lx, x0+Lx, x0+Lx, x0+Lx]; %(18) y = [y0, y0, y0+Ly, y0+Ly, y0, y0, y0+Ly, y0+Ly]; %(18) z = [z0, z0+Lz, z0+Lz, z0, z0, z0+Lz, z0+Lz, z0]; %(18) index = zeros(6,5); index(1,:) = [1 2 3 4 1]; index(2,:) = [5 6 7 8 5]; index(3,:) = [1 2 6 5 1]; index(4,:) = [4 3 7 8 4]; index(5,:) = [2 6 7 3 2]; index(6,:) = [1 5 8 4 1]; for k = 1:6 plot3(x(index(k,:)), y(index(k,:)), z(index(k,:))) hold on end

  10. We now use BoxPLot3 to generate three boxes with the following dimensions and the coordinates (xo, yo, zo). • Box #1 • Size: 357 • Location: (1, 1, 1) • Box #2 • Size: 451 • Location: (3, 4, 5) • Box #3 • Size: 111 • Location: (4.5, 5.5, 6)

  11. The script to create and display these wire frame boxes is • BoxPlot3(1, 1, 1, 3, 5, 7) • BoxPlot3(4, 6, 8, 4, 5, 1) • BoxPlot3(8, 11, 9, 1, 1, 1) • Box #1 • Size: 357 • Location: (1,1,1) • Box #2 • Size: 451 • Location: (3,4,5) • Box #3 • Size: 111 • Location: (4.5,5.5,6) function BoxPlot3(x0, y0, z0, Lx, Ly, Lz)

  12. Example – Sine Wave Drawn on the Surface of a • Cylinder • The coordinates of a sine wave on the surface of a cylinder are obtained from the following relations. If we assume that a = 10.0, b = 1.0, c = 0.3, and 0 t 2, then the script is

  13. t = linspace(0, 2*pi, 200); a = 10; b = 1.0; c = 0.3; x = b*cos(t); y = b*sin(t); z = c*cos(a*t); plot3(x, y, z, 'k') axis equal

  14. Surfaces • A set of 3D plotting functions is available to create surfaces, contours, and variations and specialization of these basic forms. • A surface is defined by the expression where x and y are the coordinates in the xy-plane and z is the resulting height.

  15. The basic surface plotting functions are • surf(x, y, z) • and • mesh(x, y, z) • where the x, y, and z are the coordinates of the points on the surface. • surf – draws a surface composed of colored patches. • The colors of the patches are determined by the magnitude of z. • mesh – draws white surface patches that are defined by • their boundary. • The colors of the lines are determined by the magnitude of z.

  16. Example – • Consider the surface created by over the range 3 < x < 3 and 3 < y < 13. We shall place the generation of the x, y, and z coordinate values in an M file so that we can use it in several examples. We call this function SurfExample.

  17. function [x, y, z] = SurfExample x1 = linspace(-3, 3, 15); % (115) y1 = linspace(-3, 13, 17); % (117) [x, y] = meshgrid(x1, y1); % (1715) z = x.^4+3*x.^22*x+6-2*y.*x.^2+y.^2-2*y; % (1715)

  18. Difference Between surf and mesh [x,y,z] = SurfExample; surf(x, y, z) [x,y,z] = SurfExample; mesh(x, y, z)

  19. [x,y,z] = SurfExample; mesh(x, y, z) hidden off

  20. Combining Surfaces and Lines • One can combine 3D plotting functions to draw multiple surfaces and multiple lines. • To illustrate this, we create two functions – • Corners • Draws four lines connecting the corners of the surface generated by SurfExample to the xy-plane passing through z = 0. • Disc • Creates a circular disc that intersects the surface created by SurfExample at zo = 80, has a radius of 10 units, and has its center at (0,5).

  21. The coordinates of the corners are: (3, 3, z(3,3)) (3, 13, z(3,13)) (3, 3, z(3,3)) (3, 13, z(3,13)) • The two functions are • function Corners • xc = [-3, -3, 3, 3]; • yc = [-3, 13, 13, -3]; • zc = xc.^4+3*xc.^22*xc+62*yc.*xc.^2+yc.^22*yc; • hold on • plot3([xc; xc], [yc; yc], [zeros(1,4); zc], 'k')

  22. function Disc(R, zo) r = linspace(0, R, 12); % (112) theta = linspace(0, 2*pi, 50); % (150) x = cos(theta')*r; % (5012) y = 5 + sin(theta')*r; % (5012) hold on z = repmat(zo, size(x)); % (5012) surf(x, y, z)

  23. The script is • [x, y, z] = SurfExample; • surf(x, y, z); • Disc(10, 80) • Corners

  24. Altering Graph Appearance • Several functions that can be used in various combinations to alter the appearance of the resulting surface plot are - • box on or box off • grid on or grid off • axis on or axis off • The function boxon only draws a box if axison has been selected.

  25. Illustration of box, grid, and axis [x,y,z] = SurfExample mesh(x, y, z) grid off [x,y,z] = SurfExample mesh(x, y, z) axis off grid off

  26. [x,y,z] = SurfExample mesh(x, y, z) axis on grid off box on

  27. The colors of either the patches created by surf or the lines created by mesh can be changed to a uniform color using • colormap(c) • where c is a three-element vector, each of whose value varies between 0 and 1. • The first element corresponds to the intensity of red, the second to the intensity of green, and the third to the intensity of blue.

  28. Some Values of the Color Vector Used in colormap(c)

  29. Additional Ways to Visually Enhance a Surface [x,y,z] = SurfExample; meshz(x, y, z) [x,y,z] = SurfExample; waterfall(x, y, z)

  30. [x,y,z] = SurfExample; ribbon(y, z) [x,y,z] = SurfExample; surfnorm(x, y, z)

  31. Contour Plots • Surfaces can also be transformed into contour plots, which are plots of the curves formed by the intersection of the surface and a plane parallel to the xy-plane at given values of z. • The functions • surfc(x, y, z) • meshc(x, y, z) • create surfaces with contours projected beneath the surface. • The quantities x, y, and z are the values of the coordinates of points that define the surface.

  32. Illustration of meshc and surfc [x,y,z] = SurfExample; meshc(x, y, z) grid off [x,y,z] = SurfExample; surfc(x, y, z) grid off

  33. Various contour plots without the surfaces can be created, either with labels or without labels. • The function • contour(x, y, z, v) • creates a 2D contour plot where • x, y, and z are the coordinates of the points that define the surface. • v, if a scalar, is the number of contours to be displayed and, if a vector of values, the contours of the surface at those values of z . • The use of v is optional.

  34. If the contour plot is to be labeled, then we use the following pair of functions. • [C, h] = contour(x, y, z, v) • clabel(C, h, v)

  35. Illustration of contour and clabel [x,y,z] = SurfExample; contour(x, y, z) [x,y,z] = SurfExample; contour(x, y, z, 4)

  36. [x,y,z] = SurfExample; [C, h] = contour(x, y, z); clabel(C, h) [x,y,z] = SurfExample; v= [10, 30:30:120]; [clabel(C, h, v)

  37. To display the contours of the surface in 3D, we use • contour3(x, y, z, v) • where • x, y, and z are the coordinates of points that define the surface. • v, if a scalar, is the number of contours to be displayed and, if a vector of values, the contours of the surface at those values of z. • The use of v is optional. • To label the contour plot, we use the pair of functions • [C, h] = contour3(x, y, z, v) • clabel(C, h, v)

  38. To fill the region between the 2D contours with different colors, we use • contourf(x, y, z, v) • The values of the colors can be identified using • colorbar(s) • which places a bar of colors and their corresponding numerical values adjacent to the figure. • The quantity s is a string equal to either 'horiz' or 'vert' to indicate the orientation of the bar. • The default value is 'vert'.

  39. Illustration of contour3, contourf, and colorbar [x,y,z] = SurfExample; [C, h] = contour3(x, y, z); clabel(C, h) [x,y,z] = SurfExample; [C, h] = contourf(x, y, z); colorbar

  40. The properties of the lines and numbering in contour can be altered a similar manner that was done for plot. For example, to have the contour labels created by contour be enlarged to 14 points and for all the contour lines to be blue, we employ the following steps. • [x, y, z] = SurfExample; • [C, h] = contour(x, y, z, v) • g = clabel(C, h, v); • set(g, 'Fontsize', 14) • set(h, 'LineColor', ‘b')

  41. Generation of Cylindrical, Spherical, and Ellipsoidal Surfaces • One can use a 2D curve as a generator to create surfaces of revolution by using • [x, y, z] = cylinder(r, n) • which returns the x, y, and z coordinates of a cylindrical surface using the vector r to define a profile curve. • The function cylinder treats each element in r as a radius at n equally spaced points around its circumference. • If n is omitted, a value of 20 is used.

  42. Example – • Consider the curve • which is rotated 360 about the z-axis. • Let us take 26 equally spaced increments in the z-direction and 16 equally spaced increments in the circumferential direction. • The script to plot a cylindrical surface is • zz = linspace(0, 2*pi, 26); • [x, y, z] = cylinder(1.1+sin(zz), 16); • surf(x, y, z) • axis off

  43. To create a sphere, one can use • [x, y, z] = sphere(n); • axisequal • surf(x, y, z) • where n is the number of n by n elements that will comprise the sphere of radius 1 centered at the origin. • If n is omitted, then n = 20.

  44. To create an ellipsoid, we use • [x, y, z] = ellipsoid(xc, yc, zc, xr, yr, zr, n); • axisequal • surf(x, y, z) • which is centered at (xc, yc, zc) and has semi-axis lengths in the x, y, and z directions, respectively, of xr, yr, and zr. • In addition, n is the number of n by n elements that will comprise the ellipsoid. • If n is omitted, then n = 20.

  45. Viewing Angle • There are instances when one wants to change the default viewing angle of the 3D image because – • It does not display the features of interest. • Several different views are to be displayed using subplot. • Exploration of the surface from many different views is desired before deciding on the final orientation. • To determine the azimuth and elevation angle of the view, we use • [a, e] = view • where a is the azimuth and e the elevation.

  46. To orient the object, one depresses the Rotate 3D icon in the figure window and orients the object until a satisfactory orientation is obtained. • Upon typing the previous expression in the command window, the values of the azimuth and elevation will be displayed. • These values are recorded and entered in the expression • view(an, en) • to create the desired orientation the next time that the script is executed. • In this expression, an and en are the numerical values of a and e taken from the command window.

  47. Shading • The surfaces created with surf have used the default shading property called 'faceted'. • The function that changes the shading is • shading s • where s is a string equal to • faceted% Default • flat • interp

  48. Illustration of view and shading zz = linspace(0, 2*pi, 26); r=1.1+sin(zz); [x, y, z] = cylinder(r, 16); surf(x, y, z) view(-88.5, -48) shadingfaceted axis off vis3d

  49. zz = linspace(0, 2*pi, 26); • r=1.1+sin(zz); • [x, y, z] = cylinder(r, 16); • surf(x, y, z) • view(-88.5, -48) • shadingflat • axis off vis3d

More Related