1
$\begingroup$

I want to create a Module which puts several parametric surfaces together as one object. For example, say I want to create a module which returns a cylinder that has a top, bottom, and cylindrical "middle" whose dimensions are based on some given values. I tried the following:

cyl[x0_, x1_] := Module[{r, mid, left, right, cylinder},
   mid = (x0 + x1) / 2;
   r = Sqrt[1 - mid^2];
   left = 
    ParametricPlot3D[{x0, u, v}, {u, -r, r}, {v, -Sqrt[1 - u^2], 
      Sqrt[1 - u^2]}];
   right = 
    ParametricPlot3D[{x1, u, v}, {u, -r, r}, {v, -Sqrt[1 - u^2], 
      Sqrt[1 - u^2]}];
   cylinder = 
    ParametricPlot3D[{u, r*Cos[v], r*Sin[v]}, {u, x0, x1}, {v, 0, 
      2 Pi}];
   Graphics3D[{left, cylinder, right}]
   ];

So, what I'm doing is making 'left' a 'cylinder' and a 'right', and I want to stick them all together. I do not want to instantly display them, however. I want to be able to use a Table to call this cyl with several different parameters and have them all displayed together.

$\endgroup$
2
$\begingroup$

Try changing your last line.

cyl[x0_, x1_] := Module[{r, mid, left, right, cylinder},
  mid = (x0 + x1)/2;
  r = Sqrt[1 - mid^2];
  left =
   ParametricPlot3D[{x0, u, v}, {u, -r, r},
    {v, -Sqrt[1 - u^2], Sqrt[1 - u^2]}];
  right =
   ParametricPlot3D[{x1, u, v}, {u, -r, r},
    {v, -Sqrt[1 - u^2], Sqrt[1 - u^2]}];
  cylinder =
   ParametricPlot3D[{u, r*Cos[v], r*Sin[v]},
    {u, x0, x1}, {v, 0, 2 Pi}];
  Normal@*First /@ {left, right, cylinder}]

e.g.

Graphics3D[{cyl[0.12, 0.3], cyl[0.002, 0.5]}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ When I try that I get "SetDelayed::write: Tag Graphics3D in ()[x0_,x1_] is Protected." followed by an output of "$Failed". $\endgroup$ – Chris Johnson Aug 28 at 20:12
  • $\begingroup$ Perhaps you included the example call in the Module? $\endgroup$ – Chris Degnen Aug 28 at 20:25
  • $\begingroup$ No; I get the same error message when I copy/paste your code. $\endgroup$ – Chris Johnson Aug 28 at 20:39
  • 1
    $\begingroup$ @ChrisJohnson - try running ClearAll[cyl] to remove old definitions before adding new definitions $\endgroup$ – Jason B. Aug 28 at 20:49
  • $\begingroup$ @JasonB That did it, thanks! $\endgroup$ – Chris Johnson Aug 28 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.