~ubuntu-branches/ubuntu/hardy/suitesparse/hardy

« back to all changes in this revision

Viewing changes to MATLAB_Tools/shellgui/seashell.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere, Rafael Laboissiere, Ondrej Certik
  • Date: 2008-02-21 14:46:50 UTC
  • mfrom: (1.1.2 upstream) (5.1.1 hardy)
  • Revision ID: james.westby@ubuntu.com-20080221144650-tgeppgj0t7s759i8
Tags: 3.1.0-3
[ Rafael Laboissiere ]
* Upload to unstable

[ Ondrej Certik ]
* XS-DM-Upload-Allowed: yes field added
* Ondrej Certik added to uploaders

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function seashell (a, b, c, n, azimuth, elevation, res)
 
2
% SEASHELL draws a pretty Florida 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, Tim Davis, University of Florida
 
50
 
 
51
% use default input parameters, if not present
 
52
if (nargin == 1 && ischar (a))
 
53
    in = -1 ;
 
54
else
 
55
    in = nargin ;
 
56
end
 
57
if (in < 1)
 
58
    a = -0.2 ;
 
59
end
 
60
if (in < 2)
 
61
    b = 0.5 ;
 
62
end
 
63
if (in < 3)
 
64
    c = 0.1 ;
 
65
end
 
66
if (in < 4)
 
67
    n = 2 ;
 
68
end
 
69
if (in < 5)
 
70
    azimuth = -150 ;
 
71
end
 
72
if (in < 6)
 
73
    elevation = 10 ;
 
74
end
 
75
if (in < 7)
 
76
    res = 128 ;
 
77
end
 
78
if (in == -1)
 
79
    azimuth = Inf ;
 
80
end
 
81
 
 
82
% sanity checks
 
83
if (a == 0)
 
84
    a = 0.01 ;
 
85
end
 
86
if (n <= 0)
 
87
    n = 0.1 ;
 
88
end
 
89
 
 
90
% construct the res-by-res mesh
 
91
t = linspace(0, 2*pi, res) ;
 
92
[u,v] = meshgrid(t) ;
 
93
 
 
94
% define the surface
 
95
x = (a*(1-v/(2*pi)).*(1+cos(u)) + c) .* cos(n*v) ;
 
96
y = (a*(1-v/(2*pi)).*(1+cos(u)) + c) .* sin(n*v) ;
 
97
z = b*v/(2*pi) + a*(1-v/(2*pi)) .* sin(u) ;
 
98
 
 
99
% plot the surface
 
100
surf(x,y,z,y)
 
101
shading interp
 
102
 
 
103
axis off
 
104
axis equal
 
105
colormap(hsv(1024))
 
106
material shiny
 
107
lighting gouraud
 
108
lightangle(80, -40)
 
109
lightangle(-90, 60)
 
110
 
 
111
% fix the view, or spin the seashell
 
112
if (isfinite (azimuth))
 
113
    view([azimuth elevation])
 
114
else
 
115
    for az = -180:10:180
 
116
        view ([az elevation])
 
117
        drawnow
 
118
    end
 
119
end