This makes a slider to control the number of points. Click in the Graphics
and the points slide over to where you click.
Manipulate[
DynamicModule[{points,oldPoints,centerG,oldCenterG,finalPnts,mousePosn},
points=Map[{10,12}+#&,Table[{Cos[2.0 Pi k/n],Sin[2.0 Pi k/n]},{k,1,n}]];
oldPoints=finalPnts=points;
oldCenterG=centerG=Mean/@Transpose[points];
Dynamic[EventHandler[Framed[Graphics[{PointSize[0.02],Point[
If[TrueQ[points==finalPnts],
oldCenterG=centerG;
oldPoints=points,
(* else *)
points=MapThread[
With[{mag=Norm[#2-#1]},If[TrueQ[mag<=0.02],#2,#1+0.04(#2-#1)/mag]]&,
{points,finalPnts}]
]
]},PlotRange->{{-15,15},{-15,15}},ImageSize->{600,600}]],
{"MouseDown":>(
mousePosn=MousePosition["Graphics"];
centerG=Mean/@Transpose[points];
finalPnts=Map[(#+mousePosn-oldCenterG)&,oldPoints]) ,
"MouseUp":>(centerG=mousePosn)
}]]
],{{n,3},3,8,1}]
The problem is that my real application doesn't allow use of Manipulate
.
I thought the code below would work, but it breaks when you click in the Graphics
.
How to get the second version working?
DynamicModule[{points, oldPoints, centerG, oldCenterG, finalPnts,
mousePosn, n},
points =
Dynamic[Map[{10, 12} + # &,
Table[{Cos[2.0 Pi k/n], Sin[2.0 Pi k/n]}, {k, 1, n}]]];
oldPoints = finalPnts = points;
oldCenterG = centerG = Mean /@ Transpose[points];
Column[{
Slider[Dynamic[n], {3, 8, 1}],
Dynamic[EventHandler[Framed[Graphics[{PointSize[0.02], Point[
If[TrueQ[points == finalPnts],
oldCenterG = centerG;
oldPoints = points,
(* else *)
points = MapThread[
With[{mag = Norm[#2 - #1]},
If[TrueQ[mag <= 0.02], #2, #1 + 0.04 (#2 - #1)/mag]] &,
{points, finalPnts}]
]
]}, PlotRange -> {{-15, 15}, {-15, 15}},
ImageSize -> {400, 400}]],
{"MouseDown" :> (
mousePosn = MousePosition["Graphics"];
centerG = Mean /@ Transpose[points];
finalPnts = Map[(# + mousePosn - oldCenterG) &, oldPoints]) ,
"MouseUp" :> (centerG = mousePosn)
}]]}]]
Manipulate
? This can obviously be re-written as aDynamicModule
but are you able to use that? What is causing your restrictions? $\endgroup$