Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

""" 

Surfaces of revolution 

 

AUTHORS: 

 

- Oscar Gerardo Lazo Arjona (2010): initial version. 

""" 

 

#***************************************************************************** 

# Copyright (C) 2010 Oscar Gerardo Lazo Arjona algebraicamente@gmail.com 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function 

 

from sage.plot.plot3d.parametric_plot3d import parametric_plot3d 

def revolution_plot3d(curve,trange,phirange=None,parallel_axis='z',axis=(0,0),print_vector=False,show_curve=False,**kwds): 

""" 

Return a plot of a revolved curve. 

 

There are three ways to call this function: 

 

- ``revolution_plot3d(f,trange)`` where `f` is a function located in the `x z` plane. 

 

- ``revolution_plot3d((f_x,f_z),trange)`` where `(f_x,f_z)` is a parametric curve on the `x z` plane. 

 

- ``revolution_plot3d((f_x,f_y,f_z),trange)`` where `(f_x,f_y,f_z)` can be any parametric curve. 

 

INPUT: 

 

- ``curve`` - A curve to be revolved, specified as a function, a 2-tuple or a 3-tuple. 

 

- ``trange`` - A 3-tuple `(t,t_{\min},t_{\max})` where t is the independent variable of the curve. 

 

- ``phirange`` - A 2-tuple of the form `(\phi_{\min},\phi_{\max})`, (default `(0,\pi)`) that specifies the angle in which the curve is to be revolved. 

 

- ``parallel_axis`` - A string (Either 'x', 'y', or 'z') that specifies the coordinate axis parallel to the revolution axis. 

 

- ``axis`` - A 2-tuple that specifies the position of the revolution axis. If parallel is: 

 

- 'z' - then axis is the point in which the revolution axis intersects the `x y` plane. 

 

- 'x' - then axis is the point in which the revolution axis intersects the `y z` plane. 

 

- 'y' - then axis is the point in which the revolution axis intersects the `x z` plane. 

 

- ``print_vector`` - If True, the parametrization of the surface of revolution will be printed. 

 

- ``show_curve`` - If True, the curve will be displayed. 

 

 

EXAMPLES: 

 

Let's revolve a simple function around different axes:: 

 

sage: u = var('u') 

sage: f = u^2 

sage: revolution_plot3d(f, (u,0,2), show_curve=True, opacity=0.7).show(aspect_ratio=(1,1,1)) 

 

.. PLOT:: 

 

u = var('u') 

f = u**2 

P = revolution_plot3d(f, (u,0,2), show_curve=True, opacity=0.7).plot() 

sphinx_plot(P) 

 

If we move slightly the axis, we get a goblet-like surface:: 

 

sage: revolution_plot3d(f, (u,0,2), axis=(1,0.2), show_curve=True, opacity=0.5).show(aspect_ratio=(1,1,1)) 

 

.. PLOT:: 

 

u = var('u') 

f = u**2 

P = revolution_plot3d(f, (u,0,2), axis=(1,0.2), show_curve=True, opacity=0.5).plot() 

sphinx_plot(P) 

 

A common problem in calculus books, find the volume within the following revolution solid:: 

 

sage: line = u 

sage: parabola = u^2 

sage: sur1 = revolution_plot3d(line, (u,0,1), opacity=0.5, rgbcolor=(1,0.5,0), show_curve=True, parallel_axis='x') 

sage: sur2 = revolution_plot3d(parabola, (u,0,1), opacity=0.5, rgbcolor=(0,1,0), show_curve=True, parallel_axis='x') 

sage: (sur1+sur2).show() 

 

.. PLOT:: 

 

u = var('u') 

line = u 

parabola = u**2 

sur1 = revolution_plot3d(line, (u,0,1), opacity=0.5, rgbcolor=(1,0.5,0), show_curve=True, parallel_axis='x') 

sur2 = revolution_plot3d(parabola, (u,0,1), opacity=0.5, rgbcolor=(0,1,0), show_curve=True, parallel_axis='x') 

P = sur1 + sur2 

sphinx_plot(P) 

 

Now let's revolve a parametrically defined circle. We can play with the topology of the surface by changing the axis, 

an axis in `(0,0)` (as the previous one) will produce a sphere-like surface:: 

 

sage: u = var('u') 

sage: circle = (cos(u), sin(u)) 

sage: revolution_plot3d(circle, (u,0,2*pi), axis=(0,0), show_curve=True, opacity=0.5).show(aspect_ratio=(1,1,1)) 

 

.. PLOT:: 

 

u = var('u') 

circle = (cos(u), sin(u)) 

P = revolution_plot3d(circle, (u,0,2*pi), axis=(0,0), show_curve=True, opacity=0.5) 

sphinx_plot(P) 

 

An axis on `(0,y)` will produce a cylinder-like surface:: 

 

sage: revolution_plot3d(circle, (u,0,2*pi), axis=(0,2), show_curve=True, opacity=0.5).show(aspect_ratio=(1,1,1)) 

 

.. PLOT:: 

 

u = var('u') 

circle = (cos(u), sin(u)) 

P = revolution_plot3d(circle, (u,0,2*pi), axis=(0,2), show_curve=True, opacity=0.5) 

sphinx_plot(P) 

 

And any other axis will produce a torus-like surface:: 

 

sage: revolution_plot3d(circle, (u,0,2*pi), axis=(2,0), show_curve=True, opacity=0.5).show(aspect_ratio=(1,1,1)) 

 

.. PLOT:: 

 

u = var('u') 

circle = (cos(u), sin(u)) 

P = revolution_plot3d(circle, (u,0,2*pi), axis=(2,0), show_curve=True, opacity=0.5) 

sphinx_plot(P) 

 

Now, we can get another goblet-like surface by revolving a curve in 3d:: 

 

sage: u = var('u') 

sage: curve = (u, cos(4*u), u^2) 

sage: P = revolution_plot3d(curve, (u,0,2), show_curve=True, parallel_axis='z',axis=(1,.2), opacity=0.5) 

sage: P.show(aspect_ratio=(1,1,1)) 

 

.. PLOT:: 

 

u = var('u') 

curve = (u, cos(4*u), u**2) 

P = revolution_plot3d(curve, (u,0,2), show_curve=True, parallel_axis='z', axis=(1,.2), opacity=0.5) 

sphinx_plot(P) 

 

A curvy curve with only a quarter turn:: 

 

sage: u = var('u') 

sage: curve = (sin(3*u), .8*cos(4*u), cos(u)) 

sage: revolution_plot3d(curve, (u,0,pi), (0,pi/2), show_curve=True, parallel_axis='z', opacity=0.5).show(aspect_ratio=(1,1,1),frame=False) 

 

.. PLOT:: 

 

u = var('u') 

curve = (sin(3*u), .8*cos(4*u), cos(u)) 

P = revolution_plot3d(curve, (u,0,pi), (0,pi/2), show_curve=True, parallel_axis='z', opacity=0.5) 

sphinx_plot(P) 

 

One can also color the surface using a coloring function of two 

parameters and a colormap as follows:: 

 

sage: u, phi = var('u,phi') 

sage: def cf(u,phi): return sin(phi+u) ^ 2 

sage: curve = (1+u^2/4, 0, u) 

sage: revolution_plot3d(curve, (u,-2,2), (0,2*pi), parallel_axis='z', color=(cf, colormaps.PiYG)).show(aspect_ratio=(1,1,1)) 

 

.. PLOT:: 

 

u, phi = var('u,phi') 

def cf(u, phi): return sin(phi+u) ** 2 

curve = (1+u**2/4, 0, u) 

P = revolution_plot3d(curve, (u,-2,2), (0,2*pi), parallel_axis='z', color=(cf, colormaps.PiYG)) 

sphinx_plot(P) 

 

The first parameter of the coloring function will be identified with the 

parameter of the curve, and the second with the angle parameter. 

 

.. WARNING:: 

 

This kind of coloring using a colormap can be visualized using 

Jmol, Tachyon (option ``viewer='tachyon'``) and Canvas3D 

(option ``viewer='canvas3d'`` in the notebook). 

""" 

from sage.symbolic.ring import SR 

from sage.symbolic.constants import pi 

from sage.functions.other import sqrt 

from sage.functions.trig import sin 

from sage.functions.trig import cos 

from sage.functions.trig import atan2 

 

if parallel_axis not in ['x', 'y', 'z']: 

raise ValueError("parallel_axis must be either 'x', 'y', or 'z'.") 

 

vart = trange[0] 

 

if str(vart) == 'phi': 

phi = SR.var('fi') 

else: 

phi = SR.var('phi') 

 

if phirange is None: # this if-else provides a phirange 

phirange = (phi, 0, 2 * pi) 

elif len(phirange) == 3: 

phi = phirange[0] 

pass 

else: 

phirange = (phi, phirange[0], phirange[1]) 

 

if isinstance(curve, (tuple, list)): 

#this if-else provides a vector v to be plotted 

#if curve is a tuple or a list of length 2, it is interpreted as a parametric curve 

#in the x-z plane. 

#if it is of length 3 it is interpreted as a parametric curve in 3d space 

 

if len(curve) == 2: 

x = curve[0] 

y = 0 

z = curve[1] 

elif len(curve) == 3: 

x = curve[0] 

y = curve[1] 

z = curve[2] 

else: 

x = vart 

y = 0 

z = curve 

 

phase = 0 

if parallel_axis == 'z': 

x0 = axis[0] 

y0 = axis[1] 

# (0,0) must be handled separately for the phase value 

if x0 != 0 or y0 != 0: 

phase = atan2(y - y0, x - x0) 

R = sqrt((x-x0)**2 + (y-y0)**2) 

v = (R*cos(phi+phase)+x0, R*sin(phi+phase)+y0, z) 

elif parallel_axis == 'x': 

y0 = axis[0] 

z0 = axis[1] 

# (0,0) must be handled separately for the phase value 

if z0 != 0 or y0 != 0: 

phase = atan2(z - z0, y - y0) 

R = sqrt((y-y0)**2 + (z-z0)**2) 

v = (x, R*cos(phi+phase)+y0, R*sin(phi+phase)+z0) 

elif parallel_axis == 'y': 

x0 = axis[0] 

z0 = axis[1] 

# (0,0) must be handled separately for the phase value 

if z0 != 0 or x0 != 0: 

phase = atan2(z - z0, x - x0) 

R = sqrt((x-x0)**2 + (z-z0)**2) 

v = (R*cos(phi+phase)+x0, y, R*sin(phi+phase)+z0) 

 

if print_vector: 

print(v) 

 

if show_curve: 

curveplot = parametric_plot3d((x, y, z), trange, thickness=2, 

rgbcolor=(1, 0, 0)) 

return parametric_plot3d(v, trange, phirange, **kwds) + curveplot 

 

return parametric_plot3d(v, trange, phirange, **kwds)