1function seashell (a, b, c, n, azimuth, elevation, res)
2% SEASHELL draws a pretty seashell, using a 3D parametric surface.
3%
4% Usage:
5%
6%   seashell (a, b, c, n, azimuth, elevation, res)
7%   seashell ('spin') ;
8%
9%   All arguments are optional.  The first four control the coefficients of
10%   the parametric surface (u and v are the surface parameters):
11%
12%       x = (a*(1-v/(2*pi)).*(1+cos(u)) + c) .* cos(n*v) ;
13%       y = (a*(1-v/(2*pi)).*(1+cos(u)) + c) .* sin(n*v) ;
14%       z = b*v/(2*pi) + a*(1-v/(2*pi)) .* sin(u) ;
15%
16%   a,b: these determine how pointy or flat the shell is (informally...)
17%   c: determines how much the shell overlaps with itself
18%   n: the number of spirals in the shell
19%
20%   azimuth, elevation: determines the viewing angle (see the 'view' function)
21%   res: the mesh size (res-by-res).  A larger number gives a smoother surface.
22%
23%   If the azimuth is Inf, then the seashell view is spun dynamically.
24%   Also try seashell ('spin') ;
25%
26% References:
27%   T. Davis & K. Sigmon, MATLAB Primer, 7th edition, CRC Press, 2005, pp. 80.
28%   von Seggern, CRC Standard Curves and Surfaces, 2nd edition, CRC Press,
29%       1993, pp. 306-307.
30%
31% Example:
32%   seashell ;          % draws the front cover of the MATLAB Primer
33%   seashell (-0.5) ;   % draws the back cover
34%   seashell (a,b,c,n,az,el,res) ;  % all options, defaults:
35%                       % a=-0.2, b=0.5, c=0.1, n=2, az=-150, el=10, res=128
36%
37%   for a = -1:.1:1
38%	seashell (a) ;
39%	drawnow ;
40%   end
41%   for b = -1:.1:1
42%	seashell (-.2, b) ;
43%	drawnow
44%   end
45%
46% See also SHELLGUI, SURF, VIEW, LINSPACE, MESHGRID, SHADING, LIGHTING,
47%   LIGHTANGLE, COLORMAP, AXIS, MATERIAL, SIN, COS, PI.
48
49% Copyright 2006, Timothy A. Davis, http://www.suitesparse.com
50
51% use default input parameters, if not present
52if (nargin == 1 && ischar (a))
53    in = -1 ;
54else
55    in = nargin ;
56end
57if (in < 1)
58    a = -0.2 ;
59end
60if (in < 2)
61    b = 0.5 ;
62end
63if (in < 3)
64    c = 0.1 ;
65end
66if (in < 4)
67    n = 2 ;
68end
69if (in < 5)
70    azimuth = -150 ;
71end
72if (in < 6)
73    elevation = 10 ;
74end
75if (in < 7)
76    res = 128 ;
77end
78if (in == -1)
79    azimuth = Inf ;
80end
81
82% sanity checks
83if (a == 0)
84    a = 0.01 ;
85end
86if (n <= 0)
87    n = 0.1 ;
88end
89
90% construct the res-by-res mesh
91t = linspace(0, 2*pi, res) ;
92[u,v] = meshgrid(t) ;
93
94% define the surface
95x = (a*(1-v/(2*pi)).*(1+cos(u)) + c) .* cos(n*v) ;
96y = (a*(1-v/(2*pi)).*(1+cos(u)) + c) .* sin(n*v) ;
97z = b*v/(2*pi) + a*(1-v/(2*pi)) .* sin(u) ;
98
99% plot the surface
100surf(x,y,z,y)
101shading interp
102
103axis off
104axis equal
105colormap(hsv(1024))
106material shiny
107lighting gouraud
108lightangle(80, -40)
109lightangle(-90, 60)
110
111% fix the view, or spin the seashell
112if (isfinite (azimuth))
113    view([azimuth elevation])
114else
115    for az = -180:10:180
116        view ([az elevation])
117        drawnow
118    end
119end
120