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]}]
]