Message Boards Message Boards

Why does this Dynamic function argument for Point fail in Free CDF?

Posted 6 years ago

I am working on a demo that will require some fairly complicated graphical interfaces involving dragging points round in the Euclidean Plane. I would like to be able to interact with these interfaces in Free CDFs. Apparently, there are aspects of the use of Dynamic that I don't fully understand. I would appreciate any knowledge of why the following example works in Mathematica 11.3, but showDemo2 fails when I select "CDF Preview " from the File menu. It appears that the function

Dynamic[getIntermediates[p1, p2, n]]

is evaluated before applying Point in Mathematica, but not in the CDF. In showDemo, when I generate the Table of points directly within the Point argument rather than through a function call, the Graphics work fine in both Mathematica and CDF.

Remove[getIntermediates];
Remove[showDemo];
Remove[showDemo2];

getIntermediates[p1_, p2_, n_] := 
 Table[With[{i = i}, p1 + (i/n) (p2 - p1)], {i, 1, n - 1}];

showDemo[point1_, point2_, parts_, size_] :=

 DynamicModule[{p1 = point1, p2 = point2, n = parts},
  Column[{
    (* Control the number of parts into which to divide the segment \
p1, p2 *)
    SetterBar[Dynamic[n], Range[5]],
    Graphics[{
      EdgeForm[{Thin, Black}], Arrow[Dynamic[{p1, p2}]],
      PointSize[Medium],
      Black, Point[Dynamic[p1]],
      PointSize[Medium],
      (* Use Table to generate a (possible empty) list of \
intermediate points *)
      Red, 
      Point[Dynamic[
        Table[With[{i = i}, p1 + (i/n) (p2 - p1)], {i, 1, n - 1}]]]
      ,
      Locator[Dynamic[p1], Appearance -> None],
      Locator[Dynamic[p2], Appearance -> None]
      }
     , Frame -> True, ImageSize -> size, 
     PlotRange -> {{-1.0, 1.0}, {-1.0, 1.0}}
     ]
    }]
  ]

showDemo2[point1_, point2_, parts_, size_] :=

 DynamicModule[{p1 = point1, p2 = point2, n = parts},
  Column[{
    (* Control the number of parts into which to divide the segment \
p1, p2 *)
    SetterBar[Dynamic[n], Range[5]],
    Graphics[{
      EdgeForm[{Thin, Black}], Arrow[Dynamic[{p1, p2}]],
      PointSize[Medium],
      Black, Point[Dynamic[p1]],
      PointSize[Medium],
      (*  Use a function to return the list of intermediate points *)

            Red, Point[Dynamic[getIntermediates[p1, p2, n]]]
      ,
      Locator[Dynamic[p1], Appearance -> None],
      Locator[Dynamic[p2], Appearance -> None]
      }
     , Frame -> True, ImageSize -> size, 
     PlotRange -> {{-1.0, 1.0}, {-1.0, 1.0}}
     ]
    }]
  ]

With[{pt1 = RandomReal[{-1.0, 1.0}, 2], 
  pt2 = RandomReal[{-1.0, 1.0}, 2]}, Row[{showDemo[pt1, pt2, 2, Small],
   showDemo2[pt1, pt2, 2, Small]}]
 ]
POSTED BY: dan Erickson
2 Replies

Thanks, Kuba!

I didn't realize that setting SaveDefinitions was necessary if the required definition was part of the notebook and the entire notebook was converted to CDF. Now I can get back to the rest of my development instead of beating my head against this particular wall.

-Dan

POSTED BY: dan Erickson

Not sure I got the point but if the issue is the missing definition of getIntermediates in CDF. Then you need to add SaveDefinitions -> True in your DynamicModule.

POSTED BY: Kuba Podkalicki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract