I needed to display edge labels of a graph in a way that allows the edge labels to be moved.
With[
{coords = {{1.08, 0.94}, {1.08, 0.036}, {0., 0.97}, {0., 0.}, {1.94, 0.49}},
edges = {{1, 2}, {1, 3}, {2, 5}, {3, 4}, {4, 2}, {5, 1}}},
DynamicModule[
{edgePosns = Table[0.5, {Length@edges}]},
(betweenPnt[a_, b_, l_] := (1 - l) a + l b;
DynamicModule[
{edgeCentres =
MapThread[
With[{av = coords[[#1[[1]]]], bv = coords[[#1[[2]]]]},
Dynamic[betweenPnt[av, bv, #2]]] &,
{edges, edgePosns}]},
Column@{
Graphics[
GraphicsComplex[
coords,
{{Line[edges]}, {Darker@Red, PointSize[0.02],
Map[Point, Range[5]]},
Map[Locator, edgeCentres]}], ImageSize -> 400],
Dynamic[DownValues[betweenPnt]]}])]]
What puzzled me is that calls to betweenPnt
are memoised when the Locator
-s are moved, as shown in the next image (apologies for tiny fonts):
What I do not understand is how or if the cacheing and the locator positions are connected, though I've understood from the related question that it is standard behaviour that the latter persist even when graphic is deleted and the code is re-evaluated.
Incidentally, I noticed that the DownValues
are not updated dynamically despite the Dynamic
wrapper around them; only after re-evaluating the whole expression do these changes appear.