home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / GRAPHICS.PAK / PARAMETR.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  5.9 KB  |  181 lines

  1. (*********************************************************************
  2.  
  3.     Adapted from
  4.     Roman E. Maeder: Programming in Mathematica,
  5.     Second Edition, Addison-Wesley, 1991.
  6.  
  7.  *********************************************************************)
  8.  
  9.  
  10. (*:Version: Mathematica 2.0 *)
  11.  
  12. (*:Name: Graphics`ParametricPlot3D *)
  13.  
  14. (*:Title: Parametric Plots of 3D Objects *)
  15.  
  16. (*:Author:
  17.     Roman E. Maeder
  18. *)
  19.  
  20. (*:Keywords:
  21.     Parametric, Spherical, Cylindrical Plot, 3D
  22. *)
  23.  
  24. (*:Requirements: none. *)
  25.  
  26. (*:Warnings: none. *)
  27.  
  28. (*:Source:
  29.     Roman E. Maeder: Programming in Mathematica, 2nd Ed.,
  30.     Addison-Wesley, 1991.
  31. *)
  32.  
  33. (*:Summary:
  34. *)
  35.  
  36.  
  37. BeginPackage["Graphics`ParametricPlot3D`"]
  38.  
  39.  
  40.  
  41. (* Do not assume that the message for ParametricPlot3D has been loaded*)
  42.  
  43. ParametricPlot3D::usage = 
  44. "ParametricPlot3D[{fx, fy, fz}, {t, tmin, tmax}] produces a
  45. three-dimensional space curve parameterized by a variable t which runs
  46. from tmin to tmax. ParametricPlot3D[{fx, fy, fz}, {t, tmin, tmax}, {u,
  47. umin, umax}] produces a three-dimensional surface parametrized by t and
  48. u. ParametricPlot3D[{fx, fy, fz, s}, ...] shades the plot according to
  49. the color specification s. ParametricPlot3D[{{fx, fy, fz}, {gx, gy, gz},
  50. ...}, ...] plots several objects together. ParametricPlot3D[{x,y,z,(style)},
  51. {u,u0,u1,du}, ({v,v0,v1,dv})] uses increments du and dv instead of the
  52. PlotPoints option." 
  53.  
  54.  
  55. PointParametricPlot3D::usage =
  56. "PointParametricPlot3D[{x,y,z}, {u,u0,u1,(du)}, (options..)] plots
  57. a one-parameter set of points in space.  PointParametricPlot3D[{x,y,z},
  58. {u,u0,u1,(du)}, {v,v0,v1,(dv)}, (options..)] plots a two-parameter
  59. set of points in space.  Options are passed to Show[]."
  60.  
  61. SphericalPlot3D::usage = "SphericalPlot3D[r, {theta-range},
  62. {phi-range}, (options...)] plots r as a function of the angles
  63. theta and phi.  SphericalPlot3D[{r, style}, ...] uses style to
  64. render each surface patch."
  65.  
  66. CylindricalPlot3D::usage = "CylindricalPlot3D[z, {r-range},
  67. {phi-range}, (options...)] plots z as a function of r and phi.
  68. CylindricalPlot3D[{z, style},  ...] uses style to render each
  69. surface patch."
  70.  
  71. Begin["`Private`"]
  72.  
  73. protected = Unprotect[ParametricPlot3D]
  74.  
  75. FilterOptions[ command_Symbol, opts___ ] :=
  76.     Module[{keywords = First /@ Options[command]},
  77.         Sequence @@ Select[ {opts}, MemberQ[keywords, First[#]]& ]
  78.     ]
  79.  
  80. (* overload ParametricPlot3D to allow increments in iterators *)
  81.  
  82. ParametricPlot3D[ fun_,
  83.     {u_, u0_, u1_, du_:Automatic}, {v_, v0_, v1_, dv_:Automatic}, opts___ ] :=
  84.     Module[{plotpoints},
  85.         plotpoints = PlotPoints /. {opts} /. Options[ParametricPlot3D];
  86.         If[ Head[plotpoints] =!= List, plotpoints = {plotpoints, plotpoints} ];
  87.         If[ du =!= Automatic, plotpoints[[1]] = Round[N[(u1-u0)/du]] + 1 ];
  88.         If[ dv =!= Automatic, plotpoints[[2]] = Round[N[(v1-v0)/dv]] + 1 ];
  89.         ParametricPlot3D[ fun, {u, u0, u1}, {v, v0, v1},
  90.             PlotPoints -> plotpoints, opts]
  91.     ]  /; du =!= Automatic || dv =!= Automatic
  92.  
  93. ParametricPlot3D[ fun_, {u_, u0_, u1_, du_}, opts___ ] :=
  94.     ParametricPlot3D[ fun, {u, u0, u1}, PlotPoints -> Round[N[(u1-u0)/du]] + 1, opts]
  95.  
  96.  
  97. Attributes[PointParametricPlot3D] = {HoldFirst}
  98.  
  99. PointParametricPlot3D[ fun_,
  100.         {u_, u0_, u1_, du_:Automatic}, {v_, v0_, v1_, dv_:Automatic}, opts___ ] :=
  101.     Module[{plotpoints, ndu = N[du], ndv = N[dv]},
  102.         plotpoints = PlotPoints /. {opts} /. Options[ParametricPlot3D];
  103.         If[ plotpoints === Automatic, plotpoints = 15];
  104.         If[ Head[plotpoints] =!= List, plotpoints = {plotpoints, plotpoints} ];
  105.         If[ du === Automatic, ndu = N[(u1-u0)/(plotpoints[[1]]-1)] ];
  106.         If[ dv === Automatic, ndv = N[(v1-v0)/(plotpoints[[2]]-1)] ];
  107.         Show[ Graphics3D[Table[ Point[N[fun]], {u, u0, u1, ndu}, {v, v0, v1, ndv} ]],
  108.               FilterOptions[Graphics3D, opts] ]
  109.     ]  /; NumberQ[N[u0]] && NumberQ[N[u1]] && NumberQ[N[v0]] && NumberQ[N[v1]]
  110.  
  111.  
  112. (* point space curve *)
  113.  
  114. PointParametricPlot3D[ fun_, ul:{_, u0_, u1_, du_}, opts___ ] :=
  115.     Show[ Graphics3D[Table[ Point[N[fun]], ul ]], FilterOptions[Graphics3D, opts] ] /;
  116.             NumberQ[N[u0]] && NumberQ[N[u1]] && NumberQ[N[du]]
  117.  
  118. PointParametricPlot3D[ fun_, {u_, u0_, u1_}, opts___ ] :=
  119.     Module[{plotpoints},
  120.         plotpoints = PlotPoints /. {opts} /. Options[ParametricPlot3D];
  121.         If[plotpoints === Automatic, plotpoints = 15];
  122.         If[ Head[plotpoints] == List, plotpoints = plotpoints[[1]] ];
  123.     PointParametricPlot3D[ fun, {u, u0, u1, (u1-u0)/(plotpoints-1)}, opts ]
  124.     ]
  125.  
  126.  
  127. Attributes[SphericalPlot3D] = {HoldFirst}
  128.  
  129. SphericalPlot3D[ {r_, style_}, tlist:{theta_, __}, plist:{phi_, __}, opts___ ] :=
  130.     Module[{rs},
  131.         ParametricPlot3D[ {(rs = r) Sin[theta] Cos[phi],
  132.                            rs Sin[theta] Sin[phi],
  133.                            rs Cos[theta],
  134.                            style},
  135.                           tlist, plist, opts ]
  136.     ]
  137.  
  138. SphericalPlot3D[ r_, tlist:{theta_, __}, plist:{phi_, __}, opts___ ] :=
  139.       ParametricPlot3D[ r{Sin[theta] Cos[phi],
  140.                           Sin[theta] Sin[phi],
  141.                           Cos[theta]},
  142.                         tlist, plist, opts ]
  143.  
  144.  
  145. Attributes[CylindricalPlot3D] = {HoldFirst}
  146.  
  147. CylindricalPlot3D[ {z_, style_}, rlist:{r_, __}, plist:{phi_, __}, opts___ ] :=
  148.     ParametricPlot3D[{r Cos[phi], r Sin[phi], z, style}, rlist, plist, opts]
  149.  
  150. CylindricalPlot3D[ z_, rlist:{r_, __}, plist:{phi_, __}, opts___ ] :=
  151.     ParametricPlot3D[{r Cos[phi], r Sin[phi], z}, rlist, plist, opts]
  152.  
  153. Protect[ Evaluate[protected] ]
  154.  
  155. End[]   (* Graphics`ParametricPlot3D`Private` *)
  156.  
  157. Protect[PointParametricPlot3D, SphericalPlot3D, CylindricalPlot3D]
  158.  
  159. EndPackage[]   (* Graphics`ParametricPlot3D` *)
  160.  
  161. (*:Limitations: none known. *)
  162.  
  163. (*:Tests:
  164. *)
  165.  
  166. (*:Examples:
  167.  
  168. CylindricalPlot3D[ r^2, {r,0,1}, {phi,0,2 Pi}]
  169.  
  170. CylindricalPlot3D[ r^2, {r,0,1}, {phi,-Pi/4,5 Pi/4},
  171.    Boxed -> False, ViewPoint ->{1.3,-2.4,1.6} ]
  172.  
  173. SphericalPlot3D[ Sin[theta]^2, {theta,0,Pi}, {phi,0,3 Pi/2}]
  174.  
  175. CylindricalPlot3D[ 1.5 Sqrt[1+r^2], {r,0,2}, {phi, 0, 2Pi}]
  176.  
  177. ParametricPlot3D[ { Cosh[z] Cos[phi], Cosh[z] Sin[phi], z },
  178.    {z, -2, 2}, {phi, 0, 2 Pi} ]
  179.  
  180. *)
  181.