Adam Szustalewicz Matematyka obliczeniowa 2002/2003 w.13 20.01.03 Wykład 13: Grafika trójwymiarowa c.d., animacja 1. Kolory i cieniowanie colormap jet, hsv, hot, cool, pink, copper, flag, gray, bone shading faceted, interpolated, flat view view(az, el), view([x,y,z]), view(3): az=-37.5, el=30; view(2): az=0, el=90 1.1 Przykłady subplot(2,2,1) surfc(peaks), set (gcf, Color , w ) colormap(jet), shading interp subplot(2,2,2) surf(peaks), view(2) shading interp, axis tight subplot(2,2,3) [x, y, z] = torus (1,50,1); surf(x,y,z), shading flat subplot(2,2,4) surf(peaks), view(2) shading faceted 1.1.1 Torus function [x, y, z] = torus (r, n, a) % TORUS Generate a torus. % torus (r, n, a) generates a plot of a if nargin < 3, a = 1 ; end % torus with central radius a and if nargin < 2, n = 30 ; end % lateral radius r. if nargin < 1, r = 0.5 ; end % n controls the number of facets theta = pi * (0:2:2*n)/n ; % on the surface. phi = 2*pi* (0:2:n) /n ; % These input variables are optional xx = (a + r*cos(phi)) * cos(theta) ; % with defaults r = 0.5, n = 30, a = 1. yy = (a + r*cos(phi)) * sin(theta) ; % zz = r * sin(phi) * ones(size(theta)) ; % [x, y, z] = torus(r, n, a) generates if nargout == 0 % three (n + 1)-by-(n + 1) matrices so surf (xx, yy, zz) ; % that surf (x, y, z) will produce the ar = (a + r)/sqrt(2) ; % torus. axis([-ar, ar, -ar, ar, -ar, ar]) ; % else % See also SPHERE, CYLINDER x = xx ; y = yy ; z = zz ; % end % MATLAB Primer, 6th Edition % Kermit Sigmon and Timothy A. Davis % Section 11.5, page 65. 1 Adam Szustalewicz Matematyka obliczeniowa 2002/2003 w.13 20.01.03 2. Animacja 1. Film clear; clc; close all z=peaks; surf(z) % surf(cos(2*pi*(5-1)/20).*z) axis tight set(gca, nextplot , replacechildren ) disp( zapamietujemy film: ) n=20; M = moviein(n); for j=1:n surf(cos(2*pi*(j-1)/20).*z) M(:,j) = getframe; end disp( odtwarzanie ... ); movie(M,2) % dwukrotne 2. Rysowanie śladu punktu ogon komety clear; clc; close all x=linspace(-2,2,500); y=exp(x).*sin(1./x); comet(x,y,0.5) disp( Nacisnij klawisz ... ); pause t = -pi:pi/200:pi; comet(t,tan(sin(t))-sin(tan(t))) disp( Nacisnij klawisz ... ); pause t = -pi:pi/500:pi; comet3(sin(5*t),cos(3*t),t) 3. matlabowska funkcja comet function comet(x, y, p) %COMET Comet-like trajectory. % COMET(Y) displays an animated comet plot of the vector Y. % COMET(X,Y) displays an animated comet plot of vector Y vs. X. % COMET(X,Y,p) uses a comet of length p*length(Y). Default is p = 0.10. % % Example: % t = -pi:pi/200:pi; % comet(t,tan(sin(t))-sin(tan(t))) % % See also COMET3. % Charles R. Denham, MathWorks, 1989. % Revised 2-9-92, LS and DTP; 8-18-92, 11-30-92 CBM. % Copyright 1984-2000 The MathWorks, Inc. % $Revision: 5.10 $ $Date: 2000/06/02 04:30:47 $ if nargin == 0, error( Not enough input arguments. ); end if nargin < 2, y = x; x = 1:length(y); end if nargin < 3, p = 0.10; end ax = newplot; 2 Adam Szustalewicz Matematyka obliczeniowa 2002/2003 w.13 20.01.03 if ~ishold, axis([min(x(isfinite(x))) max(x(isfinite(x))) ... min(y(isfinite(y))) max(y(isfinite(y)))]) end co = get(ax, colororder ); if size(co,1)>=3, % Choose first three colors for head, body, and tail head = line( color ,co(1,:), marker , o , erase , xor ,... MarkerSize ,10, xdata ,x(1), ydata ,y(1)); body = line( color ,co(2,:), linestyle , - , erase , none , ... xdata ,[], ydata ,[]); tail = line( color ,co(3,:), linestyle , - , erase , none , ... xdata ,[], ydata ,[]); else % Choose first three colors for head, body, and tail head = line( color ,co(1,:), marker , o , erase , xor , ... xdata ,x(1), ydata ,y(1)); body = line( color ,co(1,:), linestyle , -- , erase , none , ... xdata ,[], ydata ,[]); tail = line( color ,co(1,:), linestyle , - , erase , none , ... xdata ,[], ydata ,[]); end m = length(x); k = round(p*m); % Grow the body for i = 2:k+1 j = i-1:i; set(head, xdata ,x(i), ydata ,y(i)) set(body, xdata ,x(j), ydata ,y(j)) drawnow end % Primary loop for i = k+2:m j = i-1:i; set(head, xdata ,x(i), ydata ,y(i)) set(body, xdata ,x(j), ydata ,y(j)) set(tail, xdata ,x(j-k), ydata ,y(j-k)) drawnow end % Clean up the tail for i = m+1:m+k j = i-1:i; set(tail, xdata ,x(j-k), ydata ,y(j-k)) drawnow end 3 Adam Szustalewicz Matematyka obliczeniowa 2002/2003 w.13 20.01.03 "2u 4. Równanie różniczkowe = a2 "2u dla u(t, x) = sin(cx) cos(dt) "t2 "x2 clear, clc, close all, clf x1=0; % lewy koniec przedzialu xn1=1; % prawy koniec przedzialu n=128; % podzial na n rownych czesci; h=(xn1-x1)/n; x=linspace(x1,xn1,n+1); tau=0.0125; c=6*pi; d=3; ath2=(d/c*tau/h)^2; y0=sin(c*x); y1=(1-tau^2/2*d^2)*y0; y2=y0; axis([x1 xn1 -1.5 1.5]); grid on set(gca, nextplot , replacechildren ) set(gcf, DoubleBuffer , on ); fig = plot(x,y0, -k ); drawnow pause(5); % zatrzymanie fig = plot(x,y1, -k ); drawnow while 1 for k=2:n, y2(k) = ath2*(y1(k-1)-2.0*y1(k)+y1(k+1))+(2.0*y1(k)-y0(k)); end y0 = y1; y1 = y2; %fig = plot(x,y1, -k ); set(fig, YData ,y0); drawnow end 3. Zadania 1. Przerobić fragment programu z poprzedniego wykładu, rysujacy spirale w przestrzeni trójwymiarowej (figure(1)) tak, aby było widać ruch punktu rysujacego krzywa. 2. Uruchomić animacje na kilku przykładach. * * * 4