MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Compute once question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105724] Re: Compute once question
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Wed, 16 Dec 2009 06:21:14 -0500 (EST)
  • References: <hg00d9$j98$1@smc.vnet.net> <200912131038.FAA21060@smc.vnet.net> <hg7v80$en5$1@smc.vnet.net>

Hi,

> Thanks David. I think I'm 'zeroing' all the variables before I use them. Got
> some nasty displays early on that I've cleaned out so I think I know what
> you're referring to.

I think the delay is not due to the recalculation, but due to the time
the FrontEnd needs to render the graphics. So even if it is not
reevaluating the graphics, it is re-rendering, which is what is time
consuming. You can verify this by replacing the Graphics with e.g.
ByteCount[dttn], you will find that everything evaluates almost immediatly.

This means that everything works as you intended, but it doesn't help
the manipulate to be more reactive. To let the FrontEnd know that it
does not need to re-render the Graphics you will need to use extra
Dynamics as is described in the advanced documentation for Dynamics. I
have done this in the code below. Note that I put all the calculation
into a function, which was handy when experimenting with the placing of
the call to it and the Dynamics. It might be instructive to play around
with those details, but maybe also disturbing. If you want the
Manipulate to be "self contained" you can use the option SaveDefinitions
-> True instead of putting everything within the Manipulates body...

msg = {
   "A circle has a circumference of 2\[Pi]r...",
   "...so halfway around a circle is \[Pi]r",
   "We can re-arrange the slices without changing the area...",
   "...or the distance halfway around the circle.",
   "Slice the circle into more pieces & the right shape morphs...",
   "...into a rectangle",
   "The rectangle is r units high and 2\[Pi]r units wide..."};

Slices = {4, 8, 16, 100};

recalc[Step_Integer] := (
   n = 1;
   If[Step == 1,
    n = 1; ms = 1; showSlice = 0; showSlice2 = 0;
    showPerim = 0;
    showPerim2 = 0;,
    If[Step == 2, showPerim = 1; ms = 2;];
    If[Step == 3, showSlice = 1; showSlice2 = 1; ms = 3;];
    If[Step == 4, showPerim2 = 1; ms = 4; showSlice = .5];
    If[Step == 5, ms = 5; n = 2;];
    If[Step > 5 && Step < 9, n = Part[Slices, Step - 4];];
    If[n == 100, ms = 6];
    ];
   If[Step == 9, ms = 7];
   nm = Text[Style[msg[[ms]], Bold, 24], {3.25, 2.7}];
   If[Step <= 8,
    \[Theta] = Pi/(n + 1); bottom = Red; top = Gray;
    ef = If[n < 15, White, If[n < 30, LightBlue, None]];
    cn = Table[
      Circle[{1, 1}, 1, {s \[Theta], s \[Theta] + \[Theta]}], {s, 0,
       n}];
    ct = Table[
      Rotate[Take[cn, {s1}],
       Pi/2 - (s1 - 1) \[Theta] - \[Theta]/2, {1, 1}], {s1, 1, n + 1}];
    ctt =
     Table[Translate[
       Take[ct, {s2}], {2 (s2 - 1) Sin[\[Theta]/2] +
         2, -Cos[\[Theta]/2]/2}], {s2, 1, n + 1}];
    p = {3, 1 - Cos[\[Theta]/2]/2};
    p2 = p + {Cos[Pi/2 + \[Theta]/2], Sin[Pi/2 + \[Theta]/2]};
    ra = Line[{{1, 1}, {0, 1}}];
    ra2 = Line[{p, p2}];
    ral = Text[Style["r", Bold, 36], {.5, 1.1}];
    ral2 = Translate[ral, {1.3 + Cos[\[Theta]/2], -.10}];
    cir = Text[Style["\[Pi]r", Bold, 36], {1, 2.2}];
    cir2 = Translate[cir, {2 + Cos[\[Theta]/2], -.5}];
    dn = Table[
      Disk[{1, 1}, 1, {s \[Theta], s \[Theta] + \[Theta]}], {s, 0, n}];
    dt = Table[
      Rotate[Take[dn, {s1}],
       Pi/2 - (s1 - 1) \[Theta] - \[Theta]/2, {1, 1}], {s1, 1, n + 1}];
    dtt =
     Table[Translate[
       Take[dt, {s2}], {2 (s2 - 1) Sin[\[Theta]/2] +
         2, -Cos[\[Theta]/2]/2}], {s2, 1, n + 1}];
    dnn =
     Table[Disk[{1, 1},
       1, {s \[Theta] + Pi, Pi + s \[Theta] + \[Theta]}], {s, 0, n}];
    dtn =
     Table[Rotate[Take[dnn, {s1}],
       3 Pi/2 - (s1 - 1) \[Theta] - \[Theta]/2 + Pi, {1, 1}], {s1, 1,
       n + 1}];
    dttn =
     Table[Translate[
       Take[dtn, {s2}], {2 (s2 - 1) Sin[\[Theta]/2] +
         2*Sin[\[Theta]/2]/2 + 2, Cos[\[Theta]/2]/2}], {s2, 1, n + 1}];
    ];
   );

Manipulate[
 Graphics[{
    EdgeForm[ef], Dynamic[recalc[step]; nm],
    {FaceForm[top], dn},
    {FaceForm[bottom], dnn},
    {FaceForm[top], Opacity[showSlice], dtt},
    {
     FaceForm[bottom], Opacity[showSlice2], dttn,
     {Thick, Blue, {
       {Opacity[showPerim], cn, ra, cir, ral},
       {Opacity[showPerim2], ctt, cir2, ra2, ral2}}
      }
     }
    },
   PlotRange -> {{-.50, 6.5}, {0, 3}},
   Axes -> True, Background -> White,
   ImageSize -> {800, 470}
   ],
 {step, Table[ii, {ii, 9}], ControlType -> SetterBar}
 ]

hth,

albert


  • Prev by Date: Re: Compute once question
  • Next by Date: Re: Cannot solve very simple equation.
  • Previous by thread: Re: Compute once question
  • Next by thread: Re: Import from web addresses fails