I attempt to trace the path of a point when shift-dragged on a polar coordinate as shown on the picture below. But I only managed to snap the point to the grid. Insight on how to accomplish this? Any help would be much appreciated.
VPOS = {1, 1};
oVPOS = {1, 1};
angle[p_] := (
ang = ArcTan[Abs@p[[2]]/Abs@p[[1]] // N ];
Which[p[[1]] >= 0 && p[[2]] >= 0 , Return[ang ],
p[[1]] < 0 && p[[2]] >= 0 , Return[Pi - ang ],
p[[1]] < 0 && p[[2]] < 0 , Return[-Pi + ang ],
p[[1]] >= 0 && p[[2]] < 0 , Return[-ang ]
];)
cnt[p_] := (
d = Round@EuclideanDistance[{0, 0}, p];
the2 = Abs@Ceiling[ArcTan[p[[2]]/p[[1]]]/(Pi/12)];
Return[{Sign[p[[1]]] d Cos[the2*Pi/12 ], Sign[p[[2]]] d Sin[the2 *Pi/12 ]}];)
cnt2[p_] := (
od = Round@EuclideanDistance[{0, 0}, oVPOS];
othe2 = Ceiling[angle[oVPOS]/(Pi/12) ] ;
d = EuclideanDistance[{0, 0}, p];
the2 = angle[p];
dd = Round@d;
dthe2 = Ceiling[the2/(Pi/12)];
Dthe = Abs[od*( the2 - othe2) ] ;
Dd = Abs[ d - od];
If[Dthe > Dd,
Return[{ dd Cos[ the2], dd Sin[ the2 ]}],
Return[{ d Cos[dthe2*Pi/12], d Sin[dthe2*Pi/12]} ]
]
)
grids[min_, max_] :=
Join[Range[Ceiling[min], Floor[max]],
Table[{j + 1, Lighter@Lighter@Lighter@Lighter@Green}, {j,
Round[min], Round[max - 1], 1}]];
DynamicModule[{pnt = {1, 1} },
EventHandler[
Dynamic@Graphics[
{PointSize[Large], Red, Point[cnt2[VPOS]]},
Axes -> True,
GridLines -> grids,
PlotRange -> {{-10, 10}, {-10, 10}},
Prolog -> {
Lighter @ Lighter @ Blue,
Table[Circle[{0, 0}, r], {r, 1, 14}],
Table[Line[{{-15 Cos[the], -15 Sin[the]},
{15 Cos[the], 15 Sin[the]}}],
{the, 0, Pi, Pi/12}]}],
{"MouseDragged" :> (
oVPOS=VPOS;
VPOS = MousePosition["Graphics"])}]]
This is what my code produces:
This is what I want to see: